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:
help cumtrapz

Subject: help cumtrapz

From: Naveen

Date: 19 Jan, 2010 15:06:04

Message: 1 of 9

I want to integrate a function using cumtrapz function as I want to have values of the integral at intermediate steps. The function is sin(1-x) with x as an integration variable ranging from 0 to 1. I have noticed that cumtrapz function doesn't do the integration correctly. One can check it by plotting the values obtained from cumtrapz function versus the values obtained from the analytical formula (1-cos(x) in this case). I figured out myself how to make cumtrapz work for this function. But I don't find any general method to make cumtrapz work for other functions e.g. x sin(1-x). I should mention usually cumtrapz works fine for general functions. However, for integrals that have upper limit of integration appear in the arguments of integrands (for trigonometric functions), it doesn't work. Has anyone faced such problems before? Please let me know. I have evaluated this integral by
writing my own program. But it is slow as it uses loops. I want to speed it up by using MATLAB built-in functions. Any help in this regard would be helpful.

Best,
Naveen

Subject: help cumtrapz

From: Steven Lord

Date: 19 Jan, 2010 15:34:55

Message: 2 of 9


"Naveen" <naveenyadav@gmail.com> wrote in message
news:hj4hos$883$1@fred.mathworks.com...
>I want to integrate a function using cumtrapz function as I want to have
>values of the integral at intermediate steps. The function is sin(1-x) with
>x as an integration variable ranging from 0 to 1. I have noticed that
>cumtrapz function doesn't do the integration correctly. One can check it by
>plotting the values obtained from cumtrapz function versus the values
>obtained from the analytical formula (1-cos(x) in this case).

Let's say you had a constant function that took on the value y = 1 at x = 0
and y = 1 at x = 0.1; The integral of that function would be 0.1, since
it's a rectangle 1 unit high and 0.1 units wide. What does CUMTRAPZ give
you?

x = [0 0.1];
y = [1 1];
cumtrapz(y)

That's the wrong answer, isn't it? But now integrate that same function,
only the x values change:

x = [0 0.5];
y = [1 1];
cumtrapz(y)

You got the same answer, didn't you?

You're missing a factor of the step size in the integration process. One
way to make sure this factor is taken into account is to call CUMTRAPZ with
_2_ inputs, not just one, so CUMTRAPZ knows the spacing along the x axis:

cumtrapz([0 0.1], y)
cumtrapz([0 0.5], y)

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: help cumtrapz

From: pietro

Date: 8 Dec, 2010 15:47:05

Message: 3 of 9

"Steven Lord" <slord@mathworks.com> wrote in message <hj4jeq$t3o$1@fred.mathworks.com>...
>
> "Naveen" <naveenyadav@gmail.com> wrote in message
> news:hj4hos$883$1@fred.mathworks.com...
> >I want to integrate a function using cumtrapz function as I want to have
> >values of the integral at intermediate steps. The function is sin(1-x) with
> >x as an integration variable ranging from 0 to 1. I have noticed that
> >cumtrapz function doesn't do the integration correctly. One can check it by
> >plotting the values obtained from cumtrapz function versus the values
> >obtained from the analytical formula (1-cos(x) in this case).
>
> Let's say you had a constant function that took on the value y = 1 at x = 0
> and y = 1 at x = 0.1; The integral of that function would be 0.1, since
> it's a rectangle 1 unit high and 0.1 units wide. What does CUMTRAPZ give
> you?
>
> x = [0 0.1];
> y = [1 1];
> cumtrapz(y)
>
> That's the wrong answer, isn't it? But now integrate that same function,
> only the x values change:
>
> x = [0 0.5];
> y = [1 1];
> cumtrapz(y)
>
> You got the same answer, didn't you?
>
> You're missing a factor of the step size in the integration process. One
> way to make sure this factor is taken into account is to call CUMTRAPZ with
> _2_ inputs, not just one, so CUMTRAPZ knows the spacing along the x axis:
>
> cumtrapz([0 0.1], y)
> cumtrapz([0 0.5], y)
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Hello,

I get the same problem:
I have created a simple example:
x=[0:0.01:10];
y=sin(2*pi*2*x);
y1=cumtrapz(x,y);

this is the result:

http://img219.imageshack.us/f/cumtrapz.jpg/

The red isn't the real integral function: the trigonometric period is much different, moreover it isn't a cosine function. Moreover it is very strange I don't get a signal drift.

thans

Pietro

Subject: help cumtrapz

From: Paulo Silva

Date: 8 Dec, 2010 16:52:22

Message: 4 of 9

I made a GUI for numeric integration using simpson and trapezoidal methods with matlab functions and my own functions, it also draws functions and shows the method, if there are enough people interested I might upload it to file exchange, here's what the GUI looks like (it's in my own language but I can change it to english)

http://img718.imageshack.us/img718/6536/image1am.gif

Subject: help cumtrapz

From: John D'Errico

Date: 8 Dec, 2010 16:52:22

Message: 5 of 9

