Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What's wrong with cumtrapz?

937 views
Skip to first unread message

Leung, Randolph [COPE/HKG]

unread,
May 3, 2001, 11:16:42 PM5/3/01
to

Hi there,

I have some experimental time traces to integrate. CUMTRAPZ seems
to be a simple and easy option for me. To test it, I tried to integrate a
simple SIN(X) time trace as follows,

x = 0:pi/100:4*pi];
y = sin( 2*pi*x).

inty = cumtrapz( x', y' );

I would expect a COS(X) time trace after CUMTRAPZ. The integrated
time trace gave a cos pattern of variation, correct amplitude BUT was
wrong in phase and shifted upwards, i.e. it is greater than zero for all
x and gives 0, rather than 1, at x = 0. I am very confused with the
results.

Do I need to pay special care in working with CUMTRAPZ, or it doesn't
serve my purpose? If the latter is true, could you recommend me
some other routines?

Thanks.

Randolph.

--
Posted from [216.201.19.6]
via Mailgate.ORG Server - http://www.Mailgate.ORG

Nabeel

unread,
May 4, 2001, 12:30:16 AM5/4/01
to
Hi,

Running this code:

x = 0:pi/100:4*pi;

y = sin( 2*pi*x);
inty = cumtrapz(x',y');
plot(x,y,x,inty)

produces a plot that looks right to me. You wrote:

> I would expect a COS(X) time trace after CUMTRAPZ. The integrated
> time trace gave a cos pattern of variation, correct amplitude BUT was
> wrong in phase and shifted upwards, i.e. it is greater than zero for all
> x and gives 0, rather than 1, at x = 0. I am very confused with the
> results.

CUMTRAPZ is the cumulative integral, or the running sum of the area
under the curve. From the graph above, you can see that the running sum
of the area is never negative, at the lowest it's zero. As for the
value at x=0, there's no area under the curve yet, so the value of 0 is
correct.

If you work it out by hand, you'll should be able to verify the results.

-- Nabeel

Timothy E. Vaughan

unread,
May 4, 2001, 10:09:52 AM5/4/01
to

""Leung, Randolph [COPE/HKG]"" <RckL...@Copeland-Corp.com> wrote in message
news:9B636583813BD311BE6...@fs83.hk.copeland-sid.com...

>
> I have some experimental time traces to integrate. CUMTRAPZ seems
> to be a simple and easy option for me. To test it, I tried to integrate a
> simple SIN(X) time trace as follows,
>
> x = 0:pi/100:4*pi];
> y = sin( 2*pi*x).
>
> inty = cumtrapz( x', y' );
>
> I would expect a COS(X) time trace after CUMTRAPZ. The integrated
> time trace gave a cos pattern of variation, correct amplitude BUT was
> wrong in phase and shifted upwards, i.e. it is greater than zero for all
> x and gives 0, rather than 1, at x = 0. I am very confused with the
> results.

It seems you may be forgetting a bit of your calculus. You are numerically
taking the INDEFINITE integral of your function. In that case, you must be
prepared to add a constant to your solution. [I think it is really only
shifted "upward", and not really wrong in phase.] You need to determine the
appropriate constant from other conditions of your problem.

Tim

Yuji Zhang

unread,
Jun 16, 2013, 1:33:10 AM6/16/13
to
nice explanation!

Rick, there is:
if F'(x) = f(x), then
integral f(x) dx = F(x) + C C is any constant

so the integral result has some shift along y axis. The shift should be determined by boundary conditions.



"Timothy E. Vaughan" <tvaughan@_NO_SPAM_bwh.harvard.edu> wrote in message <9cu9jh$doh$1...@plato.harvard.edu>...

Yuji Zhang

unread,
Jun 16, 2013, 1:35:15 AM6/16/13
to
Hi~ I've a question - maybe trivial.

why you wanna use cumtrapz(x',y') instead of cumtrapz(x,y) ? they're the same right?

Let me know. Thank you~


Nabeel <nab...@mathworks.com> wrote in message <3AF23058...@mathworks.com>...

Bruno Luong

unread,
Jun 16, 2013, 6:02:11 AM6/16/13
to
"Yuji Zhang" <yuji....@tufts.edu> wrote in message <kpjiqj$7dd$1...@newscl01ah.mathworks.com>...
> Hi~ I've a question - maybe trivial.
>
> why you wanna use cumtrapz(x',y') instead of cumtrapz(x,y) ? they're the same right?
>

They are the same if x,y are vectors.

Otherwise (array non-vector) if it important their orientation.

Bruno

Yuji Zhang

unread,
Jun 16, 2013, 9:51:08 AM6/16/13
to
Hi Bruno~

Thanks for getting back to me.

I see. I assumed they were same.

What do you mean by "(array non-vector) if it important their orientation"?

-yuji

"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <kpk2f3$98o$1...@newscl01ah.mathworks.com>...

Steven_Lord

unread,
Jun 17, 2013, 10:52:35 AM6/17/13
to


"Yuji Zhang" <yuji....@tufts.edu> wrote in message
news:kpkfsc$6r9$1...@newscl01ah.mathworks.com...
> Hi Bruno~
>
> Thanks for getting back to me.
>
> I see. I assumed they were same.
>
> What do you mean by "(array non-vector) if it important their
> orientation"?

http://www.mathworks.com/help/matlab/ref/cumtrapz.html

Read the second sentence of this snippet from the documentation:

"Z = cumtrapz(X,Y) computes the cumulative integral of Y with respect to X
using trapezoidal integration. X and Y must be vectors of the same length,
or X must be a column vector and Y an array whose first nonsingleton
dimension is length(X). cumtrapz operates across this dimension. Inputs X
and Y can be complex."


% This works; for vectors, orientation doesn't matter
cumtrapz(1:3, (4:6).')

% This works, since the first nonsingleton dimension of the second input is
3 which is the length of the first input
cumtrapz([1; 2; 3], ones(3, 4))

% This does NOT work, even though the second input is just the transpose of
the second input from the previous example
cumtrapz([1; 2; 3], ones(4, 3))


--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

0 new messages