Search 72,405 tutors FIND TUTORS
Search for tutors
Ask a question
0 0

matlab question

My 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;
snvec(n-1) = sn;
invec(n-1) = in;
n = n+1


and then the function is called:


Tutors, please sign in to answer this question.

1 Answer

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.