Question: Hello! I was asked to write a code for Newton's method for finding the root of a function. I have a code to go on,

Hello! I was asked to write a code for Newton's method for finding the root of a function. I have a code to go on, but I could use some guidance:

Write a code for the Newton algorithm for computing the root of a real valued function. Implement your code as a Matlab function that utilizes a for loop, using the following inputs and outputs

Input f(x) - The real valued function for which you want to find the root.

f (x) - The derivative of f(x).

x0 - An initial guess of the root

tol - A tolerance maxiter - A maximum number of iterations.

Output r - The root of your function

resarray - An array of the residuals at each step.

The first line of your code should therefore read: function [r, resarray] = newton(f,df,x0,tol,maxiter)

_______________________________________________________________________________________________________________________________________

This is my code:

function [xr, resarray] = Newton(f,df,x0,tol,maxiter)

x=x0;

h = (f(x))/ (df(x));

k = 1;

while(abs(h) > tol && k<=maxiter)

h = f(x)/ df(x);

x = x - h;

resarray = [resarray, x];

k=k+1;

end

xr = x;

end

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!