Maclauren Series Iteration. Answer provided just having trouble getting code to run properly

1 visualización (últimos 30 días)
clear
new_cos=1;
x=pi/3;
stop=cos(x);
es=0.5;
ea=100;
et=100;
n=1;
nmax=1000;
while es<ea && stop~=new_cos%(stop<=new_cos) && (es<ea)%(n<nmax)
%while (es<ea) && (n<nmax)
old_cos=new_cos;
new_cos=(old_cos-(x^n))/(factorial(n));
et=abs(((stop-new_cos)/stop)*100);
ea=abs(((new_cos-old_cos)/new_cos)*100);
n=n+1;
%if new_cos~=0
%ea=abs(((new_cos-old_cos)/new_cos)*100);
%end
disp(new_cos)
disp(et)
disp(ea)
end

Respuesta aceptada

James Tursa
James Tursa el 3 de Feb. de 2020
Editada: James Tursa el 3 de Feb. de 2020
Some issues:
1) This line:
new_cos=(old_cos-(x^n))/(factorial(n));
You are dividing the old_cos by the factorial(n). This doesn't match the series formula. It is only the new term that gets divided by the factorial(n), not everything up to that term. So something like this:
new_cos = old_cos + something / factorial(n);
That "something" is going to be close to your -x^n, but not exactly. In your code, you always subtract. But in the series the terms have alternating signs. So you will need to adjust your code to provide alternating signs for that term.
2) The powers of the terms in your seriies go up by 2 each time, but you only have it going up by 1 each time:
n=n+1;
3) You are starting with the first term new_cos = 1, and then the next power you consider in your loop is for 1:
n=1;
But look at the series. The first power after that 1 term would be for n=2, not n=1.
You need to fix all of these issues.

Más respuestas (0)

Categorías

Más información sobre Language Fundamentals 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