function c = newton(x0, delta) %% %% newton.m %% %% Implements Newton's method %% %% Input: x0 initial guess for the root of f %% delta the tolerance/accuracy we desire %% (Code runs until |f(c)| <= delta.) %% %% Output: c the approxmiation to the root of f %% %% Syntax: newton(x0, delta) %% format long e c = x0; fc = f(x0); fprintf('initial guess: c=%d, fc=%d\n',c,fc) if abs(fc) <= delta %% check to see if initial guess satisfies return; %% convergence criterion. end; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% %% %% main routine %% %% %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% while abs(fc) > delta, fpc = fprime(c); if fpc==0, %% if fprime is 0, abort. error('fprime is 0') %% the error function prints message and exits end; c = c - fc/fpc; %% Newton step fc = f(c); fprintf(' c=%d, fc=%d\n',c,fc) end; %% %% put subroutines here %% %% function fx = f(x) fx = (5-x)*exp(x) - 5; %% Enter your function here. return; function fprimex = fprime(x) fprimex = (5-x)*exp(x) - exp(x); %% Enter the derivative %% of your function here. return;