While loop to calculate sine

4 visualizaciones (últimos 30 días)
Mary Jeppson
Mary Jeppson el 10 de Dic. de 2016
Respondida: the cyclist el 10 de Dic. de 2016
I have written this code that settles on an incorrect value and I can't see what's wrong. Can anyone show me?
x = pi/4;
analytical = sin(pi/4);
k = 1;
n = 2;
es = .5^(2-n);
et(1) = 1;
sumterms(1) = 0;
while et>=es
thisterm = (-1)^(k+1)*x^(k*2-1)/factorial(k*2-1);
sumterms(k+1) = thisterm + sumterms(k);
et(k+1) = abs(analytical-sumterms(k))/analytical*100;
k = k+1;
end
I don't want to abuse the 'Ask' feature of this site so let me know if I'm asking too many questions. The answers are helpful to me if as long as I'm not taking advantage. Thank you!

Respuestas (1)

the cyclist
the cyclist el 10 de Dic. de 2016
I don't understand the significance of
es = .5^(2-n);
but that is a very lax accuracy criterion. es = 1 in your case, so you only have to calculate sin(pi/4) within 1!
I hard-coded
es = 0.0001;
and got a much more accurate value of sin(pi/4).

Categorías

Más información sobre Numbers and Precision 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