home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / POLYFUN.DI$ / INTERP1.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  2.5 KB  |  89 lines

  1. function yi = interp1(x,y,xi,method)
  2. %INTERP1 1-D data interpolation (table lookup).
  3. %    YI = INTERP1(X,Y,XI) returns vector YI containing elements
  4. %       corresponding to the elements of XI and determined by interpolation
  5. %       within vectors X and Y.
  6. %
  7. %    Interpolation is the same operation as "table lookup".  Described in
  8. %    "table lookup" terms, the "table" is [X,Y] and INTERP1 "looks-up"
  9. %    the elements of XI in X, and, based upon their location, returns
  10. %    values YI interpolated within the elements of Y.
  11. %
  12. %    INTERP1 performs multiple output table lookup if Y is a matrix.
  13. %    If Y is a matrix with length(X) rows, and N columns, INTERP1
  14. %    returns a length(XI)-by-N matrix YI containing the multi-output 
  15. %    table lookup results.
  16. %
  17. %    By default, INTERP1 uses linear interpolation.
  18. %    YI = INTERP1(X,Y,XI,'method') specifies alternate methods.
  19. %    Available methods are:
  20. %
  21. %      'linear' - linear interpolation
  22. %      'spline' - cubic spline interpolation
  23. %      'cubic' - cubic interpolation
  24. %
  25. %    All the interpolation methods require that X be monotonic. The
  26. %    'cubic' method also requires that the points in X be equally-spaced.
  27. %
  28. %    For example, generate a coarse sine curve and interpolate over a
  29. %    finer abscissa:
  30. %                     x = 0:10; y = sin(x);
  31. %                     xi = 0:.25:10;
  32. %                     yi = interp1(x,y,xi);
  33. %                     plot(x,y,'o',xi,yi)
  34. %
  35. %    See also INTERPFT, INTERP2, GRIDDATA.
  36.  
  37. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  38.  
  39. error(nargchk(2,4,nargin));
  40.  
  41. % Trap call to old interp1, called interpft
  42. if nargin==2
  43.     disp('This usage of interp1(x,n) is obsolete and will be eliminated')
  44.     disp('in future versions. Please use interpft(x,n) instead.')
  45.     yi = interpft(x,y);
  46.     return
  47. end
  48.  
  49. % Check the arguments.
  50. if nargin<4,
  51.   method = 'linear';
  52. end
  53.  
  54. if ~isstr(method),
  55.   error('METHOD must be one of the strings: ''linear'',''spline'',''cubic''.');
  56. end
  57.  
  58. if min(size(x))>1, 
  59.   error('X must be a vector.'); 
  60. else
  61.   x = x(:);
  62. end
  63.  
  64. if min(size(y))==1, y = y(:); end % Make sure y is a vector.
  65.  
  66. [m,n] = size(y);
  67. if length(x)~=m,
  68.   error('X must have the same number of elements as the rows of Y.');
  69. end
  70.  
  71. if method(1)=='l', % Linear interpolation
  72.   yi = table1([x,y],xi);
  73.  
  74. elseif method(1)=='s', % Spline interpolation
  75.   xi = xi(:);
  76.   yi = zeros(length(xi),n);
  77.   for i=1:n,
  78.     yi(:,i) = spline(x,y(:,i),xi);
  79.   end
  80.  
  81. elseif method(1)=='c', % Cubic interpolation
  82.   yi = icubic(x,y,xi);
  83.  
  84. else
  85.   error([method,' is an invalid method.']);
  86.  
  87. end
  88.   
  89.