Inactive Tutor answered 07/19/13
Hi Mitwa,
The reason your program never ends is that the value of "n" in your inner while loop never changes. Hence, the value of "sn" never changes, so the code gets stuck in the infinite loop.
-Corey
M. P.
asked 12/12/12My program is never done running and I can't quite figure out why.
Here's the question:
for every n = 2, 3, 4, 5, ....100 compute a value of arcsin(1/n) by applying Newton-Raphson Method to the function f(x)=sin(x)-(1/n) with tolerance set to e = 10^-5 and an initial guess x = . Let s be the converged value for arcsin(1/n), and i be the number of iterations used.
And here's what I've got:
function [snvec, invec] = newton(maxn)
n = 2;
snvec = zeros(1, maxn-1);
invec = zeros(1, maxn-1);
while n < maxn+1
sn = 0;
in = 0;
while abs(asin(1/n) - (sin(sn)-1/n)) > 10^-5
sn = sn - (sin(sn)-1/n)/cos(sn);
in = in + 1;
end
snvec(n-1) = sn;
invec(n-1) = in;
n = n+1
end
end
and then the function is called:
newton(100)
Inactive Tutor answered 07/19/13
Hi Mitwa,
The reason your program never ends is that the value of "n" in your inner while loop never changes. Hence, the value of "sn" never changes, so the code gets stuck in the infinite loop.
-Corey
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.