Code covered by the BSD License  

Highlights from
Numerical Methods Using MATLAB, 3e

image thumbnail

Numerical Methods Using MATLAB, 3e

by

 

20 Aug 2002 (Updated )

Companion Software

approot (X,epsilon)
function R = approot (X,epsilon)

% Input - f is object function saved as an M-file named f.m
%       - X is the vector of abscissas
%       - epsilon is the tolerance 
% Output - R is the vector of approximate locations for roots

% NUMERICAL METHODS: MATLAB Programs
%(c) 1999 by John H. Mathews and Kurtis D. Fink
%To accompany the textbook:
%NUMERICAL METHODS Using MATLAB,
%by John H. Mathews and Kurtis D. Fink
%ISBN 0-13-270042-5, (c) 1999
%PRENTICE HALL, INC.
%Upper Saddle River, NJ 07458

Y=f(X);
yrange = max(Y)-min(Y);
epsilon2 = yrange*epsilon;
n=length(X);
m=0;
X(n+1)=X(n);
Y(n+1)=Y(n);

for k=2:n,
	if Y(k-1)*Y(k) <= 0,
		m=m+1;
		R(m)=(X(k-1)+X(k))/2;
	end
	s=(Y(k)-Y(k-1))*(Y(k+1)-Y(k));
	if (abs(Y(k)) < epsilon2) & (s <= 0),
		m=m+1;
		R(m)=X(k);
	end
end

Contact us