Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
What's wrong with cumtrapz?

Subject: What's wrong with cumtrapz?

From: RckLeung@Copeland-Corp.com ("Leung, Randolph [COPE/HKG]")

Date: 4 May, 2001 03:16:42

Message: 1 of 8


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

Subject: What's wrong with cumtrapz?

From: Nabeel

Date: 4 May, 2001 00:30:16

Message: 2 of 8

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

Subject: What's wrong with cumtrapz?

From: Timothy E. Vaughan

Date: 4 May, 2001 09:09:52

Message: 3 of 8


""Leung, Randolph [COPE/HKG]"" <RckLeung@Copeland-Corp.com> wrote in message
news:9B636583813BD311BE6400508B10481F6C5274@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

Subject: What's wrong with cumtrapz?

From: Yuji Zhang

Date: 16 Jun, 2013 05:33:10

Message: 4 of 8

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>...
>
> ""Leung, Randolph [COPE/HKG]"" <RckLeung@Copeland-Corp.com> wrote in message
> news:9B636583813BD311BE6400508B10481F6C5274@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
>
>
>

Subject: What's wrong with cumtrapz?

From: Yuji Zhang

Date: 16 Jun, 2013 05:35:15

Message: 5 of 8

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 <nabeel@mathworks.com> wrote in message <3AF23058.E8745FEB@mathworks.com>...
> 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

Subject: What's wrong with cumtrapz?

From: Bruno Luong

Date: 16 Jun, 2013 10:02:11

Message: 6 of 8

"Yuji Zhang" <yuji.zhang@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

Subject: What's wrong with cumtrapz?

From: Yuji Zhang

Date: 16 Jun, 2013 13:51:08

Message: 7 of 8

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.luong@fogale.findmycountry> wrote in message <kpk2f3$98o$1@newscl01ah.mathworks.com>...
> "Yuji Zhang" <yuji.zhang@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

Subject: What's wrong with cumtrapz?

From: Steven_Lord

Date: 17 Jun, 2013 14:52:35

Message: 8 of 8



"Yuji Zhang" <yuji.zhang@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
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us