Newton Raphson method in Matlab?

1 view (last 30 days)
Tyler Silva
Tyler Silva on 25 Feb 2016
Commented: Walter Roberson on 25 Feb 2016
Can i get the basics on how to do this section in matlab?
Write a MATLAB script that utilizes the Newton Raphson algorithm to search for the fifth root of any number entered by the user to within four places behind the decimal point (i.e., │f(xn)│ < 0.0001). The script should do the following:
Prompt the user for the number to find the 5th root of
Prompt the user for an initial guess
Use a while loop to iterate through the Newton Raphson algorithm as long as the required accuracy hasn’t been reached (i.e., abs(f(xn)) > 0.0001) and the number of iterations is below 100. Save all of the estimates in a vector.
Within the while loop, before updating the estimate, check to see if f’(xn)is zero. If it is, modify the value of xn just a bit to avoid division by zero in the Newton Raphson update equation. For example, you could just add a small value (xn = xn + 0.01) before the update whenever the derivative at xn is zero.
After the end of the while loop, add an fprintf statement to display the number of iterations performed.
If the number of iterations is less than 100, use an fprintf statement to output the final estimate (with five places behind the decimal point) for the 5th root of the number entered by the user. Otherwise, tell the user that the algorithm failed to converge and a better initial guess should be used.
Plot each of the estimates on the y-axis. On the x-axis, just put 0:N where N is the number of iterations. Include axis labels and a title.
This is what I have so far. I am totally confused on this section, but Im trying to get as close as I can. Could I please get some help?
x_guess = input('Please enter your guess');
x_old = 100;
iterations = 0;
while abs(x_old-x) > 10^-3 && x ~= 0
x_old = x;
x = x - (my equation will go here);
iterations = iterations + 1;
fprintf('Iteration %d: x=%.20f, err=%.20f\n', iter, x, x_true-x);
pause;
end
  1 Comment
Walter Roberson
Walter Roberson on 25 Feb 2016
You missed the very first part, the prompt of the number to find the 5th root of.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!