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:
Integration with variable limits

Subject: Integration with variable limits

From: Sanaa

Date: 25 Jul, 2013 14:09:14

Message: 1 of 18

Hi all,
How to integrate a function within a loop and the limits of integration are variables?
For instance, y(i)*(1-y(i)) is the function I wish to integrate from 0.25*i to t where
t \in (0.25*i, 0.25*(i+1)).
Any help is really appreciated.
Sanaa Moussa

Subject: Integration with variable limits

From: Torsten

Date: 25 Jul, 2013 14:24:21

Message: 2 of 18

"Sanaa" wrote in message <ksrbia$r0l$1@newscl01ah.mathworks.com>...
> Hi all,
> How to integrate a function within a loop and the limits of integration are variables?
> For instance, y(i)*(1-y(i)) is the function I wish to integrate from 0.25*i to t where
> t \in (0.25*i, 0.25*(i+1)).
> Any help is really appreciated.
> Sanaa Moussa

I think y(i)*(1-y(i)) is just a real number, not a function, isn't it ?

Best wishes
Torsten.

Subject: Integration with variable limits

From: Sanaa

Date: 25 Jul, 2013 20:58:13

Message: 3 of 18

"Torsten" wrote in message <ksrcel$2k$1@newscl01ah.mathworks.com>...
> "Sanaa" wrote in message <ksrbia$r0l$1@newscl01ah.mathworks.com>...
> > Hi all,
> > How to integrate a function within a loop and the limits of integration are variables?
> > For instance, y(i)*(1-y(i)) is the function I wish to integrate from 0.25*i to t where
> > t \in (0.25*i, 0.25*(i+1)).
> > Any help is really appreciated.
> > Sanaa Moussa
>
> I think y(i)*(1-y(i)) is just a real number, not a function, isn't it ?
>
> Best wishes
> Torsten.

Thanks for replying. You are right, but I mean the function is y*(1-y) which I want to put it into a loop
My code is
itermax=300;min=itermax-9;% That is we plot from 291:300 i.e. 10 values of x only.
r=0.25;
for rho=0:0.001:4
    %x0=0.1;
    x(1)=0.1;
    for i=1:itermax-1
        %t=linspace(i*r,(i+1)*r,itermax)
        y(i+1)=x(i);
        x(i+1)=x(i)+ rho*int(y(i)*(1-y(i)),i*r,(i+1)*r);
end
%fix(y)
plot(rho*ones(10),x(min:itermax),'b.','linewidth',0.1)
hold on
end
     fsize=15;
     xlabel('\rho','FontSize',fsize)
     ylabel('\itx','FontSize',fsize)
     %title('r=0.25, \alpha=1','FontSize',fsize)
    hold off
% print(gcf, '-djpeg', '-zbuffer', 'bif.png');

I get the error
Function 'int' is not defined for values of class 'double'.
what does this mean and how to fix it please?
Many thanks in advance.

Subject: Integration with variable limits

From: Torsten

Date: 26 Jul, 2013 06:41:27

Message: 4 of 18

"Sanaa" wrote in message <kss3h5$50n$1@newscl01ah.mathworks.com>...
> "Torsten" wrote in message <ksrcel$2k$1@newscl01ah.mathworks.com>...
> > "Sanaa" wrote in message <ksrbia$r0l$1@newscl01ah.mathworks.com>...
> > > Hi all,
> > > How to integrate a function within a loop and the limits of integration are variables?
> > > For instance, y(i)*(1-y(i)) is the function I wish to integrate from 0.25*i to t where
> > > t \in (0.25*i, 0.25*(i+1)).
> > > Any help is really appreciated.
> > > Sanaa Moussa
> >
> > I think y(i)*(1-y(i)) is just a real number, not a function, isn't it ?
> >
> > Best wishes
> > Torsten.
>
> Thanks for replying. You are right, but I mean the function is y*(1-y) which I want to put it into a loop
> My code is
> itermax=300;min=itermax-9;% That is we plot from 291:300 i.e. 10 values of x only.
> r=0.25;
> for rho=0:0.001:4
> %x0=0.1;
> x(1)=0.1;
> for i=1:itermax-1
> %t=linspace(i*r,(i+1)*r,itermax)
> y(i+1)=x(i);
> x(i+1)=x(i)+ rho*int(y(i)*(1-y(i)),i*r,(i+1)*r);
> end
> %fix(y)
> plot(rho*ones(10),x(min:itermax),'b.','linewidth',0.1)
> hold on
> end
> fsize=15;
> xlabel('\rho','FontSize',fsize)
> ylabel('\itx','FontSize',fsize)
> %title('r=0.25, \alpha=1','FontSize',fsize)
> hold off
> % print(gcf, '-djpeg', '-zbuffer', 'bif.png');
>
> I get the error
> Function 'int' is not defined for values of class 'double'.
> what does this mean and how to fix it please?
> Many thanks in advance.

It means what I said before:
y(i)*(1-y(i))
is a scalar value and not a function.
So "int" does not know how to handle this because it expects a function, not a scalar as its first input argument.

Best wishes
Torsten.

Subject: Integration with variable limits

From: Sanaa

Date: 30 Jul, 2013 21:33:24

Message: 5 of 18

"Torsten" wrote in message <kst5mn$h75$1@newscl01ah.mathworks.com>...
> "Sanaa" wrote in message <kss3h5$50n$1@newscl01ah.mathworks.com>...
> > "Torsten" wrote in message <ksrcel$2k$1@newscl01ah.mathworks.com>...
> > > "Sanaa" wrote in message <ksrbia$r0l$1@newscl01ah.mathworks.com>...
> > > > Hi all,
> > > > How to integrate a function within a loop and the limits of integration are variables?
> > > > For instance, y(i)*(1-y(i)) is the function I wish to integrate from 0.25*i to t where
> > > > t \in (0.25*i, 0.25*(i+1)).
> > > > Any help is really appreciated.
> > > > Sanaa Moussa
> > >
> > > I think y(i)*(1-y(i)) is just a real number, not a function, isn't it ?
> > >
> > > Best wishes
> > > Torsten.
> >
> > Thanks for replying. You are right, but I mean the function is y*(1-y) which I want to put it into a loop
> > My code is
> > itermax=300;min=itermax-9;% That is we plot from 291:300 i.e. 10 values of x only.
> > r=0.25;
> > for rho=0:0.001:4
> > %x0=0.1;
> > x(1)=0.1;
> > for i=1:itermax-1
> > %t=linspace(i*r,(i+1)*r,itermax)
> > y(i+1)=x(i);
> > x(i+1)=x(i)+ rho*int(y(i)*(1-y(i)),i*r,(i+1)*r);
> > end
> > %fix(y)
> > plot(rho*ones(10),x(min:itermax),'b.','linewidth',0.1)
> > hold on
> > end
> > fsize=15;
> > xlabel('\rho','FontSize',fsize)
> > ylabel('\itx','FontSize',fsize)
> > %title('r=0.25, \alpha=1','FontSize',fsize)
> > hold off
> > % print(gcf, '-djpeg', '-zbuffer', 'bif.png');
> >
> > I get the error
> > Function 'int' is not defined for values of class 'double'.
> > what does this mean and how to fix it please?
> > Many thanks in advance.
>
> It means what I said before:
> y(i)*(1-y(i))
> is a scalar value and not a function.
> So "int" does not know how to handle this because it expects a function, not a scalar as its first input argument.
>
> Best wishes
> Torsten.

Sorry for not posing my question correctly. The function I want to integrate is
y(s)*(1-y(s)) ds from (n*0.25) to t, and t/in(n*0.25, (n+1)*0.25).
Do you have any idea on how to format my code above to solve my problem?
Thanks a lot in advance

Subject: Integration with variable limits

From: Torsten

Date: 31 Jul, 2013 06:57:07

Message: 6 of 18

"Sanaa" wrote in message <kt9bf4$56h$1@newscl01ah.mathworks.com>...
> "Torsten" wrote in message <kst5mn$h75$1@newscl01ah.mathworks.com>...
> > "Sanaa" wrote in message <kss3h5$50n$1@newscl01ah.mathworks.com>...
> > > "Torsten" wrote in message <ksrcel$2k$1@newscl01ah.mathworks.com>...
> > > > "Sanaa" wrote in message <ksrbia$r0l$1@newscl01ah.mathworks.com>...
> > > > > Hi all,
> > > > > How to integrate a function within a loop and the limits of integration are variables?
> > > > > For instance, y(i)*(1-y(i)) is the function I wish to integrate from 0.25*i to t where
> > > > > t \in (0.25*i, 0.25*(i+1)).
> > > > > Any help is really appreciated.
> > > > > Sanaa Moussa
> > > >
> > > > I think y(i)*(1-y(i)) is just a real number, not a function, isn't it ?
> > > >
> > > > Best wishes
> > > > Torsten.
> > >
> > > Thanks for replying. You are right, but I mean the function is y*(1-y) which I want to put it into a loop
> > > My code is
> > > itermax=300;min=itermax-9;% That is we plot from 291:300 i.e. 10 values of x only.
> > > r=0.25;
> > > for rho=0:0.001:4
> > > %x0=0.1;
> > > x(1)=0.1;
> > > for i=1:itermax-1
> > > %t=linspace(i*r,(i+1)*r,itermax)
> > > y(i+1)=x(i);
> > > x(i+1)=x(i)+ rho*int(y(i)*(1-y(i)),i*r,(i+1)*r);
> > > end
> > > %fix(y)
> > > plot(rho*ones(10),x(min:itermax),'b.','linewidth',0.1)
> > > hold on
> > > end
> > > fsize=15;
> > > xlabel('\rho','FontSize',fsize)
> > > ylabel('\itx','FontSize',fsize)
> > > %title('r=0.25, \alpha=1','FontSize',fsize)
> > > hold off
> > > % print(gcf, '-djpeg', '-zbuffer', 'bif.png');
> > >
> > > I get the error
> > > Function 'int' is not defined for values of class 'double'.
> > > what does this mean and how to fix it please?
> > > Many thanks in advance.
> >
> > It means what I said before:
> > y(i)*(1-y(i))
> > is a scalar value and not a function.
> > So "int" does not know how to handle this because it expects a function, not a scalar as its first input argument.
> >
> > Best wishes
> > Torsten.
>
> Sorry for not posing my question correctly. The function I want to integrate is
> y(s)*(1-y(s)) ds from (n*0.25) to t, and t/in(n*0.25, (n+1)*0.25).
> Do you have any idea on how to format my code above to solve my problem?
> Thanks a lot in advance

int_{n*0.25}^{t} (y(s)*(1-y(s)) ds = (1/2*t^2 - 1/3*t^3) - (1/2*(n*0.25)^2 - 1/3*(n*0.25)^3)
for t/in(n*0.25, (n+1)*0.25).
Is it that what you asked for ?

Best wishes
Torsten.

Subject: Integration with variable limits

From: Torsten

Date: 31 Jul, 2013 07:09:08

Message: 7 of 18

"Torsten" wrote in message <ktacg3$fkj$1@newscl01ah.mathworks.com>...
> "Sanaa" wrote in message <kt9bf4$56h$1@newscl01ah.mathworks.com>...
> > "Torsten" wrote in message <kst5mn$h75$1@newscl01ah.mathworks.com>...
> > > "Sanaa" wrote in message <kss3h5$50n$1@newscl01ah.mathworks.com>...
> > > > "Torsten" wrote in message <ksrcel$2k$1@newscl01ah.mathworks.com>...
> > > > > "Sanaa" wrote in message <ksrbia$r0l$1@newscl01ah.mathworks.com>...
> > > > > > Hi all,
> > > > > > How to integrate a function within a loop and the limits of integration are variables?
> > > > > > For instance, y(i)*(1-y(i)) is the function I wish to integrate from 0.25*i to t where
> > > > > > t \in (0.25*i, 0.25*(i+1)).
> > > > > > Any help is really appreciated.
> > > > > > Sanaa Moussa
> > > > >
> > > > > I think y(i)*(1-y(i)) is just a real number, not a function, isn't it ?
> > > > >
> > > > > Best wishes
> > > > > Torsten.
> > > >
> > > > Thanks for replying. You are right, but I mean the function is y*(1-y) which I want to put it into a loop
> > > > My code is
> > > > itermax=300;min=itermax-9;% That is we plot from 291:300 i.e. 10 values of x only.
> > > > r=0.25;
> > > > for rho=0:0.001:4
> > > > %x0=0.1;
> > > > x(1)=0.1;
> > > > for i=1:itermax-1
> > > > %t=linspace(i*r,(i+1)*r,itermax)
> > > > y(i+1)=x(i);
> > > > x(i+1)=x(i)+ rho*int(y(i)*(1-y(i)),i*r,(i+1)*r);
> > > > end
> > > > %fix(y)
> > > > plot(rho*ones(10),x(min:itermax),'b.','linewidth',0.1)
> > > > hold on
> > > > end
> > > > fsize=15;
> > > > xlabel('\rho','FontSize',fsize)
> > > > ylabel('\itx','FontSize',fsize)
> > > > %title('r=0.25, \alpha=1','FontSize',fsize)
> > > > hold off
> > > > % print(gcf, '-djpeg', '-zbuffer', 'bif.png');
> > > >
> > > > I get the error
> > > > Function 'int' is not defined for values of class 'double'.
> > > > what does this mean and how to fix it please?
> > > > Many thanks in advance.
> > >
> > > It means what I said before:
> > > y(i)*(1-y(i))
> > > is a scalar value and not a function.
> > > So "int" does not know how to handle this because it expects a function, not a scalar as its first input argument.
> > >
> > > Best wishes
> > > Torsten.
> >
> > Sorry for not posing my question correctly. The function I want to integrate is
> > y(s)*(1-y(s)) ds from (n*0.25) to t, and t/in(n*0.25, (n+1)*0.25).
> > Do you have any idea on how to format my code above to solve my problem?
> > Thanks a lot in advance
>
> int_{n*0.25}^{t} (y(s)*(1-y(s)) ds = (1/2*t^2 - 1/3*t^3) - (1/2*(n*0.25)^2 - 1/3*(n*0.25)^3)
> for t/in(n*0.25, (n+1)*0.25).
> Is it that what you asked for ?
>
> Best wishes
> Torsten.

No sorry, this is wrong - I integrated s*(1-s).
For integration, you will have to know the explicit form of y as a function of s,
e.g. y(s)=sin(s) or something like that.

Best wishes
Torsten.

Subject: Integration with variable limits

From: Steven_Lord

Date: 31 Jul, 2013 13:59:27

Message: 8 of 18



"Torsten " <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message
news:ktad6k$gtn$1@newscl01ah.mathworks.com...
> "Torsten" wrote in message <ktacg3$fkj$1@newscl01ah.mathworks.com>...
>> "Sanaa" wrote in message <kt9bf4$56h$1@newscl01ah.mathworks.com>...
>> > "Torsten" wrote in message <kst5mn$h75$1@newscl01ah.mathworks.com>...
>> > > "Sanaa" wrote in message <kss3h5$50n$1@newscl01ah.mathworks.com>...
>> > > > "Torsten" wrote in message
>> > > > <ksrcel$2k$1@newscl01ah.mathworks.com>...
>> > > > > "Sanaa" wrote in message
>> > > > > <ksrbia$r0l$1@newscl01ah.mathworks.com>...
>> > > > > > Hi all,
>> > > > > > How to integrate a function within a loop and the limits of
>> > > > > > integration are variables?
>> > > > > > For instance, y(i)*(1-y(i)) is the function I wish to integrate
>> > > > > > from 0.25*i to t where t \in (0.25*i, 0.25*(i+1)).
>> > > > > > Any help is really appreciated.
>> > > > > > Sanaa Moussa
>> > > > >
>> > > > > I think y(i)*(1-y(i)) is just a real number, not a function,
>> > > > > isn't it ?
>> > > > >
>> > > > > Best wishes
>> > > > > Torsten.
>> > > >
>> > > > Thanks for replying. You are right, but I mean the function is
>> > > > y*(1-y) which I want to put it into a loop
>> > > > My code is
>> > > > itermax=300;min=itermax-9;% That is we plot from 291:300 i.e. 10
>> > > > values of x only.
>> > > > r=0.25;
>> > > > for rho=0:0.001:4
>> > > > %x0=0.1;
>> > > > x(1)=0.1;
>> > > > for i=1:itermax-1
>> > > > %t=linspace(i*r,(i+1)*r,itermax)
>> > > > y(i+1)=x(i);
>> > > > x(i+1)=x(i)+ rho*int(y(i)*(1-y(i)),i*r,(i+1)*r);
>> > > > end
>> > > > %fix(y)
>> > > > plot(rho*ones(10),x(min:itermax),'b.','linewidth',0.1)
>> > > > hold on
>> > > > end
>> > > > fsize=15;
>> > > > xlabel('\rho','FontSize',fsize)
>> > > > ylabel('\itx','FontSize',fsize)
>> > > > %title('r=0.25, \alpha=1','FontSize',fsize)
>> > > > hold off
>> > > > % print(gcf, '-djpeg', '-zbuffer', 'bif.png');
>> > > >
>> > > > I get the error
>> > > > Function 'int' is not defined for values of class 'double'.
>> > > > what does this mean and how to fix it please?
>> > > > Many thanks in advance.
>> > >
>> > > It means what I said before:
>> > > y(i)*(1-y(i)) is a scalar value and not a function.
>> > > So "int" does not know how to handle this because it expects a
>> > > function, not a scalar as its first input argument.
>> > >
>> > > Best wishes
>> > > Torsten.
>> >
>> > Sorry for not posing my question correctly. The function I want to
>> > integrate is
>> > y(s)*(1-y(s)) ds from (n*0.25) to t, and t/in(n*0.25, (n+1)*0.25).
>> > Do you have any idea on how to format my code above to solve my
>> > problem?
>> > Thanks a lot in advance
>>
>> int_{n*0.25}^{t} (y(s)*(1-y(s)) ds = (1/2*t^2 - 1/3*t^3) -
>> (1/2*(n*0.25)^2 - 1/3*(n*0.25)^3)
>> for t/in(n*0.25, (n+1)*0.25).
>> Is it that what you asked for ?
>>
>> Best wishes
>> Torsten.
>
> No sorry, this is wrong - I integrated s*(1-s).
> For integration, you will have to know the explicit form of y as a
> function of s,
> e.g. y(s)=sin(s) or something like that.

If Y is only known as data, you will need to do one of two things:

1) Use TRAPZ or CUMTRAPZ.
2) Interpolate the Y data to obtain the value of the "function" at
intermediate points. This can be dangerous if your data does not
sufficiently represent your actual function:

x = 0:10
y = sin(x*pi).^2
answer1 = integral(@(t) interp1(x, y, t), 0, 10)
syms z
answer2 = int(sin(z*pi)^2, 0, 10)

Your numeric data makes your function "look like" the constant function y =
0 and so answer1 is very close to 0. The symbolic integration gives an
answer2 of 5, which is correct. Evaluating y at more points (x = 0:0.1:10
for example) would show the true form of the curve and gives an answer very
close to 5.

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

Subject: Integration with variable limits

From: Sanaa

Date: 15 Aug, 2013 12:04:07

Message: 9 of 18

"Steven_Lord" <slord@mathworks.com> wrote in message <ktb57u$el8$1@newscl01ah.mathworks.com>...
>
>
> "Torsten " <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message
> news:ktad6k$gtn$1@newscl01ah.mathworks.com...
> > "Torsten" wrote in message <ktacg3$fkj$1@newscl01ah.mathworks.com>...
> >> "Sanaa" wrote in message <kt9bf4$56h$1@newscl01ah.mathworks.com>...
> >> > "Torsten" wrote in message <kst5mn$h75$1@newscl01ah.mathworks.com>...
> >> > > "Sanaa" wrote in message <kss3h5$50n$1@newscl01ah.mathworks.com>...
> >> > > > "Torsten" wrote in message
> >> > > > <ksrcel$2k$1@newscl01ah.mathworks.com>...
> >> > > > > "Sanaa" wrote in message
> >> > > > > <ksrbia$r0l$1@newscl01ah.mathworks.com>...
> >> > > > > > Hi all,
> >> > > > > > How to integrate a function within a loop and the limits of
> >> > > > > > integration are variables?
> >> > > > > > For instance, y(i)*(1-y(i)) is the function I wish to integrate
> >> > > > > > from 0.25*i to t where t \in (0.25*i, 0.25*(i+1)).
> >> > > > > > Any help is really appreciated.
> >> > > > > > Sanaa Moussa
> >> > > > >
> >> > > > > I think y(i)*(1-y(i)) is just a real number, not a function,
> >> > > > > isn't it ?
> >> > > > >
> >> > > > > Best wishes
> >> > > > > Torsten.
> >> > > >
> >> > > > Thanks for replying. You are right, but I mean the function is
> >> > > > y*(1-y) which I want to put it into a loop
> >> > > > My code is
> >> > > > itermax=300;min=itermax-9;% That is we plot from 291:300 i.e. 10
> >> > > > values of x only.
> >> > > > r=0.25;
> >> > > > for rho=0:0.001:4
> >> > > > %x0=0.1;
> >> > > > x(1)=0.1;
> >> > > > for i=1:itermax-1
> >> > > > %t=linspace(i*r,(i+1)*r,itermax)
> >> > > > y(i+1)=x(i);
> >> > > > x(i+1)=x(i)+ rho*int(y(i)*(1-y(i)),i*r,(i+1)*r);
> >> > > > end
> >> > > > %fix(y)
> >> > > > plot(rho*ones(10),x(min:itermax),'b.','linewidth',0.1)
> >> > > > hold on
> >> > > > end
> >> > > > fsize=15;
> >> > > > xlabel('\rho','FontSize',fsize)
> >> > > > ylabel('\itx','FontSize',fsize)
> >> > > > %title('r=0.25, \alpha=1','FontSize',fsize)
> >> > > > hold off
> >> > > > % print(gcf, '-djpeg', '-zbuffer', 'bif.png');
> >> > > >
> >> > > > I get the error
> >> > > > Function 'int' is not defined for values of class 'double'.
> >> > > > what does this mean and how to fix it please?
> >> > > > Many thanks in advance.
> >> > >
> >> > > It means what I said before:
> >> > > y(i)*(1-y(i)) is a scalar value and not a function.
> >> > > So "int" does not know how to handle this because it expects a
> >> > > function, not a scalar as its first input argument.
> >> > >
> >> > > Best wishes
> >> > > Torsten.
> >> >
> >> > Sorry for not posing my question correctly. The function I want to
> >> > integrate is
> >> > y(s)*(1-y(s)) ds from (n*0.25) to t, and t/in(n*0.25, (n+1)*0.25).
> >> > Do you have any idea on how to format my code above to solve my
> >> > problem?
> >> > Thanks a lot in advance
> >>
> >> int_{n*0.25}^{t} (y(s)*(1-y(s)) ds = (1/2*t^2 - 1/3*t^3) -
> >> (1/2*(n*0.25)^2 - 1/3*(n*0.25)^3)
> >> for t/in(n*0.25, (n+1)*0.25).
> >> Is it that what you asked for ?
> >>
> >> Best wishes
> >> Torsten.
> >
> > No sorry, this is wrong - I integrated s*(1-s).
> > For integration, you will have to know the explicit form of y as a
> > function of s,
> > e.g. y(s)=sin(s) or something like that.
>
> If Y is only known as data, you will need to do one of two things:
>
> 1) Use TRAPZ or CUMTRAPZ.
> 2) Interpolate the Y data to obtain the value of the "function" at
> intermediate points. This can be dangerous if your data does not
> sufficiently represent your actual function:
>
> x = 0:10
> y = sin(x*pi).^2
> answer1 = integral(@(t) interp1(x, y, t), 0, 10)
> syms z
> answer2 = int(sin(z*pi)^2, 0, 10)
>
> Your numeric data makes your function "look like" the constant function y =
> 0 and so answer1 is very close to 0. The symbolic integration gives an
> answer2 of 5, which is correct. Evaluating y at more points (x = 0:0.1:10
> for example) would show the true form of the curve and gives an answer very
> close to 5.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Thank you very much for your discussion and sorry for getting back to you very late.
Actually, I don't know how to interpolate the data that's why I intended to use cumptrapz. However, there is no posibility to put the bounds of my integration in that function!!! I want to integrate from i*0.25 to t, and t from i*0.25 to (i+1)*0.25!!

Subject: Integration with variable limits

From: Torsten

Date: 15 Aug, 2013 12:20:08

Message: 10 of 18

"Sanaa" wrote in message <kuig3n$9d$1@newscl01ah.mathworks.com>...
> "Steven_Lord" <slord@mathworks.com> wrote in message <ktb57u$el8$1@newscl01ah.mathworks.com>...
> >
> >
> > "Torsten " <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message
> > news:ktad6k$gtn$1@newscl01ah.mathworks.com...
> > > "Torsten" wrote in message <ktacg3$fkj$1@newscl01ah.mathworks.com>...
> > >> "Sanaa" wrote in message <kt9bf4$56h$1@newscl01ah.mathworks.com>...
> > >> > "Torsten" wrote in message <kst5mn$h75$1@newscl01ah.mathworks.com>...
> > >> > > "Sanaa" wrote in message <kss3h5$50n$1@newscl01ah.mathworks.com>...
> > >> > > > "Torsten" wrote in message
> > >> > > > <ksrcel$2k$1@newscl01ah.mathworks.com>...
> > >> > > > > "Sanaa" wrote in message
> > >> > > > > <ksrbia$r0l$1@newscl01ah.mathworks.com>...
> > >> > > > > > Hi all,
> > >> > > > > > How to integrate a function within a loop and the limits of
> > >> > > > > > integration are variables?
> > >> > > > > > For instance, y(i)*(1-y(i)) is the function I wish to integrate
> > >> > > > > > from 0.25*i to t where t \in (0.25*i, 0.25*(i+1)).
> > >> > > > > > Any help is really appreciated.
> > >> > > > > > Sanaa Moussa
> > >> > > > >
> > >> > > > > I think y(i)*(1-y(i)) is just a real number, not a function,
> > >> > > > > isn't it ?
> > >> > > > >
> > >> > > > > Best wishes
> > >> > > > > Torsten.
> > >> > > >
> > >> > > > Thanks for replying. You are right, but I mean the function is
> > >> > > > y*(1-y) which I want to put it into a loop
> > >> > > > My code is
> > >> > > > itermax=300;min=itermax-9;% That is we plot from 291:300 i.e. 10
> > >> > > > values of x only.
> > >> > > > r=0.25;
> > >> > > > for rho=0:0.001:4
> > >> > > > %x0=0.1;
> > >> > > > x(1)=0.1;
> > >> > > > for i=1:itermax-1
> > >> > > > %t=linspace(i*r,(i+1)*r,itermax)
> > >> > > > y(i+1)=x(i);
> > >> > > > x(i+1)=x(i)+ rho*int(y(i)*(1-y(i)),i*r,(i+1)*r);
> > >> > > > end
> > >> > > > %fix(y)
> > >> > > > plot(rho*ones(10),x(min:itermax),'b.','linewidth',0.1)
> > >> > > > hold on
> > >> > > > end
> > >> > > > fsize=15;
> > >> > > > xlabel('\rho','FontSize',fsize)
> > >> > > > ylabel('\itx','FontSize',fsize)
> > >> > > > %title('r=0.25, \alpha=1','FontSize',fsize)
> > >> > > > hold off
> > >> > > > % print(gcf, '-djpeg', '-zbuffer', 'bif.png');
> > >> > > >
> > >> > > > I get the error
> > >> > > > Function 'int' is not defined for values of class 'double'.
> > >> > > > what does this mean and how to fix it please?
> > >> > > > Many thanks in advance.
> > >> > >
> > >> > > It means what I said before:
> > >> > > y(i)*(1-y(i)) is a scalar value and not a function.
> > >> > > So "int" does not know how to handle this because it expects a
> > >> > > function, not a scalar as its first input argument.
> > >> > >
> > >> > > Best wishes
> > >> > > Torsten.
> > >> >
> > >> > Sorry for not posing my question correctly. The function I want to
> > >> > integrate is
> > >> > y(s)*(1-y(s)) ds from (n*0.25) to t, and t/in(n*0.25, (n+1)*0.25).
> > >> > Do you have any idea on how to format my code above to solve my
> > >> > problem?
> > >> > Thanks a lot in advance
> > >>
> > >> int_{n*0.25}^{t} (y(s)*(1-y(s)) ds = (1/2*t^2 - 1/3*t^3) -
> > >> (1/2*(n*0.25)^2 - 1/3*(n*0.25)^3)
> > >> for t/in(n*0.25, (n+1)*0.25).
> > >> Is it that what you asked for ?
> > >>
> > >> Best wishes
> > >> Torsten.
> > >
> > > No sorry, this is wrong - I integrated s*(1-s).
> > > For integration, you will have to know the explicit form of y as a
> > > function of s,
> > > e.g. y(s)=sin(s) or something like that.
> >
> > If Y is only known as data, you will need to do one of two things:
> >
> > 1) Use TRAPZ or CUMTRAPZ.
> > 2) Interpolate the Y data to obtain the value of the "function" at
> > intermediate points. This can be dangerous if your data does not
> > sufficiently represent your actual function:
> >
> > x = 0:10
> > y = sin(x*pi).^2
> > answer1 = integral(@(t) interp1(x, y, t), 0, 10)
> > syms z
> > answer2 = int(sin(z*pi)^2, 0, 10)
> >
> > Your numeric data makes your function "look like" the constant function y =
> > 0 and so answer1 is very close to 0. The symbolic integration gives an
> > answer2 of 5, which is correct. Evaluating y at more points (x = 0:0.1:10
> > for example) would show the true form of the curve and gives an answer very
> > close to 5.
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > To contact Technical Support use the Contact Us link on
> > http://www.mathworks.com
>
> Thank you very much for your discussion and sorry for getting back to you very late.
> Actually, I don't know how to interpolate the data that's why I intended to use cumptrapz. However, there is no posibility to put the bounds of my integration in that function!!! I want to integrate from i*0.25 to t, and t from i*0.25 to (i+1)*0.25!!

Just for clarification:
How is your function given you want to integrate ?
1. By data points (x1,y1),(x2,y2),...,(xn,yn) you already calculated beforehand,
2. By a functional equation, e.g. f(x)=x^2,
3. By a differential equation dy/dx=y(x)*(1-y(x)),
4. ??
 
Best wishes
Torsten.

Subject: Integration with variable limits

From: Sanaa

Date: 15 Aug, 2013 13:46:09

Message: 11 of 18

"Torsten" wrote in message <kuih1o$c4g$1@newscl01ah.mathworks.com>...
> "Sanaa" wrote in message <kuig3n$9d$1@newscl01ah.mathworks.com>...
> > "Steven_Lord" <slord@mathworks.com> wrote in message <ktb57u$el8$1@newscl01ah.mathworks.com>...
> > >
> > >
> > > "Torsten " <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message
> > > news:ktad6k$gtn$1@newscl01ah.mathworks.com...
> > > > "Torsten" wrote in message <ktacg3$fkj$1@newscl01ah.mathworks.com>...
> > > >> "Sanaa" wrote in message <kt9bf4$56h$1@newscl01ah.mathworks.com>...
> > > >> > "Torsten" wrote in message <kst5mn$h75$1@newscl01ah.mathworks.com>...
> > > >> > > "Sanaa" wrote in message <kss3h5$50n$1@newscl01ah.mathworks.com>...
> > > >> > > > "Torsten" wrote in message
> > > >> > > > <ksrcel$2k$1@newscl01ah.mathworks.com>...
> > > >> > > > > "Sanaa" wrote in message
> > > >> > > > > <ksrbia$r0l$1@newscl01ah.mathworks.com>...
> > > >> > > > > > Hi all,
> > > >> > > > > > How to integrate a function within a loop and the limits of
> > > >> > > > > > integration are variables?
> > > >> > > > > > For instance, y(i)*(1-y(i)) is the function I wish to integrate
> > > >> > > > > > from 0.25*i to t where t \in (0.25*i, 0.25*(i+1)).
> > > >> > > > > > Any help is really appreciated.
> > > >> > > > > > Sanaa Moussa
> > > >> > > > >
> > > >> > > > > I think y(i)*(1-y(i)) is just a real number, not a function,
> > > >> > > > > isn't it ?
> > > >> > > > >
> > > >> > > > > Best wishes
> > > >> > > > > Torsten.
> > > >> > > >
> > > >> > > > Thanks for replying. You are right, but I mean the function is
> > > >> > > > y*(1-y) which I want to put it into a loop
> > > >> > > > My code is
> > > >> > > > itermax=300;min=itermax-9;% That is we plot from 291:300 i.e. 10
> > > >> > > > values of x only.
> > > >> > > > r=0.25;
> > > >> > > > for rho=0:0.001:4
> > > >> > > > %x0=0.1;
> > > >> > > > x(1)=0.1;
> > > >> > > > for i=1:itermax-1
> > > >> > > > %t=linspace(i*r,(i+1)*r,itermax)
> > > >> > > > y(i+1)=x(i);
> > > >> > > > x(i+1)=x(i)+ rho*int(y(i)*(1-y(i)),i*r,(i+1)*r);
> > > >> > > > end
> > > >> > > > %fix(y)
> > > >> > > > plot(rho*ones(10),x(min:itermax),'b.','linewidth',0.1)
> > > >> > > > hold on
> > > >> > > > end
> > > >> > > > fsize=15;
> > > >> > > > xlabel('\rho','FontSize',fsize)
> > > >> > > > ylabel('\itx','FontSize',fsize)
> > > >> > > > %title('r=0.25, \alpha=1','FontSize',fsize)
> > > >> > > > hold off
> > > >> > > > % print(gcf, '-djpeg', '-zbuffer', 'bif.png');
> > > >> > > >
> > > >> > > > I get the error
> > > >> > > > Function 'int' is not defined for values of class 'double'.
> > > >> > > > what does this mean and how to fix it please?
> > > >> > > > Many thanks in advance.
> > > >> > >
> > > >> > > It means what I said before:
> > > >> > > y(i)*(1-y(i)) is a scalar value and not a function.
> > > >> > > So "int" does not know how to handle this because it expects a
> > > >> > > function, not a scalar as its first input argument.
> > > >> > >
> > > >> > > Best wishes
> > > >> > > Torsten.
> > > >> >
> > > >> > Sorry for not posing my question correctly. The function I want to
> > > >> > integrate is
> > > >> > y(s)*(1-y(s)) ds from (n*0.25) to t, and t/in(n*0.25, (n+1)*0.25).
> > > >> > Do you have any idea on how to format my code above to solve my
> > > >> > problem?
> > > >> > Thanks a lot in advance
> > > >>
> > > >> int_{n*0.25}^{t} (y(s)*(1-y(s)) ds = (1/2*t^2 - 1/3*t^3) -
> > > >> (1/2*(n*0.25)^2 - 1/3*(n*0.25)^3)
> > > >> for t/in(n*0.25, (n+1)*0.25).
> > > >> Is it that what you asked for ?
> > > >>
> > > >> Best wishes
> > > >> Torsten.
> > > >
> > > > No sorry, this is wrong - I integrated s*(1-s).
> > > > For integration, you will have to know the explicit form of y as a
> > > > function of s,
> > > > e.g. y(s)=sin(s) or something like that.
> > >
> > > If Y is only known as data, you will need to do one of two things:
> > >
> > > 1) Use TRAPZ or CUMTRAPZ.
> > > 2) Interpolate the Y data to obtain the value of the "function" at
> > > intermediate points. This can be dangerous if your data does not
> > > sufficiently represent your actual function:
> > >
> > > x = 0:10
> > > y = sin(x*pi).^2
> > > answer1 = integral(@(t) interp1(x, y, t), 0, 10)
> > > syms z
> > > answer2 = int(sin(z*pi)^2, 0, 10)
> > >
> > > Your numeric data makes your function "look like" the constant function y =
> > > 0 and so answer1 is very close to 0. The symbolic integration gives an
> > > answer2 of 5, which is correct. Evaluating y at more points (x = 0:0.1:10
> > > for example) would show the true form of the curve and gives an answer very
> > > close to 5.
> > >
> > > --
> > > Steve Lord
> > > slord@mathworks.com
> > > To contact Technical Support use the Contact Us link on
> > > http://www.mathworks.com
> >
> > Thank you very much for your discussion and sorry for getting back to you very late.
> > Actually, I don't know how to interpolate the data that's why I intended to use cumptrapz. However, there is no posibility to put the bounds of my integration in that function!!! I want to integrate from i*0.25 to t, and t from i*0.25 to (i+1)*0.25!!
>
> Just for clarification:
> How is your function given you want to integrate ?
> 1. By data points (x1,y1),(x2,y2),...,(xn,yn) you already calculated beforehand,
> 2. By a functional equation, e.g. f(x)=x^2,
> 3. By a differential equation dy/dx=y(x)*(1-y(x)),
> 4. ??
>
> Best wishes
> Torsten.
Tanks a lot for replying. before I make a discretization process, I have the system:
y(t) = x(t-r),
dx/dt = f(y(t)), where f = rho*y(1-y).
After discretization and integration I have the system
y_(n+1)=x_n;
x_(n+1)= x_n + integration (f(y_(n+1)(s))) ds.

Subject: Integration with variable limits

From: Torsten

Date: 15 Aug, 2013 14:27:19

Message: 12 of 18

"Sanaa" wrote in message <kuim31$gkn$1@newscl01ah.mathworks.com>...
> "Torsten" wrote in message <kuih1o$c4g$1@newscl01ah.mathworks.com>...
> > "Sanaa" wrote in message <kuig3n$9d$1@newscl01ah.mathworks.com>...
> > > "Steven_Lord" <slord@mathworks.com> wrote in message <ktb57u$el8$1@newscl01ah.mathworks.com>...
> > > >
> > > >
> > > > "Torsten " <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message
> > > > news:ktad6k$gtn$1@newscl01ah.mathworks.com...
> > > > > "Torsten" wrote in message <ktacg3$fkj$1@newscl01ah.mathworks.com>...
> > > > >> "Sanaa" wrote in message <kt9bf4$56h$1@newscl01ah.mathworks.com>...
> > > > >> > "Torsten" wrote in message <kst5mn$h75$1@newscl01ah.mathworks.com>...
> > > > >> > > "Sanaa" wrote in message <kss3h5$50n$1@newscl01ah.mathworks.com>...
> > > > >> > > > "Torsten" wrote in message
> > > > >> > > > <ksrcel$2k$1@newscl01ah.mathworks.com>...
> > > > >> > > > > "Sanaa" wrote in message
> > > > >> > > > > <ksrbia$r0l$1@newscl01ah.mathworks.com>...
> > > > >> > > > > > Hi all,
> > > > >> > > > > > How to integrate a function within a loop and the limits of
> > > > >> > > > > > integration are variables?
> > > > >> > > > > > For instance, y(i)*(1-y(i)) is the function I wish to integrate
> > > > >> > > > > > from 0.25*i to t where t \in (0.25*i, 0.25*(i+1)).
> > > > >> > > > > > Any help is really appreciated.
> > > > >> > > > > > Sanaa Moussa
> > > > >> > > > >
> > > > >> > > > > I think y(i)*(1-y(i)) is just a real number, not a function,
> > > > >> > > > > isn't it ?
> > > > >> > > > >
> > > > >> > > > > Best wishes
> > > > >> > > > > Torsten.
> > > > >> > > >
> > > > >> > > > Thanks for replying. You are right, but I mean the function is
> > > > >> > > > y*(1-y) which I want to put it into a loop
> > > > >> > > > My code is
> > > > >> > > > itermax=300;min=itermax-9;% That is we plot from 291:300 i.e. 10
> > > > >> > > > values of x only.
> > > > >> > > > r=0.25;
> > > > >> > > > for rho=0:0.001:4
> > > > >> > > > %x0=0.1;
> > > > >> > > > x(1)=0.1;
> > > > >> > > > for i=1:itermax-1
> > > > >> > > > %t=linspace(i*r,(i+1)*r,itermax)
> > > > >> > > > y(i+1)=x(i);
> > > > >> > > > x(i+1)=x(i)+ rho*int(y(i)*(1-y(i)),i*r,(i+1)*r);
> > > > >> > > > end
> > > > >> > > > %fix(y)
> > > > >> > > > plot(rho*ones(10),x(min:itermax),'b.','linewidth',0.1)
> > > > >> > > > hold on
> > > > >> > > > end
> > > > >> > > > fsize=15;
> > > > >> > > > xlabel('\rho','FontSize',fsize)
> > > > >> > > > ylabel('\itx','FontSize',fsize)
> > > > >> > > > %title('r=0.25, \alpha=1','FontSize',fsize)
> > > > >> > > > hold off
> > > > >> > > > % print(gcf, '-djpeg', '-zbuffer', 'bif.png');
> > > > >> > > >
> > > > >> > > > I get the error
> > > > >> > > > Function 'int' is not defined for values of class 'double'.
> > > > >> > > > what does this mean and how to fix it please?
> > > > >> > > > Many thanks in advance.
> > > > >> > >
> > > > >> > > It means what I said before:
> > > > >> > > y(i)*(1-y(i)) is a scalar value and not a function.
> > > > >> > > So "int" does not know how to handle this because it expects a
> > > > >> > > function, not a scalar as its first input argument.
> > > > >> > >
> > > > >> > > Best wishes
> > > > >> > > Torsten.
> > > > >> >
> > > > >> > Sorry for not posing my question correctly. The function I want to
> > > > >> > integrate is
> > > > >> > y(s)*(1-y(s)) ds from (n*0.25) to t, and t/in(n*0.25, (n+1)*0.25).
> > > > >> > Do you have any idea on how to format my code above to solve my
> > > > >> > problem?
> > > > >> > Thanks a lot in advance
> > > > >>
> > > > >> int_{n*0.25}^{t} (y(s)*(1-y(s)) ds = (1/2*t^2 - 1/3*t^3) -
> > > > >> (1/2*(n*0.25)^2 - 1/3*(n*0.25)^3)
> > > > >> for t/in(n*0.25, (n+1)*0.25).
> > > > >> Is it that what you asked for ?
> > > > >>
> > > > >> Best wishes
> > > > >> Torsten.
> > > > >
> > > > > No sorry, this is wrong - I integrated s*(1-s).
> > > > > For integration, you will have to know the explicit form of y as a
> > > > > function of s,
> > > > > e.g. y(s)=sin(s) or something like that.
> > > >
> > > > If Y is only known as data, you will need to do one of two things:
> > > >
> > > > 1) Use TRAPZ or CUMTRAPZ.
> > > > 2) Interpolate the Y data to obtain the value of the "function" at
> > > > intermediate points. This can be dangerous if your data does not
> > > > sufficiently represent your actual function:
> > > >
> > > > x = 0:10
> > > > y = sin(x*pi).^2
> > > > answer1 = integral(@(t) interp1(x, y, t), 0, 10)
> > > > syms z
> > > > answer2 = int(sin(z*pi)^2, 0, 10)
> > > >
> > > > Your numeric data makes your function "look like" the constant function y =
> > > > 0 and so answer1 is very close to 0. The symbolic integration gives an
> > > > answer2 of 5, which is correct. Evaluating y at more points (x = 0:0.1:10
> > > > for example) would show the true form of the curve and gives an answer very
> > > > close to 5.
> > > >
> > > > --
> > > > Steve Lord
> > > > slord@mathworks.com
> > > > To contact Technical Support use the Contact Us link on
> > > > http://www.mathworks.com
> > >
> > > Thank you very much for your discussion and sorry for getting back to you very late.
> > > Actually, I don't know how to interpolate the data that's why I intended to use cumptrapz. However, there is no posibility to put the bounds of my integration in that function!!! I want to integrate from i*0.25 to t, and t from i*0.25 to (i+1)*0.25!!
> >
> > Just for clarification:
> > How is your function given you want to integrate ?
> > 1. By data points (x1,y1),(x2,y2),...,(xn,yn) you already calculated beforehand,
> > 2. By a functional equation, e.g. f(x)=x^2,
> > 3. By a differential equation dy/dx=y(x)*(1-y(x)),
> > 4. ??
> >
> > Best wishes
> > Torsten.
> Tanks a lot for replying. before I make a discretization process, I have the system:
> y(t) = x(t-r),
> dx/dt = f(y(t)), where f = rho*y(1-y).
> After discretization and integration I have the system
> y_(n+1)=x_n;
> x_(n+1)= x_n + integration (f(y_(n+1)(s))) ds.

So your final aim is to solve the delay differential equation
dx(t)/dt = rho*x(t-r)*(1-x(t-r))
with x given on an interval of length r at the beginning, r and rho constant over time ?

Best wishes
Torsten.

 

Subject: Integration with variable limits

From: Sanaa

Date: 16 Aug, 2013 12:58:07

Message: 13 of 18


> > y_(n+1)=x_n;
> > x_(n+1)= x_n + integration (f(y_(n+1)(s))) ds.
>
> So your final aim is to solve the delay differential equation
> dx(t)/dt = rho*x(t-r)*(1-x(t-r))
> with x given on an interval of length r at the beginning, r and rho constant over time ?
>
> Best wishes
> Torsten.

No. I don't want to solve the original delay differential equations, I wish to solve the discrete system after a certain discretization method applied to it.
Thanks a lot in advance.

Subject: Integration with variable limits

From: Torsten

Date: 16 Aug, 2013 13:23:07

Message: 14 of 18

"Sanaa" wrote in message <kul7kv$a10$1@newscl01ah.mathworks.com>...
>
> > > y_(n+1)=x_n;
> > > x_(n+1)= x_n + integration (f(y_(n+1)(s))) ds.
> >
> > So your final aim is to solve the delay differential equation
> > dx(t)/dt = rho*x(t-r)*(1-x(t-r))
> > with x given on an interval of length r at the beginning, r and rho constant over time ?
> >
> > Best wishes
> > Torsten.
>
> No. I don't want to solve the original delay differential equations, I wish to solve the discrete system after a certain discretization method applied to it.
> Thanks a lot in advance.

But if the discretrization method is given, you are also given how the above integral is approximated.
Or are you _searching_ for an adequate discretization method for the delay differential equation ?

Best wishes
Torsten.

Subject: Integration with variable limits

From: Sanaa

Date: 16 Aug, 2013 13:59:07

Message: 15 of 18

"Torsten" wrote in message <kul93r$t5q$1@newscl01ah.mathworks.com>...
> "Sanaa" wrote in message <kul7kv$a10$1@newscl01ah.mathworks.com>...
> >
> > > > y_(n+1)=x_n;
> > > > x_(n+1)= x_n + integration (f(y_(n+1)(s))) ds.
> > >
> > > So your final aim is to solve the delay differential equation
> > > dx(t)/dt = rho*x(t-r)*(1-x(t-r))
> > > with x given on an interval of length r at the beginning, r and rho constant over time ?
> > >
> > > Best wishes
> > > Torsten.
> >
> > No. I don't want to solve the original delay differential equations, I wish to solve the discrete system after a certain discretization method applied to it.
> > Thanks a lot in advance.
>
> But if the discretrization method is given, you are also given how the above integral is approximated.
> Or are you _searching_ for an adequate discretization method for the delay differential equation ?
>
> Best wishes
> Torsten.

Thanks once again for your kind reply. The discretization method here is very simple, the step methods, I am not searching for the method, i just don't know how to approximate the integral. I have read about trapz and cumtrapz but my only problem now is the variable limits of integration. Using trapz or cumptrapz don't allow me to specify the variable limits; I am really confused.

Subject: Integration with variable limits

From: Torsten

Date: 16 Aug, 2013 14:26:07

Message: 16 of 18

"Sanaa" wrote in message <kulb7b$qrm$1@newscl01ah.mathworks.com>...
> "Torsten" wrote in message <kul93r$t5q$1@newscl01ah.mathworks.com>...
> > "Sanaa" wrote in message <kul7kv$a10$1@newscl01ah.mathworks.com>...
> > >
> > > > > y_(n+1)=x_n;
> > > > > x_(n+1)= x_n + integration (f(y_(n+1)(s))) ds.
> > > >
> > > > So your final aim is to solve the delay differential equation
> > > > dx(t)/dt = rho*x(t-r)*(1-x(t-r))
> > > > with x given on an interval of length r at the beginning, r and rho constant over time ?
> > > >
> > > > Best wishes
> > > > Torsten.
> > >
> > > No. I don't want to solve the original delay differential equations, I wish to solve the discrete system after a certain discretization method applied to it.
> > > Thanks a lot in advance.
> >
> > But if the discretrization method is given, you are also given how the above integral is approximated.
> > Or are you _searching_ for an adequate discretization method for the delay differential equation ?
> >
> > Best wishes
> > Torsten.
>
> Thanks once again for your kind reply. The discretization method here is very simple, the step methods, I am not searching for the method, i just don't know how to approximate the integral. I have read about trapz and cumtrapz but my only problem now is the variable limits of integration. Using trapz or cumptrapz don't allow me to specify the variable limits; I am really confused.

First take care that your step length divides r - otherwise you will have trouble.
Say you have the delay differential equation from above
dx/dt = rho*x(t-r)*(1-x(t-r))
with x given on the interval [-r,0].
Now take a step dt such that dt divides r (say n*dt=r)
x(t+dt)-x(t) = integral_{t'=t}^{t'=t+dt} (rho*x(t'-r)*(1-x(t'-r)) dt')
The integral can be approximated by the trapezoidal rule:
integral_{t'=t}^{t'=t+dt} (rho*x(t'-r)*(1-x(t'-r)) dt') ~
dt*(rho*x(t+dt-r)*(1-x(t+dt-r))+rho*x(t-r)*(1-x(t-r)))/2.
Thus your recursion reads
x(t+dt)=x(t)+dt/2*rho*(x(t+dt-r)*(1-x(t+dt-r))+x(t-r)*(1-x(t-r)))
Inserting n*dt=r gives
x(t+dt)=x(t)+dt/2*rho*(x(t-(n-1)*dt)*(1-x(t-(n-1)*dt))+x(t-n*dt)*(1-x(t-n*dt)))
or as discrete recursion
x_(k+1)=x_k+dt/2*rho*(x_(k-(n-1))*(1-x_(k-(n-1)))+x_(k-n)*(1-x_(k-n)))
for k=0,1,2,...
The values x(-(n-1)),x(-(n-2)),...,x(0) are given by the initial condition on the interval
[-r,0].
Does this help ?

Best wishes
Torsten.

Subject: Integration with variable limits

From: Sanaa

Date: 16 Aug, 2013 17:07:08

Message: 17 of 18

"Torsten" wrote in message <kulcpv$jj9$1@newscl01ah.mathworks.com>...
> "Sanaa" wrote in message <kulb7b$qrm$1@newscl01ah.mathworks.com>...
> > "Torsten" wrote in message <kul93r$t5q$1@newscl01ah.mathworks.com>...
> > > "Sanaa" wrote in message <kul7kv$a10$1@newscl01ah.mathworks.com>...
> > > >
> > > > > > y_(n+1)=x_n;
> > > > > > x_(n+1)= x_n + integration (f(y_(n+1)(s))) ds.
> > > > >
> > > > > So your final aim is to solve the delay differential equation
> > > > > dx(t)/dt = rho*x(t-r)*(1-x(t-r))
> > > > > with x given on an interval of length r at the beginning, r and rho constant over time ?
> > > > >
> > > > > Best wishes
> > > > > Torsten.
> > > >
> > > > No. I don't want to solve the original delay differential equations, I wish to solve the discrete system after a certain discretization method applied to it.
> > > > Thanks a lot in advance.
> > >
> > > But if the discretrization method is given, you are also given how the above integral is approximated.
> > > Or are you _searching_ for an adequate discretization method for the delay differential equation ?
> > >
> > > Best wishes
> > > Torsten.
> >
> > Thanks once again for your kind reply. The discretization method here is very simple, the step methods, I am not searching for the method, i just don't know how to approximate the integral. I have read about trapz and cumtrapz but my only problem now is the variable limits of integration. Using trapz or cumptrapz don't allow me to specify the variable limits; I am really confused.
>
> First take care that your step length divides r - otherwise you will have trouble.
> Say you have the delay differential equation from above
> dx/dt = rho*x(t-r)*(1-x(t-r))
> with x given on the interval [-r,0].
> Now take a step dt such that dt divides r (say n*dt=r)
> x(t+dt)-x(t) = integral_{t'=t}^{t'=t+dt} (rho*x(t'-r)*(1-x(t'-r)) dt')
> The integral can be approximated by the trapezoidal rule:
> integral_{t'=t}^{t'=t+dt} (rho*x(t'-r)*(1-x(t'-r)) dt') ~
> dt*(rho*x(t+dt-r)*(1-x(t+dt-r))+rho*x(t-r)*(1-x(t-r)))/2.
> Thus your recursion reads
> x(t+dt)=x(t)+dt/2*rho*(x(t+dt-r)*(1-x(t+dt-r))+x(t-r)*(1-x(t-r)))
> Inserting n*dt=r gives
> x(t+dt)=x(t)+dt/2*rho*(x(t-(n-1)*dt)*(1-x(t-(n-1)*dt))+x(t-n*dt)*(1-x(t-n*dt)))
> or as discrete recursion
> x_(k+1)=x_k+dt/2*rho*(x_(k-(n-1))*(1-x_(k-(n-1)))+x_(k-n)*(1-x_(k-n)))
> for k=0,1,2,...
> The values x(-(n-1)),x(-(n-2)),...,x(0) are given by the initial condition on the interval
> [-r,0].
> Does this help ?
>
> Best wishes
> Torsten.

I am sorry for any inconvenience. I want the symbolic integration of the function (whatever the original problem is and whatever the dis. method is); that is,
y_(n+1)=x_n;
 x_(n+1)= x_n + integration (f(y_(n+1)(s))) ds.
where f(y(t)) = rho*y(t)*(1-y(t)).
From the discussion above, I think this task is very difficult.
Anyway, thank you very much for your help.

Subject: Integration with variable limits

From: Torsten

Date: 20 Aug, 2013 14:20:28

Message: 18 of 18

"Sanaa" wrote in message <kulm7s$kes$1@newscl01ah.mathworks.com>...
> "Torsten" wrote in message <kulcpv$jj9$1@newscl01ah.mathworks.com>...
> > "Sanaa" wrote in message <kulb7b$qrm$1@newscl01ah.mathworks.com>...
> > > "Torsten" wrote in message <kul93r$t5q$1@newscl01ah.mathworks.com>...
> > > > "Sanaa" wrote in message <kul7kv$a10$1@newscl01ah.mathworks.com>...
> > > > >
> > > > > > > y_(n+1)=x_n;
> > > > > > > x_(n+1)= x_n + integration (f(y_(n+1)(s))) ds.
> > > > > >
> > > > > > So your final aim is to solve the delay differential equation
> > > > > > dx(t)/dt = rho*x(t-r)*(1-x(t-r))
> > > > > > with x given on an interval of length r at the beginning, r and rho constant over time ?
> > > > > >
> > > > > > Best wishes
> > > > > > Torsten.
> > > > >
> > > > > No. I don't want to solve the original delay differential equations, I wish to solve the discrete system after a certain discretization method applied to it.
> > > > > Thanks a lot in advance.
> > > >
> > > > But if the discretrization method is given, you are also given how the above integral is approximated.
> > > > Or are you _searching_ for an adequate discretization method for the delay differential equation ?
> > > >
> > > > Best wishes
> > > > Torsten.
> > >
> > > Thanks once again for your kind reply. The discretization method here is very simple, the step methods, I am not searching for the method, i just don't know how to approximate the integral. I have read about trapz and cumtrapz but my only problem now is the variable limits of integration. Using trapz or cumptrapz don't allow me to specify the variable limits; I am really confused.
> >
> > First take care that your step length divides r - otherwise you will have trouble.
> > Say you have the delay differential equation from above
> > dx/dt = rho*x(t-r)*(1-x(t-r))
> > with x given on the interval [-r,0].
> > Now take a step dt such that dt divides r (say n*dt=r)
> > x(t+dt)-x(t) = integral_{t'=t}^{t'=t+dt} (rho*x(t'-r)*(1-x(t'-r)) dt')
> > The integral can be approximated by the trapezoidal rule:
> > integral_{t'=t}^{t'=t+dt} (rho*x(t'-r)*(1-x(t'-r)) dt') ~
> > dt*(rho*x(t+dt-r)*(1-x(t+dt-r))+rho*x(t-r)*(1-x(t-r)))/2.
> > Thus your recursion reads
> > x(t+dt)=x(t)+dt/2*rho*(x(t+dt-r)*(1-x(t+dt-r))+x(t-r)*(1-x(t-r)))
> > Inserting n*dt=r gives
> > x(t+dt)=x(t)+dt/2*rho*(x(t-(n-1)*dt)*(1-x(t-(n-1)*dt))+x(t-n*dt)*(1-x(t-n*dt)))
> > or as discrete recursion
> > x_(k+1)=x_k+dt/2*rho*(x_(k-(n-1))*(1-x_(k-(n-1)))+x_(k-n)*(1-x_(k-n)))
> > for k=0,1,2,...
> > The values x(-(n-1)),x(-(n-2)),...,x(0) are given by the initial condition on the interval
> > [-r,0].
> > Does this help ?
> >
> > Best wishes
> > Torsten.
>
> I am sorry for any inconvenience. I want the symbolic integration of the function (whatever the original problem is and whatever the dis. method is); that is,
> y_(n+1)=x_n;
> x_(n+1)= x_n + integration (f(y_(n+1)(s))) ds.
> where f(y(t)) = rho*y(t)*(1-y(t)).
> From the discussion above, I think this task is very difficult.
> Anyway, thank you very much for your help.

Let's forget about delay differential equations for a moment and say you want
to solve the differential equation
y'(t)=f(t,y(t))
y(t0)=y0.
Say you have the solution at times t0 < t1 < t2 < ... < tn
and you want to integrate up to tn+dt.
Then you integrate both sides of
y'(t)=f(t,y(t))
in the limits from tn to tn+dt and get
y(tn+dt)-y(tn) = integral_{t'=tn}^{t'=tn+dt} f(t',y(t')) dt'.
Since you only know approximations for y in discrete points t0,t1,...,tn,
you can't use "int" or any symbolic integrator to integrate the right-hand side.
But there are methods which use the known discrete values (t0,y(t0)),...,(tn,y(tn))
to approximate the integral.
The simplest of these is to replace f(t,y(t)) on the interval [tn;tn+dt] by a constant
function:
f(t,y(t))=f(tn,y(tn)) for all t in [tn;tn+dt] or
f(t,y(t))=f(tn+dt,y(tn+dt)) for all t in [tn;tn+dt].
From the first approximation, the resulting method is the explicit Euler-method,
from the second approximatio, the resuting method is the implicit Euler method.
Or you can approximate the integral by a linear function ; this results in the
trapezoidal rule:
f(t,y(t))=f(tn,y(tn))+(t-tn)/dt*(f(tn+dt,y(tn+dt))-f(tn,y(tn))).
That's the rule I suggested above.
So there is no way in your case to "integate the function symbolically" since you don't know an explicit expression for the function. You have to work with the discrete known function values (t0,y(t0)),...,(tn,y(tn)) and find an approximation for the integral from these discrete values.

Best wishes
Torsten.

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