% Script file to show how to use bisection and Newton's method % (all in one script file). The commands are the next two lines- % be sure split out and save the functions as their own m-files % (like bisect.m, NewtonMethod.m and testfunc01.m ) xb=bisect(@testfunc01,0,1,1e-9) xc=NewtonMethod(@testfunc01,0,20,1e-9) % Functions defined below: function [y,dy]=testfunc01(x) % Test function e^x-3x y=exp(x)-3*x; % Output y dy=exp(x)-3; % and dy end function out=NewtonMethod(F,x0,numits,tol) % Inputs to Newton's Method: % F- Name of a function that returns BOTH % values of g(x) and g'(x) % x0= Initial value of x. % numits= Maximum number of iterations % tol = Tolerance (like 1e-6) measures % change between iterates of x % % Example: % function [y,dy]=F(x) % y=x^2 - x - 1; % dy=2*x-1 % % out=NewtonMethod(F,1,10,1e-8) % for k=1:numits [g,dg]=feval(F,x0); if dg==0 error('Derivative is zero'); end xnew=x0-g/dg; d=abs(xnew-x0); if d