Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to fix this code and make it work to solve x0=-2? Write a MATLAB function, called Newtons_method that inputs a function, f, its derivative

image text in transcribed

image text in transcribed

image text in transcribedHow to fix this code and make it work to solve x0=-2?

Write a MATLAB function, called Newtons_method that inputs a function, f, its derivative f', an initial guess x_@, an error tolerance, tol, and a maximum number of iterations, N, and outputs the root of f obtained using Newton's method (denoted by c), starting with x_0. Your function should have an error defined by err = {x_n-x_{n-1}}\, and stop when the error is less than the tolerance, or if the number of iterations exceeds N - whichever happens first. Your function header should look something like: function [C, n,err] = Newtons_method(f, fp, x0, tol,N) n is the last iteration when you stop. Use the function you created to find the root of the equation arctan(x)=1 with initial guess x_0 = 2, to an accuracy of less than tol = 10^(-8). Did your method converge, and if so, how many iterations did it take? If not, why it did not converge, and what happened -- did it diverge, or end up in an infinite loop? Plot on the same graph the function and the axis y = 0. Test with x = -2. What happens? Script C Reset MATLAB Documentation i f=@(x) atan(x); 2 X=-1:0.01:1; 3 y=f(x); 4 plot(x,y,X,zeros(1, length(x))); 5 [C, n, err]=Newtons_method (@(x) atan(x),@(x) 1/(x^2 + 1),2,1e-8,100) [C, n, err]=Newtons_method (@(x) atan(x),@(x) 1/(x^2 + 1),-2, 18-8, 100) 7 function [c, n,err]=Newtons_method(f, fp,xo, tol,N) x1=x0; x0=x0 -1; n=0; C=0; 12 err=[]; while(abs(x1 - x0)>tol) Script Reset MATLAB Documentation i f=@(x) atan(x); 2 X=-1:0.01:1; 3 y=f(x); 4 plot(x,y,X, zeros(1, length(x)); [C, n, err) -Newtons_method (@(x) atan(x),@(x) 1/(x^2 + 1), 2, 18-8, 100) 6 [C, n, err] -Newtons_method (@(x) atan(x),@(x) 1/(x^2 + 1), -2,1e-8, 100) function [c, n, err]=Newtons_method(f, fp, xe, tol,N) 8 x1=X0; x0=X0-1; n=0; 11 C=0; 12 err=[]; while(abs(x1 - x0)>tol) 14 x0=X1; x1=x0-f(x0)/fp(x0); n=n+1; 17 if(n==N) err=[]; C=[]; break; end end C=XO; err=abs(x1-xo); end 18 21 Previous Assessment: 1 of 2 Tests Passed (50%) Submit Respect guidelines 50% (50%) 0% (50%) Test solution for x0 = 2 Variable c has an incorrect value Total: 50%

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