Comparing Performance of Difference Methods for Estimate Derivative

Click For Summary

Discussion Overview

The discussion focuses on comparing the performance of forward, backward, and central difference methods for estimating the derivative of the function y(x)=e^-xsin(3x). Participants are addressing issues related to MATLAB syntax and function definitions while implementing these methods over a specified range of x values with added random noise.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant expresses confusion over entering the function correctly in MATLAB, specifically questioning the syntax used for multiplication.
  • Another participant suggests using explicit multiplication with the correct syntax to avoid errors, highlighting the need for element-wise operations when dealing with vectors.
  • A third participant introduces the use of inline functions in MATLAB and corrects the earlier suggestion by stating that an inline function is not necessary for this case.
  • Subsequent posts address errors encountered when calculating the true derivative, with participants emphasizing the importance of proper syntax, particularly the use of the multiplication operator.
  • One participant clarifies that the correct expression for the true derivative requires the use of element-wise multiplication and proper placement of the multiplication operator.

Areas of Agreement / Disagreement

Participants generally agree on the need for correct MATLAB syntax and the use of element-wise operations, but there is no consensus on the best approach to implement the derivative estimation methods, as some participants are still encountering errors.

Contextual Notes

Limitations in the discussion include unresolved syntax errors and the need for clarity on MATLAB's operational rules, particularly regarding vector operations and function definitions.

beatboxbo
Messages
5
Reaction score
0
The problem is: Compare the performance of the forward, backward, and central difference methods for estimating the derivative of the following function: y(x)=e^-xsin(3x). Use 101 points from x = 0 to x = 4. Use a random additive error of +/- 0.01.

I understand the process of solving this one but I keep getting errors with how I enter my function, I still don't get all of the rules for those. What I am using is
y = exp(-x)*sin(3x) I am guessing there's something wrong with this? Ill enter the rest of my entry to make it easier:

>> x = linspace(0,4,101);
n = length(x);
% The sinusoidal funciton
y = exp(-x)*sin(3.x);
% added with measurment errors;
ym = y + 0.01*2*(rand(1,n)-0.5);
% Note: rand(1,n) returns a vector of uniformly distributed random numbers
% between 0.0 and 1.0
figure(1);
plot(x,y,'k-', x,ym,'bo');
xlabel('x');
ylabel('y');
legend('y=exp(-x)*sin(3x)','meared signal with noise');
% The true derivative
dyt = -exp(-x)*sin(3x) + 3exp(-x)*cos(3x);
% backward difference
dyb = diff(ym)./diff(x);
xb = x(2:n);
% forward difference
dyf = diff(ym)./diff(x);
xf = x(1:n-1);
% central difference
dyc = gradient(ym)./gradient(x);
xc = x;
figure(2);
plot(x,dyt,'k');
hold on;
plot(xf, dyf, 'bo');
plot(xb, dyb, 'gs');
plot(xc, dyc, 'r^');
legend('true derivative','forward difference', 'backward','central');
xlabel('x');
ylabel('dy/dx');
? y = exp(-x)*sin(3x);
|
Error: Unexpected MATLAB expression.
 
Physics news on Phys.org
Try y = exp(-x)*sin(3*.[/color]x)

In MATLAB you have to explicitly write out each mutliplication (and division), otherwise MATLAB treats '3x' as a variable, rather than '3 times x'.
 
Last edited:
In Matlab you define an inline function like this

>> y = inline('exp(-x).*sin(3*x)')

the .* instead of simply * is because x may be a vector, so we want an element by element product, which returns a vector. Note that the inverted commas are necessary.

EDIT: silly me, you don't need an inline function, you need this:

>> exp(-x).*sin(3*x);
 
Last edited:
Thanks for the help guys I appreciate it.

Now I have another problem though, ha, for the true derivative I'm getting another error now. Dont know if I entered the wrong derivative or what I did?

This is what I got:

% The true derivative
dyt = -exp(-x)*sin(3x) + 3exp(-x)*cos(3*x);

and the error:
? dyt = -exp(-x)*sin(3x) + 3exp(-x)*cos(3*x);
|
Error: Unexpected MATLAB expression.

Thanks Again
 
x is a vector right? So exp(-x) returns a vector, and sin(3*x) returns a vector too. The multiplication you are trying to do is element by element, so you require .* and NOT *, e.g.
>> [2 5 7].*[3 3 2] = [6 15 14]
However
>> [2 5 7]*[3 3 2]
is incorrect; it is not a well-formed expression in Matlab. * is used when one of the things you are multiplying together is a scalar, so it is correct to use * in the following situations:
>> 5*[1 5] = [5 25]
>> 5*3 = 15
>> [0 4]*3 = [0 12]
If you want to multiply two vectors element-by-element, you have to do it as I showed above, using .*
Remember WHENEVER you multiply two things, it doesn't matter what they are, you need to use * or .*, you don't simply put them next to each other. So this is wrong:
>> 3[1 2]
Similar comments apply to the other operation ^,\, but not to addittio and subtraction, but remember to only add and subtract like things, e.g. vectors of the same length, matrices of the same size (both 5x3 or whatever) etc. If you are unsure of whether to us * or .*, or + or .+, just remember that a dot before an operation ALWAYS works, no matter what the objects used (provided all else is correct), so the following all work:
>> 3*[3 1 6] = [9 3 18]
>> 3.*[3 1 6] = [9 3 18]
>> [4 5 2] + [1 1 6] = [5 6 8]
>> [4 5 2] .+ [1 1 6] = [5 6 8]
and so on.
Remember, computers are VERY finnicky about what you type. Garbage in, garbage out. They have been programmed to accept exact statements, and if you don't enter your commands exactly as the program was written, the program cannot guess what you are trying to.
The correct expression you were looking for is:
>> dyt = -exp(-x).*sin(3*x) + 3*exp(-x).*cos(3*x);
Do you understand why?
Cheers.
 
beatboxbo said:
This is what I got:

% The true derivative
dyt = -exp(-x)*sin(3x) + 3exp(-x)*cos(3*x);

and the error:
? dyt = -exp(-x)*sin(3x) + 3exp(-x)*cos(3*x);
|
Error: Unexpected MATLAB expression.

Thanks Again


You should have typed 3*exp(-x)*cos(3*x)
The * was missing. :biggrin:
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
2K
Replies
7
Views
3K
Replies
5
Views
8K