In: Mechanical Engineering
Plot the estimate of the derivative dy"dx from the following data. Do this by using forward, backward, and central differences. Compare the results.
clear
clc
x=[0 1 2 3 4 5 6 7 8 9 10];
y=[0 2 5 7 9 12 15 18 22 20 17];
l=1;m=1;n=1;
for i=1:10
forward(l)=(y(i+1)-y(i))/(x(i+1)-x(i))
l=l+1;
end
for j=2:11
backward(m)=(y(j)-y(j-1))/(x(j)-x(j-1))
m=m+1;
end
for k=2:10
central(n)=(y(k+1)-y(k-1))/2
n=n+1;
end
x1=0:9;
plot(x1,forward,\'r\');
hold on
x2=1:10;
plot(x2,backward,\'g\')
hold on
x3=1:9;
plot(x3,central,\'b\')
grid on
legend(\'forward\',\'backward\',\'central\')