필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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

채택된 답변

James Tursa
James Tursa 2020년 2월 3일
편집: James Tursa 2020년 2월 3일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by