comparison scripts/general/trapz.m @ 11587:c792872f8942

all script files: untabify and strip trailing whitespace
author John W. Eaton <jwe@octave.org>
date Thu, 20 Jan 2011 17:35:29 -0500
parents fd0a3ac60b0e
children 16cca721117b
comparison
equal deleted inserted replaced
11586:12df7854fa7c 11587:c792872f8942
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{z} =} trapz (@var{y}) 20 ## @deftypefn {Function File} {@var{z} =} trapz (@var{y})
21 ## @deftypefnx {Function File} {@var{z} =} trapz (@var{x}, @var{y}) 21 ## @deftypefnx {Function File} {@var{z} =} trapz (@var{x}, @var{y})
22 ## @deftypefnx {Function File} {@var{z} =} trapz (@dots{}, @var{dim}) 22 ## @deftypefnx {Function File} {@var{z} =} trapz (@dots{}, @var{dim})
23 ## 23 ##
24 ## Numerical integration using trapezoidal method. @code{trapz 24 ## Numerical integration using trapezoidal method. @code{trapz
25 ## (@var{y})} computes the integral of the @var{y} along the first 25 ## (@var{y})} computes the integral of the @var{y} along the first
26 ## non-singleton dimension. If the argument @var{x} is omitted a 26 ## non-singleton dimension. If the argument @var{x} is omitted a
27 ## equally spaced vector is assumed. @code{trapz (@var{x}, @var{y})} 27 ## equally spaced vector is assumed. @code{trapz (@var{x}, @var{y})}
28 ## evaluates the integral with respect to @var{x}. 28 ## evaluates the integral with respect to @var{x}.
29 ## 29 ##
30 ## @seealso{cumtrapz} 30 ## @seealso{cumtrapz}
31 ## @end deftypefn 31 ## @end deftypefn
32 32
33 ## Author: Kai Habel <kai.habel@gmx.de> 33 ## Author: Kai Habel <kai.habel@gmx.de>
34 ## 34 ##
35 ## also: June 2000 - Paul Kienzle (fixes,suggestions) 35 ## also: June 2000 - Paul Kienzle (fixes,suggestions)
36 ## 2006-05-12 David Bateman - Modified for NDArrays 36 ## 2006-05-12 David Bateman - Modified for NDArrays
37 37
38 function z = trapz (x, y, dim) 38 function z = trapz (x, y, dim)
39 39
40 if (nargin < 1) || (nargin > 3) 40 if (nargin < 1) || (nargin > 3)
41 print_usage (); 41 print_usage ();
42 endif 42 endif
43 43
44 nd = ndims (x); 44 nd = ndims (x);
69 dim = floor (dim); 69 dim = floor (dim);
70 if (dim < 1 || dim > nd) 70 if (dim < 1 || dim > nd)
71 error ("trapz: invalid dimension DIM along which to sort"); 71 error ("trapz: invalid dimension DIM along which to sort");
72 endif 72 endif
73 endif 73 endif
74 74
75 n = sz(dim); 75 n = sz(dim);
76 idx1 = cell (); 76 idx1 = cell ();
77 for i = 1:nd 77 for i = 1:nd
78 idx1{i} = 1:sz(i); 78 idx1{i} = 1:sz(i);
79 endfor 79 endfor
80 idx2 = idx1; 80 idx2 = idx1;
81 idx1{dim} = 2 : n; 81 idx1{dim} = 2 : n;
82 idx2{dim} = 1 : (n - 1); 82 idx2{dim} = 1 : (n - 1);
83 83
84 if (! have_x) 84 if (! have_x)
85 z = 0.5 * sum (x(idx1{:}) + x(idx2{:}), dim); 85 z = 0.5 * sum (x(idx1{:}) + x(idx2{:}), dim);
86 else 86 else
87 if (! size_equal (x, y)) 87 if (! size_equal (x, y))
88 error ("trapz: X and Y must have same shape"); 88 error ("trapz: X and Y must have same shape");
89 endif 89 endif
90 z = 0.5 * sum ((x(idx1{:}) - x(idx2{:})) .* 90 z = 0.5 * sum ((x(idx1{:}) - x(idx2{:})) .*
91 (y(idx1{:}) + y(idx2{:})), dim); 91 (y(idx1{:}) + y(idx2{:})), dim);
92 endif 92 endif
93 endfunction 93 endfunction
94 94
95 %!assert (trapz(1:5), 12) 95 %!assert (trapz(1:5), 12)