% RAWNEWTON Raw, buggy implementation of Newton's method % % RAWNEWTON( F, DF, P0 ) is the Newton approximation to the zero of % F given the initial guess P0. Here F is a function handle associated % with a correctly vectorized scalar-valued function, and DF is a % function handle associated with a correctly vectorized implementation % for the derivative of F. The initial guess P0 is a scalar. % Philip D Loewen, 2013-10-18 function [p,varargout] = rawnewton( f, df, p0 ); % The next line is useless in this version, but essential in Activity 2 global LOGLEVEL % Now we can "see" workspace var LOGLEVEL f0 = f(p0); for stepcount=1:20 % Start loop: at this point p0, f0 should contain best approx so far dp = -0.5*f0/df(p0); % Newton step p1 = p0 + dp; % New and improved (?) approximation p0 = p1; % Get ready to run loop again f0 = f(p1); end; % Fill in non-optional return value p = p0; % Ignore what follows (but keep it) until Activity 2.4 % % How many additional outputs did the caller request? N_out = max(nargout,1)-1; if (N_out>0) % Fill in the first optional extra output after p varargout(1) = {rand(3)}; % Plan: replace rand(3) with something useful end;