Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function with the header: function [R] = myNewton1 (f, x0, to1) which ta

ID: 3817682 • Letter: W

Question

Write a function with the header: function [R] = myNewton1 (f, x0, to1) which takes as input f: a function handle x0: the initial guess of the root to1: a tolerance above which the algorithm will keep iterating. Tips: The code should calculate the value of the derivative of the function numerically. You may use your "myPartialDeriv" or implement a finite difference approximation. Be sure to include an iteration counter which will stop the while-loop if the number of iterations get greater than 100. It is not necessary to print out a convergence table within the while loop. (i.e., there should be no fprintf statements in your code) Test Case: >> format longg >> f = @(x) 2* (1-cos (x)) + 4* (1- squareroot (1-(0.5*sin (x)).^2)) - 1.2; >> [root] = myNewton1 (f, 1, le-8) root = 0.95 8192178746275

Explanation / Answer

function [R] =myNewton1(f,x0, tol)

% Newton's Method

% input:

% f: a funtion handle

% x0:the initial guess of the root

% tol : a tolreance

% Output: R= the root of the function

format long

if nargin < 3, xtol =5*eps; end

if nargin < 4, ftol = 5*eps; end

xeps = max(xtol, 5*eps);

feps = max(ftol, 5*eps); %smallest tol are 5*eps

xref = abs(x0(2)-x0(1);

fa = feval(f,x0(1));

fb = feaval(f,x0(2));

fref =max([abs(fa) abs(fb)]);

x=x0(1) +0.5*(x0(2)-x0(1));

n=0; maxit =100;

while n<=maxit %maximum iteration for n=100

n=n+!;

[f,dfdx] = feval(f,x);

dx = f/dfdx;

x = x- dx;

end;

if (abs(f/fref) < feps ) | (abs (dx/xref) <xeps) ,

R=x;

return; end;

Test Case:

format long

f= @ (X) 2*(1-cos(x)) + 4 * (1-sqrt (1-(0.5 * sin(x)) ^ 2))-1.2;

[R] - myNewton1(f,1, 1e-8)

output:

R = 0.958192178746275

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote