MATLAB Comparing Performance of Difference Methods for Estimate Derivative

AI Thread Summary
The discussion focuses on the implementation of numerical methods for estimating the derivative of the function y(x) = e^(-x)sin(3x) using MATLAB. The user encounters issues with syntax errors in their code, particularly in how they express multiplication and function definitions. Key points include the need to use the element-wise multiplication operator (.*) when dealing with vectors in MATLAB, as well as the requirement to explicitly include multiplication symbols (*) between variables and constants. The correct formulation for the function and its derivative is emphasized, with the final correct expression for the true derivative being dyt = -exp(-x).*sin(3*x) + 3*exp(-x).*cos(3*x). The discussion highlights the importance of precise syntax in programming to avoid errors and achieve accurate computations.
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 hav 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
4
Views
2K
Replies
2
Views
2K
Replies
4
Views
3K
Replies
1
Views
2K
Replies
2
Views
6K
Replies
1
Views
3K
Replies
1
Views
4K
Back
Top