comparison scripts/general/trapz.m @ 10549:95c3e38098bf

Untabify .m scripts
author Rik <code@nomad.inbox5.com>
date Fri, 23 Apr 2010 11:28:50 -0700
parents 1bf0ce0930be
children 35adf2a71f3f
comparison
equal deleted inserted replaced
10548:479536c5bb10 10549:95c3e38098bf
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 40
41 if (nargin < 1) || (nargin > 3) 41 if (nargin < 1) || (nargin > 3)
42 print_usage (); 42 print_usage ();
43 endif 43 endif
44 44
81 idx1 = cell (); 81 idx1 = cell ();
82 for i = 1:nd 82 for i = 1:nd
83 idx1{i} = 1:sz(i); 83 idx1{i} = 1:sz(i);
84 endfor 84 endfor
85 idx2 = idx1; 85 idx2 = idx1;
86 idx1{dim} = 2 : n; 86 idx1{dim} = 2 : n;
87 idx2{dim} = 1 : (n - 1); 87 idx2{dim} = 1 : (n - 1);
88 88
89 if (! have_x) 89 if (! have_x)
90 z = 0.5 * sum (x(idx1{:}) + x(idx2{:}), dim); 90 z = 0.5 * sum (x(idx1{:}) + x(idx2{:}), dim);
91 else 91 else
92 if (! size_equal (x, y)) 92 if (! size_equal (x, y))
93 error ("trapz: x and y must have same shape"); 93 error ("trapz: x and y must have same shape");
94 endif 94 endif
95 z = 0.5 * sum ((x(idx1{:}) - x(idx2{:})) .* 95 z = 0.5 * sum ((x(idx1{:}) - x(idx2{:})) .*
96 (y(idx1{:}) + y(idx2{:})), dim); 96 (y(idx1{:}) + y(idx2{:})), dim);
97 endif 97 endif
98 endfunction 98 endfunction
99 99
100 %!assert (trapz(1:5), 12) 100 %!assert (trapz(1:5), 12)
101 %!assert (trapz(0:0.5:2,1:5), 6) 101 %!assert (trapz(0:0.5:2,1:5), 6)