Comparing Performance of Difference Methods for Estimate Derivative

In summary, the forward, backward, and central difference methods for estimating the derivative of the following function: y(x)=e^-xsin(3x) produce different results. The true derivative is -exp(-x)*sin(3x) + 3exp(-x)*cos(3*x).
  • #1
5
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
  • #2
Try y = exp(-x)*sin(3*.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:
  • #3
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:
  • #4
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
 
  • #5
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.
 
  • #6
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:
 

1. What is the purpose of comparing performance of different methods for estimating derivatives?

The purpose of comparing performance of different methods for estimating derivatives is to determine the most accurate and efficient method for calculating derivatives in a particular scenario. This can help scientists and researchers make more precise calculations and draw more accurate conclusions from their data.

2. What are some common methods for estimating derivatives?

Some common methods for estimating derivatives include the forward difference method, the backward difference method, the central difference method, and the complex-step method. Each method has its advantages and disadvantages, and comparing their performance can help determine which is most suitable for a given situation.

3. What factors should be considered when comparing the performance of different methods for estimating derivatives?

When comparing the performance of different methods for estimating derivatives, factors such as accuracy, efficiency, and stability should be considered. Accuracy refers to how close the estimated derivative is to the actual value, efficiency relates to the speed of computation, and stability refers to the robustness of the method to small changes in input values.

4. How can the performance of different methods for estimating derivatives be evaluated?

The performance of different methods for estimating derivatives can be evaluated by comparing the results of each method for a given function or data set. This can be done by calculating the error between the estimated and actual derivatives, measuring the time it takes to compute the derivatives, and analyzing the stability of the methods for different input values.

5. What are some potential applications of comparing performance of different methods for estimating derivatives?

Comparing performance of different methods for estimating derivatives can be useful in a variety of fields, such as physics, engineering, and finance. It can be applied to optimize calculations in scientific simulations, improve the accuracy of financial models, and assist in the development of new technologies and products.

Suggested for: Comparing Performance of Difference Methods for Estimate Derivative

Back
Top