Borrar filtros
Borrar filtros

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 visualización (últimos 30 días)
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);

Respuestas (1)

Roger Stafford
Roger Stafford el 5 de En. de 2016
Editada: Roger Stafford el 5 de En. de 2016
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.

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by