function [Nx,iter]=newton1(xinit,maxiters) %FUNCTION Nx=newton1(xinit,maxiters) % %This is a sample function to perform Newton's Method. %The functions are defined within this program. % Input: xinit = initial guess % maxiters= maximum times to iterate % Output: Nx = approximate solution % iter= the iteration at which we stopped. % % Sample useage: [x,iter]=newton1(0.6,500); % % This is a sample program; if we were doing this % for real we'd want better stopping criteria and % a different method of calling F and dF. xinit=0.3; x=xinit; for i=1:maxiters %100 is the max number of iterations we'll try y=F(x); dy=dF(x); if abs(dy)>0.00001 Nx=x-(F(x)/dF(x)); x=Nx; else iter=i; break end end iter=maxiters; function y=F(x) y=cos(x)-x; return function dy=dF(x) dy=-sin(x)-1; return