Question: Implement Newton's method as a function: function , tol ) Note: The derivative will be computed automatically within myNewton.m so that you do not need

Implement Newton's method as a function:
function , tol)
Note: The derivative will be computed automatically within myNewton.m so that you do not need to input the derivative of f.
As a class convention, we say that the initial guess counts as 1 iteration. So, in a sense, n=2 would mean we took one step of Newton's method.
Debugging tips:
If tests 1 and 7 are the only tests failed, and the suggested problem in the "code to call function" is close but not quite correct, then it is likely that you stopped correctly, but did not report the most recent iteration.
are not using the initial condition x0 to start iterating in addition to the suggestion made by Matlab of using ";".
Function ?
function [p,n]= myNewton(f,x1,tol)
%input f, anonymous function for root finding problem
%input x1, an initial guess
%input tol, a tolerance (method will stop when successive iterates are within tol of each other)
%output p, a root f(p) approx 0
%output n, the number of iterations to reach p, or the number n when p = x_n
% Compute the derivative and save it as an anonymous function
xsym = sym('x');
fsym = f(xsym);
fprimesym = diff(fsym,xsym);
fprime = matlabFunction(fprimesym,'vars',xsym);
Code to call your function ?
%As a test, the root of f(x)= x+2^x
% define f, an initial guess (use 1), and use a tolerance of 1e-6
% send these into the function and store the result as the variable p
% the method will converge in 6 iterations
format long % to display more digits
Implement Newton's method as a function: function

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 Programming Questions!