Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Newton's Method in MATLAB During this module, we are going to use Newton's method to compute the root(s) of the function f(x) = x' +
Newton's Method in MATLAB During this module, we are going to use Newton's method to compute the root(s) of the function f(x) = x' + 3x? 2x 4 Since we need an initial approximation ('guess') of each root to use in Newton's method, let's plot the function f(x) to see many roots there are, and approximately where they lie. Exercise 1 Use MATLAB to create a plot of the function f(x) that clearly shows the locations of its real roots, then save and upload the plot: Upload PNG here Show/hide hint You can do this in the Command Window. Make sure you initialise x as a vector. Try x = linspace(-4, 2, 100);. Then don't forget the dot-operator when writing f = and your function. You can save a figure in MATLAB, by clicking File > Save As on your figure. Make sure you save as a .png, and not a .fig! Download the following M-file, then open and view its contents. This M-file contains the function newton to help us get started. Be sure to read theE comments: newton.m (Right-click > Save link as...) newton.m E function x = newton(x) E %NEWTON Performs a single iteration of Newton's method. To use Newton's method to compute the root of f(x) with initial guess x1, e.g. if x1 is 10, first use the command (in the Command 3 4 Window): x = newton(10) to compute the next approximate root. Then further approximate roots are computed by repeatedly executing the command: 10 11 x = newton(x) 12 % Perform one iteration of Newton's method. n = 2; 14 % computed after 1 interation (not really needed until exercise 3) % xp is "x-previous" and contains x1 3D ; x = xp - f(xp)/fprime(xp); % x is the next x being computed, x2 16 17 % Exercise 3 add while loop here 20 % alternative to 'disp' function - keeps things neat! fprintf('Newton approximates root at %.15f, after %d iteration(s) ',x,n-1); fprintf('f(x) at approximate root x is %.6f ',f(x)); fprintf('|x-xp| is %.6f',abs (x-xp)); % End of function. 21 22 24 end 26 27 % To change the function f(x), edit this function. O function y = f(x) y = x^3 + 3*x^2 - 2*x - 4; end % To change the function f'(x), edit this function. O function yprime = fprime(x) yprime = 3*X^2 + 6*x - 2; end Exercise 2 Your plot from Exercise 1 should show that f(x) has a root near x = 1.23. Use the function newton with initial guess x1 = 10, to compute an approximation of this root by computing successive approximate roots 10 is true. 2. n should be incremented by 1 every loop. 3. Don't forget to re-calculate xp and x every loop! Check that your while loop is working correctly. Type >> newton (10); into the Command Window and check if it outputs: Newton approximates root at 1.236067979714056, after 8 iteration(s) f(x) at approximate root x is 0.000000 x-xp is 0.000057 Use the following prompt to upload newton.m: No file selected. Browse... Upload your M-file 0% Newton's method is not a fail-safe way to find roots, since the sequence of approximations may not converge for a 'bad' initial guess. This is a downside of Newton's method (compared to the Bisection method, which will always converge for valid initial values). For this reason, it is worthwhile to enforce a maximum number of iterations. If a 'bad' guess is made, this is stop the while loop from looping forever. For this, we need to insert a counter into the while loop to keep track of how many iterations have passed. Exercise 4 Add an extra condition to your while loop, so that the loop will exit once n is greater than 101. Hint: Use & &. Show/hide hint How many iterations does it take to find the approximate root, for initial guess x1 = 1? Enter iterations Sometimes we want to visualise how quickly Newton's method is converging (or not converging), we can visualise the convergence by plotting the value of xn versus n. Exercise 5 To plot the value of the approximate root, x, during each iteration, carefully follow the 4 steps below: 1. Add close all; hold on; plot(1, xp, 'r^'); before your while loop. 2. Add plot (n, x, 'ro'); as the first command inside your while loop. 3. Add your final approximate root to the plot by adding plot (n, x, 'r*'); after the end of your while loop. 4. Add appropriate title, and x- and y-axis labels. 5. Add print('convergence.png','-dpng') to the end of your newton function to automatically save your figure as 'convergence.png'. and upload your graph newton (10), below Now run ('convergence.png'). Exercise 6 For which of the following initial guesses, x1, use your function newton to find if Newton's method converges to a root (and the root), diverges, or oscillates? X1 = -3 Converges to root Enter root Diverges Oscillates [- = Ix Converges to root Enter root Diverges Oscillates X1 = 0 Converges to root Enter root Diverges Oscillates
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started