function x=newton(Fs,FPs,P0,y,P,TOL); % NEWTON-RAPHSON ALGORITHM 2.3 % % To find a solution to f(x) = 0 given an % initial approximation p0: % % y,P can be used for additional info if the function % requires it. % % Initially written by Burden and Faires, revised by % B.A. Broughton, 10/01/1999 % % INPUT: initial approximation p0; tolerance TOL; % maximum number of iterations NO. % % OUTPUT: approximate solution p or a message of failure TRUE = 1; FALSE = 0; F = inline(Fs,'x','P','y'); FP = inline(FPs,'x','P'); NO=150; OUP = 1; F0 = F(P0,P,y); % STEP 1 I = 1; OK = TRUE; % STEP 2 while I <= NO & OK == TRUE % STEP 3 % compute P(I) FP0 = FP(P0,P); D = F0/FP0; % STEP 6 P0 = P0 - D; F0 = F(P0,P,y); % STEP 4 if abs(D) < TOL % procedure completed successfully % fprintf(OUP,'\nApproximate solution = %.10e\n',P0); % fprintf(OUP,'with F(P) = %.10e\n',F0); % fprintf(OUP,'Number of iterations = %d\n',I); % fprintf(OUP,'Tolerance = %.10e\n',TOL); OK = FALSE; % STEP 5 else I = I+1; end end if OK == TRUE % STEP 7 % procedure completed unsuccessfully fprintf(OUP,'\nIteration number %d',NO); fprintf(OUP,' gave approximation %.10e\n',P0); fprintf(OUP,'with F(P) = %.10e not within tolerance %.10e\n',F0,TOL); end x = P0;