home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_2 / approot.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  548 b   |  24 lines  |  [MATF/MATL]

  1. function  R = approot(X,Y,epsilon)
  2. % R = approot(X,Y,epsilon)
  3. % X is the vector of abscissas, input.
  4. % Y is the vector of ordinates, input.
  5. % epsilon is the tolerance, input.
  6. % R is the vector of approximate locations for roots, output.
  7. yrange   = max(Y)-min(Y);
  8. epsilon2 = yrange*epsilon;
  9. n = length(X);
  10. m = 0;
  11. X(n+1) = X(n);
  12. Y(n+1) = Y(n);
  13. for k=2:n,
  14.   if  Y(k-1)*Y(k) <= 0,
  15.     m = m + 1;
  16.     R(m) = (X(k-1)+X(k))/2;
  17.   end
  18.   s = (Y(k) - Y(k-1))*(Y(k+1) - Y(k));
  19.   if  (abs(Y(k)) < epsilon2) & (s <= 0),
  20.     m = m + 1;
  21.     R(m) = X(k);
  22.   end
  23. end
  24.