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

  1. function y = table1(tab,x0)
  2. %TABLE1    Table look-up.
  3. %    Y = TABLE1(TAB,X0) returns a table of linearly interpolated rows from
  4. %    table TAB, looking up X0 in the first column of TAB.
  5. %
  6. %    See also INTERP2, TABLE2.
  7.  
  8. %    NOTE:  TAB's 1st column is checked for monotonicity.
  9. %    Different algorithms are used depending on whether X0 is scalar
  10. %    so that vectorization can be exploited in the case of vector X0.
  11. %    It is an error to request a value outside the range of the first
  12. %    column of TAB for X0.
  13.  
  14. %    Tomas Schoenthal 5-1-85
  15. %    Egbert Kankeleit 1-15-87
  16. %    Revised by L. Shure 2-3-87
  17. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  18.  
  19. if (nargin ~= 2), error('Wrong number of input arguments.'), end
  20.  
  21. [m,n]=size(tab)  ;  k0=max(size(x0))  ;
  22.  
  23. if k0==1
  24.    dx=tab(2:m,1)-tab(1:m-1,1);
  25.    sig=sign(dx(1,1));
  26.    if any(sign(dx(:))-sig);
  27.    error('First column of the table must be monotonic.')
  28.    end
  29.    if sig > 0
  30.         ii = find(tab(:,1) >= x0);
  31.         if size(ii)==0; error('x0 larger than all values in first column'),end
  32.         if x0 < tab(1,1)
  33.            error('x0 smaller than all values in first column') 
  34.         end
  35.    else
  36.    ii = find(tab(:,1) <= x0);
  37.         if size(ii)==0;
  38.            error('x0 smaller than all values in first column')
  39.         end
  40.         if x0 > tab(1,1)
  41.            error('x0 larger than all values in first column')
  42.         end
  43.    end
  44.    kk=ii(1);
  45.    xhigh = tab(kk,1);
  46.    if kk > 1 
  47.       xlow = tab(kk-1,1);
  48.    else
  49.       xlow = xhigh;
  50.    end
  51.    if xlow == xhigh
  52.       y = tab(kk,:);
  53.    else
  54.       alpha = (xhigh-x0)/(xhigh-xlow);
  55.       beta = 1. - alpha;
  56.       y = alpha*tab(kk-1,:) + beta*tab(kk,:);
  57.    end
  58.    y = y(2:n);
  59.    return
  60. else
  61.    dx=tab(2:m,:)-tab(1:m-1,:);
  62.    dx=[dx;dx(m-1,:)];sig=sign(dx(1,1));
  63.    if any(sign(dx(:,1))-sig);
  64.    error('First column of the table must be monotonic.')
  65.    end
  66.    y=zeros(k0,n-1);
  67.    for k=1:k0
  68.      if sig>0; ii=max(find(tab(:,1) <= x0(k) ));
  69.         if size(ii)==0; error('x0 smaller than all values in first column'),end
  70.         if x0(k) > tab(m,1)
  71.            error('x0 larger than all values in first column') 
  72.         end
  73.      else ; ii=max(find(tab(:,1) >= x0(k) ));
  74.         if size(ii)==0;
  75.            error('x0 larger than all values in first column')
  76.         end
  77.         if x0(k) < tab(m,1)
  78.            error('x0 smaller than all values in first column')
  79.         end
  80.      end
  81.      y(k,:)=tab(ii,2:n)+dx(ii,2:n)*(x0(k)-tab(ii,1))/dx(ii,1);
  82.    end
  83. end
  84.