필터 지우기
필터 지우기

I need help with a taylor series approximation of e^x

조회 수: 2 (최근 30일)
Thomas MacDowell
Thomas MacDowell 2018년 7월 14일
답변: sparsh mehta 2021년 5월 19일
Please help!
I have a portion of a code I am trying to write that isn't co-operating. This function is written to approximate e^x by a taylor series expansions using a set number of terms. Earlier on in the code I successfully evaluated the function at 3 and 5 terms. My third task is to figure out how many terms it takes to get an evaluation within 0.000001 error.
%%Necessary Terms %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
errMax = 0.000001;
ex = 1;
nMax = 1000000;
for nInitial=0:nMax
while abs((exp(x)-ex)/exp(x))*100 >= errMax
ex = ex + x.^nInitial/factorial(nInitial);
end
end
fprintf(['The number of terms necessary for the function to be within the allowed error of 0.0001 is ' num2str(nInitial) '.\n']);
fprintf(['The true value of e^3 is ' num2str(exp(3)) '.\n']);
end
Please help!

채택된 답변

Walter Roberson
Walter Roberson 2018년 7월 14일
for nInitial=0:nMax
if abs((exp(x)-ex)/exp(x))*100 < errMax
break;
end
ex = ex + x.^nInitial/factorial(nInitial);
end
  댓글 수: 4
Thomas MacDowell
Thomas MacDowell 2018년 7월 14일
at the 647th term in the series it turns into 'NaN' which is why it isn't giving me an answer, how can I get the number of terms? I'mm pulling my hair out over this
Walter Roberson
Walter Roberson 2018년 7월 14일
Your calculation is off by 1.0. Initialize ex to 0 instead of 1.

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

추가 답변 (1개)

sparsh mehta
sparsh mehta 2021년 5월 19일
or nInitial=0:nMax
if abs((exp(x)-ex)/exp(x))*100 < errMax
break;
end
ex = ex + x.^nInitial/factorial(nInitial);
end

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by