I am trying to plot, at x=pi/4, the variation of the error percentage along h (h increments from 0.01 to 0.5), using the first order central difference method for function f(x)=sinx. Can't figure out what's wrong with the code. Any help please?
1 次查看(过去 30 天)
显示 更早的评论
z = cos(pi/4);%Exact value
n=0;
for h=0.01:0.01:0.5;
n=n+1;
t(n) =((sin(pi/4)+(h))-(sin(pi/4)-(h)))/(2*h);
Percent_Error(n)= abs (((t(n)-(z))./z)*100);
end
hold on
plot (t, Percent_Error);
0 个评论
回答(1 个)
Roger Stafford
2016-1-5
编辑:Roger Stafford
2016-1-5
What you want for t(n) is:
t(n) =(sin(pi/4+h)-sin(pi/4-h))/(2*h);
In other words the increment 'h' should be applied to the argument of the sine function, not outside it.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!