Please decode this question for me. I have been trying to undestand the question and tried to write a code to do what it is asking but my answers are not matching with the question's answers.
Question as follows:
- For a given a>0, the following relationship will determine the positive value of the a to within sqrt(a) tolerance t(0) for any starting value (guess) x(0) > 0:
x_n+1 = 1/2*(x_n + a/x_n) ; n = 0,1,2, ...
(When x_0<0 the negative root is found.) Write a script that determines sqrt(a) to within abs(x_n - x_n+1) < 10^-6 for a = 7. How many iterations does it take if (a) x_0 = 3 and (b) x_0 = 100? The first iteration is the determination of x 1 . [ Hint: Notice that the above relationship is not an explicit function of n. Here, the subscript n is simply an indicator that the next (new) value x n+1 is a function of the previous (old) value x n . Thus, each time through the loop, the old and new values keep changing. Therefore, one has to keep track of n to record the number of times this relationship is used until the convergence criterion is met.] [Answer: (a) n iterations = 4 and (b) n iterations = 10.] Write and save the script as an M-file. Then display the script in the notebook with the "type" command. Then execute the script in the notebook.
Code I created:
a=7; niteration=0; x=input('Type in a number : ') while ans < 10^(-6) ans = 0.5*(x+(sqrt(a)/x)); x=x+1; ninteration=ninteration+1; end fprintf('Number of iteration = %3.0f ',niteration)
I greatly appreciate it if someone can decode, check my work, and give pointers what I need to do.
Thank you very much.