Taylor Series Summation Too Large

조회 수: 1 (최근 30일)
Madi  Macias
Madi Macias 2016년 8월 6일
댓글: Madi Macias 2016년 8월 6일
My goal is to run through the first 100 terms of the generalized taylor series for sin(x) for a user defined x value. However the code i currently have is giving an output of a huge number that is not the correct summation. Here is what i have:
x=sym('x')
x=input('Enter a scalar value for x: ')
n=0:99;
k=0;
for i=1:length(n)
y(i)=((-1).^n(i))*((x.^(2*n(i))+1)/factorial((2*n(i))+1));
k=k+n(i);
end
fprintf('Summation of first 100 Terms: %d\n',k)
I'm not really sure what is causing this to happen. Thank you to anyone that can help!

채택된 답변

Mischa Kim
Mischa Kim 2016년 8월 6일
You need add up the y(i). In other words, use
k = k + y(i);

추가 답변 (1개)

Walter Roberson
Walter Roberson 2016년 8월 6일
Why are you bothering to calculate y(i) if you are going to ignore its value and base your output k strictly on the sum of the vector n?
  댓글 수: 1
Madi  Macias
Madi Macias 2016년 8월 6일
Oh goodness i didn't even realize that's what i was doing. How should i fix this?

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by