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

  1. function z = trapz(x,y)
  2. %TRAPZ    Trapezoidal numerical integration.
  3. %    Z = TRAPZ(X,Y) computes the integral of Y with respect to X using
  4. %    trapezoidal integration.  X and Y must be vectors of the same length,
  5. %    or X must be a column vector and Y a matrix with as many rows as X.
  6. %    TRAPZ computes the integral of each column of Y separately.
  7. %    The resulting Z is a scalar or a row vector.
  8. %
  9. %    Z = TRAPZ(Y) computes the trapezoidal integral of Y assuming unit
  10. %    spacing between the data points.  To compute the integral for
  11. %    spacing different from one, multiply Z by the spacing increment.
  12. %
  13. %    See also SUM, CUMSUM.
  14.  
  15. %     Clay M. Thompson, 10/16/90; Cleve Moler, 1/19/92.
  16. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  17.  
  18. %   Make sure x and y are column vectors, or y is a matrix.
  19. if nargin < 2, y = x; end
  20. [m,n] = size(y);
  21. if m == 1, y = y(:); m = n; end
  22. if nargin < 2, x = 1:m; end
  23. x = x(:);
  24. if length(x) ~= m
  25.     error('Input arguments must be the same length.');
  26. end
  27.  
  28. %   Trapezoid sum computed with vector-matrix multiply.
  29.  
  30. z = diff(x)' * (y(1:m-1,:) + y(2:m,:))/2;
  31.