Help with while loop

Hello,
I have to create a program that will find the taylor series expansion at an arbitrary value x. I also have to find the relevant error of the series expansion from the true exp.
Create a MATLAB program to compute this series and stop at %error < 3%. Display the value of the series approximation and how many terms that you used to calculate this value.
x=1.5;
y=0;
n=0;
error=0;
while(error>3)
if n<2
f=1;
elseif n>=2
f=f*n;
end
y=y+(x^n/(f));
error=((exp(x)-y)/exp(x))*100;
n=n+1;
end
disp(n-1)
disp(error)
This is currently what I have but my program will only compute for the first n. I can not seem to see what is going wrong and would appreciate any hints.

 채택된 답변

Matt Fig
Matt Fig 2012년 9월 11일
편집: Matt Fig 2012년 9월 11일

0 개 추천

You were pretty close, but there are a couple of mistakes.
  1. You start out the error at zero, then ask the loop to run only when the error is greater than 3....
  2. You are not actually calculating the factorial for each new value of n.
  3. You need to use the absolute value when calculating the error.
x=1.5;
y=0;
n=0;
ER=100; % Don't name a variable 'error'. Start >3 so loop runs!
while(ER>3)
f = 1;
for ii=2:n
f=f*ii;
end
y = y+(x^n/(f));
ER = (abs(exp(x)-y)/exp(x))*100;
n=n+1;
end
disp(n-1)
disp(ER)

댓글 수: 1

Stephen
Stephen 2012년 9월 11일
Thanks Matt! It seems so obvious now that I should have started ER 100% instead of 0%. I have also changed the variable error to ER and made some changes to the factorial and it all works now!

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

추가 답변 (1개)

Jan
Jan 2012년 9월 11일
편집: Jan 2012년 9월 11일

0 개 추천

You start at "n=0" and use "f=1" for the first two terms. In addition you want to measure the positive(!) distance between the approximation and the true result. Therefore you need the ABS() function.
Btw, do not overwrite the important function error by a local variable. Although this does not cause an error here, it will in any larger program with error handling.

댓글 수: 2

Stephen
Stephen 2012년 9월 11일
Thanks for the reply Jan! We are not aloud to use built in functions for this specific problem. I said n=0 since the summation starts from zero and I used the if/elseif statements for f to deal with the factorial since we couldn't use that function either(0!=1). For the first part to calculate the series with 15 n terms I used this script file:
x=1.5;
y=0;
for n=[0:4];
if n<2
f=1;
elseif n>=2
f=f*n;
end
y=y+(x^n/(f));
end y
exponential=exp(x)
I received the correct answer using this but now I don't know how many n terms I need that will satisfy the error condition. Tee code in my first post is my attempt and when I go through it by hand it makes sense to me but matlab stops with the first term.
Jan
Jan 2012년 9월 11일
Your program does contain over a dozen of built-in function calls. Teachers ask sometimes very stupid questions... See Answers: what can be programmed without built-ins

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2012년 9월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by