Develop a Matlab program to obtain first-derivative estimates for unequally spac
ID: 3012138 • Letter: D
Question
Develop a Matlab program to obtain first-derivative estimates for unequally spaced data. Test it with the following data: where f(x) = 5e^-2x x. Compare your results with the true derivatives. First Derivative f'(x_i) = f(x_i + 1) - f(x_i)/h f'(x_i) = -f(x_i + 2)+4f(x_i +1) - 3f(x_i)/2h Backward difference First Derivative f'(x_i) = f(x_i) - f(x_i-1)/h f'(x_i) = 3f(x_i) - 4f(x_i -1) + f(x_i-2)/2h Centered difference First Derivative f'(x_i) = f(x_i + 1) - f(x_i-1)/2h f'(x_i) = -f(x_i + 2) + 8f(x_i + 1) - 8f(x_i - 1) + f(x_i-2)/12hExplanation / Answer
Hereby,this is the program for the given question to find derrivative.
Here for loop is used only for 3 times because for 2nd derivative of centered difference can be calculated only for i=3 and other derivatives cannot be calculated for i>3 because we have only values of x till i=5.
clear all;
close all;
clc;
%Lets find first derivative using the given formula in question
f=[0.6767 .3734 .3261 .08422 .01596];
x=[1 1.5 1.6 2.5 3.5];
for i=1:1:3
f_x1=f(i); % f(x(i))
f_x3=f(i+2);
f_x2=f(i+1);
h=x(i+1)-x(i);
df1(i)=(f_x2-f_x1)/h; %with O(h) error
df2(i)=(-f_x3+4*f_x2-3*f_x1)/(2*h); % with O(h^2) error
%-------Backward difference--------------------------------
h=x(i+1)-x(i);
dfb1(i+1)=(f_x2-f_x1)/h;
h=x(i+2)-x(i+1);
dfb2(i+2)=(3*f_x3-4*f_x2+f_x1)/(2*h);
%---------------Centered Difference-----------------------------
h=x(i+1)-x(i);
dfc1(i+1)=(f_x3-f_x1)/(2*h);
if i==3
h=0.1;
f_x=f(2);
f_x0=f(1);
dfc2=(-f_x3+8*f_x2-8*f_x+f_x0)/(12*h);
end
%-------------normal differentiation------------
df(i)=5*exp(-2*x(i))*(1-2*x(i));
end
--------------------------------End------------------------------------------------------
Now you can see the results by typing the name of arrays in the command window.
eg. df for normal differentiation values corresponding to each x.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.