"pietro " <bracardi82@email.it> wrote in message <ido99p$dd0$1@fred.mathworks.com>...

> Hello,
>
> I get the same problem:
> I have created a simple example:
> x=[0:0.01:10];
> y=sin(2*pi*2*x);
> y1=cumtrapz(x,y);
>
> this is the result:
>
> http://img219.imageshack.us/f/cumtrapz.jpg/
>
> The red isn't the real integral function: the trigonometric period is much different, moreover it isn't a cosine function. Moreover it is very strange I don't get a signal drift.
>

Why do you think it is NOT a cosine?

I think that perhaps you did the integration wrong, nor
can I remotely guess what you expect the period of this
function to be.

The integral of the function sin(4*pi*x) is roughly

   - cos(4*pi*x)/(4*pi) + C

where C is an arbitrary constant of integration. cumtrapz
presumes an implicit integration constant of zero. Also
note the sign out front.

Note that the result has 4*pi in it. But in your example,
you used sin(2*pi*2*x). This IS sin(4*pi*x), since I
vaguely recall that multiplication has many nice properties
like commutativity and associativity. The plot label shows
sin(2*pi*x), which is inconsistent with your problem as
stated.

So perhaps what is strange is your algebra.

John

Subject: help cumtrapz

From: pietro

Date: 8 Dec, 2010 17:10:21

Message: 6 of 9

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <idod46$1of$1@fred.mathworks.com>...
> "pietro " <bracardi82@email.it> wrote in message <ido99p$dd0$1@fred.mathworks.com>...
>
> > Hello,
> >
> > I get the same problem:
> > I have created a simple example:
> > x=[0:0.01:10];
> > y=sin(2*pi*2*x);
> > y1=cumtrapz(x,y);
> >
> > this is the result:
> >
> > http://img219.imageshack.us/f/cumtrapz.jpg/
> >
> > The red isn't the real integral function: the trigonometric period is much different, moreover it isn't a cosine function. Moreover it is very strange I don't get a signal drift.
> >
>
> Why do you think it is NOT a cosine?
>
> I think that perhaps you did the integration wrong, nor
> can I remotely guess what you expect the period of this
> function to be.
>
> The integral of the function sin(4*pi*x) is roughly
>
> - cos(4*pi*x)/(4*pi) + C
>
> where C is an arbitrary constant of integration. cumtrapz
> presumes an implicit integration constant of zero. Also
> note the sign out front.
>
> Note that the result has 4*pi in it. But in your example,
> you used sin(2*pi*2*x). This IS sin(4*pi*x), since I
> vaguely recall that multiplication has many nice properties
> like commutativity and associativity. The plot label shows
> sin(2*pi*x), which is inconsistent with your problem as
> stated.
>
> So perhaps what is strange is your algebra.
>
> John

Hi John,

I have make mistake on legend label, but the data are correct, please check ok the graph, the sin in the blue line has two periods pour second.
However you are on right when you say, the integral of the function sin(4*pi*x) is roughly - cos(4*pi*x)/(4*pi) + C; in my case C=0. For X=0 I should get 1/(4*pi), instead I get 0. Moreover the max value of integrate function should be 1/(4*pi)=0.08 but I get 1/(2*pi)=0.16. So I'm not be able to understand why I am on fault. May you explain please?
thanks

Pietro

Subject: help cumtrapz

From: John D'Errico

Date: 8 Dec, 2010 17:29:08

Message: 7 of 9

"pietro " <bracardi82@email.it> wrote in message <idoe5t$a9h$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <idod46$1of$1@fred.mathworks.com>...
> > "pietro " <bracardi82@email.it> wrote in message <ido99p$dd0$1@fred.mathworks.com>...
> >
> > > Hello,
> > >
> > > I get the same problem:
> > > I have created a simple example:
> > > x=[0:0.01:10];
> > > y=sin(2*pi*2*x);
> > > y1=cumtrapz(x,y);
> > >
> > > this is the result:
> > >
> > > http://img219.imageshack.us/f/cumtrapz.jpg/
> > >
> > > The red isn't the real integral function: the trigonometric period is much different, moreover it isn't a cosine function. Moreover it is very strange I don't get a signal drift.
> > >
> >
> > Why do you think it is NOT a cosine?
> >
> > I think that perhaps you did the integration wrong, nor
> > can I remotely guess what you expect the period of this
> > function to be.
> >
> > The integral of the function sin(4*pi*x) is roughly
> >
> > - cos(4*pi*x)/(4*pi) + C
> >
> > where C is an arbitrary constant of integration. cumtrapz
> > presumes an implicit integration constant of zero. Also
> > note the sign out front.
> >
> > Note that the result has 4*pi in it. But in your example,
> > you used sin(2*pi*2*x). This IS sin(4*pi*x), since I
> > vaguely recall that multiplication has many nice properties
> > like commutativity and associativity. The plot label shows
> > sin(2*pi*x), which is inconsistent with your problem as
> > stated.
> >
> > So perhaps what is strange is your algebra.
> >
> > John
>
> Hi John,
>
> I have make mistake on legend label, but the data are correct, please check ok the graph, the sin in the blue line has two periods pour second.
> However you are on right when you say, the integral of the function sin(4*pi*x) is roughly - cos(4*pi*x)/(4*pi) + C; in my case C=0. For X=0 I should get 1/(4*pi), instead I get 0. Moreover the max value of integrate function should be 1/(4*pi)=0.08 but I get 1/(2*pi)=0.16. So I'm not be able to understand why I am on fault. May you explain please?
>

I am quite surprised that you think the DEFINITE integral
of sin(4*pi*x), over the limits of integration [0,0] is
ANY non-zero number. cumtrapz computes a definite
integral.

Perhaps you are forgetting what the constant of integration
does for you. Return directly to calculus 102, do not pass
go, do not collect $200.

When you check the maximum value of that returned by
cumtrapz, did you bother to look at the MINIMUM of that
same curve? What property do you know about a cosine
function? What has this to do with the constant of
integration?

John

Subject: help cumtrapz

From: pietro

Date: 8 Dec, 2010 21:58:07

Message: 8 of 9

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <idof94$mdg$1@fred.mathworks.com>...
> "pietro " <bracardi82@email.it> wrote in message <idoe5t$a9h$1@fred.mathworks.com>...
> > "John D'Errico" <woodchips@rochester.rr.com> wrote in message <idod46$1of$1@fred.mathworks.com>...
> > > "pietro " <bracardi82@email.it> wrote in message <ido99p$dd0$1@fred.mathworks.com>...
> > >
> > > > Hello,
> > > >
> > > > I get the same problem:
> > > > I have created a simple example:
> > > > x=[0:0.01:10];
> > > > y=sin(2*pi*2*x);
> > > > y1=cumtrapz(x,y);
> > > >
> > > > this is the result:
> > > >
> > > > http://img219.imageshack.us/f/cumtrapz.jpg/
> > > >
> > > > The red isn't the real integral function: the trigonometric period is much different, moreover it isn't a cosine function. Moreover it is very strange I don't get a signal drift.
> > > >
> > >
> > > Why do you think it is NOT a cosine?
> > >
> > > I think that perhaps you did the integration wrong, nor
> > > can I remotely guess what you expect the period of this
> > > function to be.
> > >
> > > The integral of the function sin(4*pi*x) is roughly
> > >
> > > - cos(4*pi*x)/(4*pi) + C
> > >
> > > where C is an arbitrary constant of integration. cumtrapz
> > > presumes an implicit integration constant of zero. Also
> > > note the sign out front.
> > >
> > > Note that the result has 4*pi in it. But in your example,
> > > you used sin(2*pi*2*x). This IS sin(4*pi*x), since I
> > > vaguely recall that multiplication has many nice properties
> > > like commutativity and associativity. The plot label shows
> > > sin(2*pi*x), which is inconsistent with your problem as
> > > stated.
> > >
> > > So perhaps what is strange is your algebra.
> > >
> > > John
> >
> > Hi John,
> >
> > I have make mistake on legend label, but the data are correct, please check ok the graph, the sin in the blue line has two periods pour second.
> > However you are on right when you say, the integral of the function sin(4*pi*x) is roughly - cos(4*pi*x)/(4*pi) + C; in my case C=0. For X=0 I should get 1/(4*pi), instead I get 0. Moreover the max value of integrate function should be 1/(4*pi)=0.08 but I get 1/(2*pi)=0.16. So I'm not be able to understand why I am on fault. May you explain please?
> >
>
> I am quite surprised that you think the DEFINITE integral
> of sin(4*pi*x), over the limits of integration [0,0] is
> ANY non-zero number. cumtrapz computes a definite
> integral.
>
> Perhaps you are forgetting what the constant of integration
> does for you. Return directly to calculus 102, do not pass
> go, do not collect $200.
>
> When you check the maximum value of that returned by
> cumtrapz, did you bother to look at the MINIMUM of that
> same curve? What property do you know about a cosine
> function? What has this to do with the constant of
> integration?
>
> John

Maybe I have misunderstood something, but with cumtrapz, can I get the INDEFINITE integral of a funcion?

However yes, the amplitude is correct I'm sorry I was in mistaken, but che integral constant isn't null because the function is shifted, isn't it?

Thanks

Pietro

Subject: help cumtrapz

From: Meghan

Date: 2 Aug, 2012 18:33:40

Message: 9 of 9

Paulo,
I would be interested in your GUI if you are willing to share the files. I will reference you regarding use of your tool. Thank you.
Meghan

"Paulo Silva" wrote in message <idod46$1n1$1@fred.mathworks.com>...
> I made a GUI for numeric integration using simpson and trapezoidal methods with matlab functions and my own functions, it also draws functions and shows the method, if there are enough people interested I might upload it to file exchange, here's what the GUI looks like (it's in my own language but I can change it to english)
>
> http://img718.imageshack.us/img718/6536/image1am.gif

Tags for 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