Having trouble with Taylor Series summation

조회 수: 2 (최근 30일)
Samuel Temes
Samuel Temes 2017년 4월 22일
댓글: Torsten 2017년 4월 24일
I'm estimating the value of sin(x) using the summation form of the sin(x) taylor series. I'm supposed to sum the first 250 terms terms but I'm getting NaN. This code worked fine for the first 100 and didn't read NaN until after the first 220 terms. Is there another way around this?
a20 = input('Input a value for X');
aSum2 = 0;
for j = 0:250 %Sums first 220 terms !!!After 220 Matlab reads "NaN"
%adds a23 to aSum for every itteration
a23 = ((-1)^j*a20^(2*j+1))/(factorial(2*j+1));
aSum2 = aSum2+a23;
end
disp(aSum2);

답변 (1개)

John D'Errico
John D'Errico 2017년 4월 22일
편집: John D'Errico 2017년 4월 22일
Why do you think you were told to compute 250 terms?
What is a20? By the way, it is a REALLY bad idea to use numbered variables like that. You have a20, a23, etc. Those variables have little in common, except that they are in the same program. If you continue down this line, you will soon be in programming hell, with completely unreadable, un-debuggable code.
Regardless, what is the value of a20^(2*221 + 1) ? Since I have no idea what a20 is, how can I know?
What is the value of factorial(2*221 + 1) ? I'll help you out here.
factorial(2*221 + 1)
ans =
Inf
Next, what happens when you try to compute that ratio?
Never just write a formula without thinking about what you are trying to compute. When you get garbage, look at the terms in that formula.
Next, can you compute each term in that series from the previous one? (Of course you can.) You never need to compute a factorial at all, IF you think about the relationship between successive terms.
So, go back to the formula for a factorial, and figure out the relationship between
a20^(2*j+1)/factorial(2*j + 1)
and
a20^(2*(j-1)+1)/factorial(2*(j-1) + 1)
Come on. You need to think, as this is a key part of what you need to do. I've already told you a huge amount, so now it is your turn.
  댓글 수: 2
Samuel Temes
Samuel Temes 2017년 4월 23일
I use those odd variable names because of the nature of my homework. We have multiple sections that we have to turn in, in the same file (ex; question a sub-question 2 variable 1: a21). I wouldn't code like that normally. At the risk of sounding stupid, can you clarify what you mean by the factorial relationship you're talking about? I'm probably overthinking it.
Torsten
Torsten 2017년 4월 24일
Use
a20^(2*(j+1)+1)/(factorial(2*(j+1)+1)) =
a20^(2*j+1)/factorial(2*j+1) * a20^2/((2*j+2)*(2*j+3))
Best wishes
Torsten.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by