Question: This is a matlab question. Please include code so I can learn! Thank You! 6. (5 points) We saw in the last problem that Newtons

This is a matlab question. Please include code so I can learn! Thank You!

6. (5 points) We saw in the last problem that Newtons method will work for complex roots if we start with a complex initial guess. Its true. The simple polynomial P(x) = x^ 3 1 has three roots: 1, (1i sqrt(3))/ 2 , and (1+i sqrt (3))/ 2 (you can check this with the roots command).

Test your Newtons method with tol = 10^7 on this problem. With an initial guess of p0 = 0.25 + 0.5i, the method should converge to 0.500000 0.866025i.

What other initial guesses will converge to this root? What initial guesses will converge to the other roots? Lets make a plot. In a script file, set up arrays

x = linspace(1,1,100);

y = x;

These arrays represent a 100 100 grid on 1 x, y 1. Use a nested for loop to call your Newtons method for each point in the 100 100 grid with x + yi as the initial guess. Remember not to use i as an index, since we are using it for sqrt( 1).

If Newtons method starting at x + yi converges to 1, plot a red dot at (x, y) using a plot(x(j),y(k),r*) command.

If it converges to (1i sqrt(3))/ 2 , plot a blue dot.

If it converges to (1+i sqrt (3))/ 2 , plot a green dot.

Use a hold on command to catch all the dots. The resulting plot is called the basin of attraction for this problem. Turn it in.

Warning! When checking for equality of real numbers, it is dangerous to use ==. Since your Newtons method code will probably not converge exactly to 1, we dont want to use:

if p == 1 % check if it is the root 1

plot(x(j),y(k),'r*'); % if so, plot a red dot

We get around this by instead checking to see if the root output by our Newton code is close to 1:

if abs(p1) < 1e4 % check if p is close to 1

plot(x(j),y(k),'r*'); % if so, plot a red dot

BELOW I WILL POST MY CODE FOR THE NEWTONS METHOD< THIS CAN BE USED FOR THE GRAPH

tol=.0000001

nmax=100

function [ x, ex ] = newton( f, df, x0, tol, nmax ) % % NEWTON Newton's Method % Newton's method for finding successively better approximations to the % zeroes of a real-valued function. % % Input: % f - input funtion % df - derived input function % x0 - inicial aproximation % tol - tolerance % nmax - maximum number of iterations % % Output: % x - aproximation to root % ex - error estimate % % Example: % [ x, ex ] = newton( 'exp(x)+x', 'exp(x)+1', 0, 0.5*10^-5, 10 ) if nargin == 3 tol = 1e-4; nmax = 1e1; elseif nargin == 4 nmax = 1e1; elseif nargin ~= 5 error('newton: invalid input parameters'); end f = inline(f); df = inline(df); x(1) = x0 - (f(x0)/df(x0)); ex(1) = abs(x(1)-x0); k = 2; while (ex(k-1) >= tol) && (k <= nmax) x(k) = x(k-1) - (f(x(k-1))/df(x(k-1))); ex(k) = abs(x(k)-x(k-1)); k = k+1; end end

just copy and paste code if possible! Thank You!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!