While loop runs infinitely

조회 수: 4 (최근 30일)
Anthony Siddique
Anthony Siddique 2015년 12월 1일
댓글: Torsten 2015년 12월 1일
I'm trying to create a function that takes two parameters: x and threshold. X is the angle and the threshold is the percent error I need to get when approximating sine with the taylor series. However my while loop runs infinitely and I'm very confused as to how do go about fixing it.
Here's what I have so far.
function [approx, terms] = approx_sine(x, threshold)
approx = x; % Initial approximation
terms = 0; % Number of additional terms added to improve the approximation;
% Write your code here using a while loop to improve the above approximation
while threshold <= abs((sin(x)-approx)/sin(x))
terms = terms + 2;
approx = (-1)^(terms+1)*(x.^terms)/ factorial(terms)+x;
end
end

채택된 답변

Thorsten
Thorsten 2015년 12월 1일
편집: Thorsten 2015년 12월 1일
The code for approximation is wrong. There are some errors in your formula: term takes values 0, 2, 4, ... but should take values 1,3,5,7; the (-1)^n changes sign with every new term, but since your term is always even, (-1)^(term+1) is always odd; and finally you add the n'th approximation term to the initial approximation x, instead of adding it to the most recent approximation.
This works
function [approx, n] = approxsine(x, threshold)
approx = x; % Initial approximation
n = 0; % Number of additional terms added to improve the approximation;
% Write your code here using a while loop to improve the above approximation
while threshold <= abs((sin(x)-approx)/sin(x))
n = n + 1;
approx = approx + (-1)^(n)*x.^(2*n+1)/factorial(2*n+1);
end
end
  댓글 수: 2
Image Analyst
Image Analyst 2015년 12월 1일
And I'm a very strong advocate of failsafes, especially in while loops to prevent just this problem of infinite loops. Check the loop counter and if it's more than you ever realistically expect, bail out with an error message:
n = 1;
nMax = 1000000
while threshold <= abs((sin(x)-approx)/sin(x)) && n < nMax
n = n + 1;
approx = approx + (-1)^(n)*x.^(2*n+1)/factorial(2*n+1);
end
if n >= nMax
errorMessage = sprintf('No solution found after %d iterations!', n-1);
uiwait(errordlg(errorMessage));
end
Note the additional check of "&& n < nMax" I added to the while line.
Torsten
Torsten 2015년 12월 1일
Since sin(x) < = 1, better use absolute instead of relative error within error estimate:
while threshold <= abs(sin(x)-approx)
Best wishes
Torsten.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 12월 1일
Remove the line
threshold = abs((sin(x)-approx)/sin(x))
It is overwriting the threshold that you passed in.
  댓글 수: 2
Anthony Siddique
Anthony Siddique 2015년 12월 1일
I'm still having the same problem. The while loop won't resolve.
Walter Roberson
Walter Roberson 2015년 12월 1일
>> limit((-1)^(terms+1)*x^terms/factorial(terms), terms = infinity);
0
Your approx is (-1)^(terms+1)*x^terms/factorial(terms) + x . As I indicate above, as terms increases, the first part of that tends to 0 for all x. With the first part tending to 0, only the second part will start to matter, the "+ x" part. Your approx goes to x.
Your termination test then becomes
threshold <= abs((sin(x)-x)/sin(x))
or threshold <= abs(1 - x/sin(x))
For any given threshold there is a limited range of x that can satisfy this, and that range of x does not increase smoothly with the threshold.

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by