Newton. m
% Program 2.5 (Newton-Raphson iteration) function [P0, err, K, y] = Newton (F, DF, P0, Delta, Epsilon, max1) % input-F is the object function input as a string 'F' %-DF is the derivative of F input as a string 'df '%-P0 is the initial approximation to a zero of F %-delta is the tolerance for P0 %-Epsilon is the tolerance for the function values Y %-max1 is the maximum number of iterations % output-P0 is the Newton-Raphson Approximation to the zero %-err is the error estimate for P0 %-k is the number of iterations %-Y is the function value F (P0) for k = 1: max1 p1 = p0-feval (F, P0)/feval (DF, P0); err = ABS (p1-p0); relerr = 2 * err/(ABS (P1) + delta); p0 = p1; y = feval (F, P0); If (ERR <delta) | (relerr <delta) | (ABS (y) <epsilon ), break, endend
Fun. m
Function f = fun (x) F = x ^ 2-91; derivative. mfunction df = derivative (x) df = 2 * X;
Untitled4.m
P0 = 9.5; Delta = 10e-10; Epsilon = 10e-10; max1 = 6; F = 'fun '; df = 'derivative'; [P0, err, K, y] = Newton (F, DF, P0, Delta, Epsilon, max1)