comparison main/general/cumtrapz.m @ 0:6b33357c7561 octave-forge

Initial revision
author pkienzle
date Wed, 10 Oct 2001 19:54:49 +0000
parents
children 23ab5a2ed477
comparison
equal deleted inserted replaced
-1:000000000000 0:6b33357c7561
1 ## Copyright (C) 2000 Kai Habel
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} {@var{C} =} cumtrapz (@var{Y})
19 ## @deftypefnx {Function File} {@var{C} =} cumtrapz (@var{X},@var{Y})
20 ##
21 ## cumulative numerical intergration using trapezodial method.
22 ## cumtrapz (@var{y}) computes the cumulative integral of the vector y.
23 ## If @var{y} is a matrix the integral is computed columnwise.
24 ## If the @var(X) argument is omitted a equally spaced vector is assumed.
25 ## cumtrapz (@var{X},@var{Y}) evaluates the cumulative integral
26 ## with respect to @var{X}.
27 ##
28 ## @seealso{trapz,cumsum}
29 ## @end deftypefn
30
31 ## Author: Kai Habel <kai.habel@gmx.de>
32 ##
33 ## also: June 2000 Paul Kienzle (fixes,suggestions)
34
35 function C = cumtrapz (X, Y)
36
37 transposed = false;
38
39 if (nargin < 1) || (nargin > 2)
40 usage ("trapz (X, Y)");
41 elseif (nargin == 1)
42
43 if !(is_matrix (X))
44 error ("argument must be vector or matrix");
45 endif
46
47 if (is_vector(X) && (rows (X) == 1))
48 ## row vector
49 X=X(:);
50 transposed = true;
51 endif
52
53 r = rows(X);
54 C = zeros (size (X));
55
56 tmp = X(2:r, :) .+ X(1:r-1,:);
57
58 if (rows(tmp) == 1)
59 C(2,:) = 0.5 * tmp;
60 else
61 C(2:r,:) = 0.5 * cumsum (tmp);
62 endif
63
64 if (transposed) C = C'; endif
65
66 elseif (nargin == 2)
67
68 if !(is_matrix (X) && is_matrix (Y))
69 error ("arguments must be vectors or matrices of same size");
70 endif
71
72 if (size (X) == size (Y'))
73 X = X';
74 elseif (size (X) != size (Y))
75 error ("X and Y must have same shape");
76 endif
77
78 if (is_vector (Y) && (rows (Y) == 1))
79 ## Y is row vector
80 X = X (:); Y = Y (:);
81 transposed = true;
82 endif
83
84 r = rows (Y);
85 C = zeros (size (Y));
86
87 tmp = (X(2:r, :) .- X(1:r-1,:)) .* (Y(2:r,:) .+ Y(1:r - 1, :));
88
89 if (rows(tmp) == 1)
90 C(2,:) = 0.5 *tmp;
91 else
92 C(2:r,:) = 0.5 * cumsum (tmp);
93 endif
94
95 if (transposed) C = C'; endif
96
97 endif
98 endfunction