Answered step by step
Verified Expert Solution
Link Copied!

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 +

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
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 + 3x2 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 the comments: newton.m (Right click > Save link as...) newton. m x + 1 function X = newtonix) WEWTON Performs a single iteration of Newton's method. To use Newton's ethod to compute the root off with initial 2 newton.m (Right-click > Save link as...) newton. m x + function x = newton(x) 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 Window): x = newton (10) to compute the next approximate root. Then further approximate roots are computed by repeatedly executing the command: x = newton(x) 16 - Perform one iteration of Newton's method. n = 2; computed after 1 interation (not really needed until exe! xp = x; % xp is "x-previous" and contains x1 X = xp - f(xp)/fprime(xp); x is the next x being computed, X2 Exercise 3 add while loop here alternative to 'disp' function - keeps things neat! fprintf("Newton approximates root at *.15f, after ad iter fprintf('f(x) at approximate root xis 1.6f ', f(x)); fprintf("\x-xplis 1.6f', abs(x-xp)); End of function. end 25 26 - 29 38 - 31 - To change the function f(x), edit this function. function y = f(x) y = x^3 + 3*x^2 - 2x - 4; end 34 35 - 36 - To change the function f'(x), edit this function. 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 X2, X3, X4, ... until xn - Xn-11 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-xplis 0.000057 Use the following prompt to upload newton.m: Browse... No file selected. 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). ULLII CUB of iterations. If a 'bad guess is made, this is stop the while loop Tom 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 Here is a very simple example of having multiple conditions for a while loop. a = 17 b = 1; while a

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions