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:
Using the ODE solver in a FOR loop

Subject: Using the ODE solver in a FOR loop

From: Meiyour

Date: 15 Jul, 2013 13:02:10

Message: 1 of 12

Hi. I need some help with the ODE15 solver.

I am trying to use the solver four coupled partial differential equations, using the method of lines. However, i need to include the solver in a for loop, but every attemp i've made so far has proved abortive.

How do i gi about this? Need something like:

For i=1:n
  u1( t(i), y1(i) ) = ode15(..........);
  u2( t(i), y2(i) ) = ode15(...........);
end

Ant help would be greatly appreciated.

Subject: Using the ODE solver in a FOR loop

From: Torsten

Date: 15 Jul, 2013 13:46:08

Message: 2 of 12

"Meiyour" wrote in message <ks0rsi$3k3$1@newscl01ah.mathworks.com>...
> Hi. I need some help with the ODE15 solver.
>
> I am trying to use the solver four coupled partial differential equations, using the method of lines. However, i need to include the solver in a for loop, but every attemp i've made so far has proved abortive.
>
> How do i gi about this? Need something like:
>
> For i=1:n
> u1( t(i), y1(i) ) = ode15(..........);
> u2( t(i), y2(i) ) = ode15(...........);
> end
>
> Ant help would be greatly appreciated.

What are you trying to do in the above loop ?

Best wishes
Torsten.

Subject: Using the ODE solver in a FOR loop

From: Meiyour

Date: 15 Jul, 2013 15:07:18

Message: 3 of 12

Actually, i am trying to solve 4 coupled partial differential equations by the method of lines. I have discretized the space derivative, leaving me with 4 ODEs to solve.

However, i am having problems defining the four ODEs within the same for loop.
The ODE15s loops through the time elements defined. I want to incorporate a for loop which makes the code iterate through the discretized space elements too.

Basically, i need to use the method of lines for four coupled partial differential equations.

Hope i'm making a bit of sense. Would really appreciate your help.

 "Torsten" wrote in message <ks0uf0$ba9$1@newscl01ah.mathworks.com>...
> "Meiyour" wrote in message <ks0rsi$3k3$1@newscl01ah.mathworks.com>...
> > Hi. I need some help with the ODE15 solver.
> >
> > I am trying to use the solver four coupled partial differential equations, using the method of lines. However, i need to include the solver in a for loop, but every attemp i've made so far has proved abortive.
> >
> > How do i gi about this? Need something like:
> >
> > For i=1:n
> > u1( t(i), y1(i) ) = ode15(..........);
> > u2( t(i), y2(i) ) = ode15(...........);
> > end
> >
> > Ant help would be greatly appreciated.
>
> What are you trying to do in the above loop ?
>
> Best wishes
> Torsten.

Subject: Using the ODE solver in a FOR loop

From: Marc

Date: 16 Jul, 2013 05:09:11

Message: 4 of 12

"Meiyour" wrote in message <ks1376$ot5$1@newscl01ah.mathworks.com>...
> Actually, i am trying to solve 4 coupled partial differential equations by the method of lines. I have discretized the space derivative, leaving me with 4 ODEs to solve.
>
> However, i am having problems defining the four ODEs within the same for loop.
> The ODE15s loops through the time elements defined. I want to incorporate a for loop which makes the code iterate through the discretized space elements too.
>
> Basically, i need to use the method of lines for four coupled partial differential equations.
>
> Hope i'm making a bit of sense. Would really appreciate your help.
>
> "Torsten" wrote in message <ks0uf0$ba9$1@newscl01ah.mathworks.com>...
> > "Meiyour" wrote in message <ks0rsi$3k3$1@newscl01ah.mathworks.com>...
> > > Hi. I need some help with the ODE15 solver.
> > >
> > > I am trying to use the solver four coupled partial differential equations, using the method of lines. However, i need to include the solver in a for loop, but every attemp i've made so far has proved abortive.
> > >
> > > How do i gi about this? Need something like:
> > >
> > > For i=1:n
> > > u1( t(i), y1(i) ) = ode15(..........);
> > > u2( t(i), y2(i) ) = ode15(...........);
> > > end
> > >
> > > Ant help would be greatly appreciated.
> >
> > What are you trying to do in the above loop ?
> >
> > Best wishes

See Shampine, et al... for Matlab AND MOL. If you set up your function correctly, there should be no need to use a for loop. By setting the time interval, ODE15s is smart enough to find the t and y answers for that interval. If for some reason you think ode15s runs into some issue within that time interval, using a for loop to break up the time span may be appropriate.

Albeit, I have rarely seen this to be the case.

Otherwise, you will need to provide a bit more detail regarding your system and any code that you have attempted for us to be more helpful

Subject: Using the ODE solver in a FOR loop

From: Torsten

Date: 16 Jul, 2013 06:17:28

Message: 5 of 12

"Meiyour" wrote in message <ks1376$ot5$1@newscl01ah.mathworks.com>...
> Actually, i am trying to solve 4 coupled partial differential equations by the method of lines. I have discretized the space derivative, leaving me with 4 ODEs to solve.
>
> However, i am having problems defining the four ODEs within the same for loop.
> The ODE15s loops through the time elements defined. I want to incorporate a for loop which makes the code iterate through the discretized space elements too.
>
> Basically, i need to use the method of lines for four coupled partial differential equations.
>
> Hope i'm making a bit of sense. Would really appreciate your help.
>
> "Torsten" wrote in message <ks0uf0$ba9$1@newscl01ah.mathworks.com>...
> > "Meiyour" wrote in message <ks0rsi$3k3$1@newscl01ah.mathworks.com>...
> > > Hi. I need some help with the ODE15 solver.
> > >
> > > I am trying to use the solver four coupled partial differential equations, using the method of lines. However, i need to include the solver in a for loop, but every attemp i've made so far has proved abortive.
> > >
> > > How do i gi about this? Need something like:
> > >
> > > For i=1:n
> > > u1( t(i), y1(i) ) = ode15(..........);
> > > u2( t(i), y2(i) ) = ode15(...........);
> > > end
> > >
> > > Ant help would be greatly appreciated.
> >
> > What are you trying to do in the above loop ?
> >
> > Best wishes
> > Torsten.

As Marc already said, you should solve the four ODEs per grid point _simultaneously_ , not sequentially. This will make the solution process much more stable and fast.

Best wishes
Torsten.

Subject: Using the ODE solver in a FOR loop

From: Meiyour

Date: 17 Jul, 2013 07:26:12

Message: 6 of 12

I am trying to solve the coupled equations
dx1/dt + dx1/dz = s1;
dx2/dt + dx2/dz = s2;
dx3/dt + dx3/dz = s3;
dx4/dt + dx4/dz = s4
where s1-s4 = f(x1,x2). Discretization of the equations (distance) in m steps gives
dxn(i) /dt = s1 - (xn(i)-xn(i-1))/delz where n=1-4, i = 1-m

Need to solve the equations for various times and distances. Shampine does not incorporate a source term (very complex one at that).

I have attempted to solve all the equations simultaneously, giving me 4m equations in total. However, i have struggled to implement this on MATLAB.

Any advice you can give me? Thanks.

Subject: Using the ODE solver in a FOR loop

From: Torsten

Date: 17 Jul, 2013 09:31:09

Message: 7 of 12

"Meiyour" wrote in message <ks5guk$25e$1@newscl01ah.mathworks.com>...
> I am trying to solve the coupled equations
> dx1/dt + dx1/dz = s1;
> dx2/dt + dx2/dz = s2;
> dx3/dt + dx3/dz = s3;
> dx4/dt + dx4/dz = s4
> where s1-s4 = f(x1,x2). Discretization of the equations (distance) in m steps gives
> dxn(i) /dt = s1 - (xn(i)-xn(i-1))/delz where n=1-4, i = 1-m
>
> Need to solve the equations for various times and distances. Shampine does not incorporate a source term (very complex one at that).
>
> I have attempted to solve all the equations simultaneously, giving me 4m equations in total. However, i have struggled to implement this on MATLAB.
>
> Any advice you can give me? Thanks.

dy=myfun(t,y)
x1(1:m)=y(1:m);
x2(1:m)=y(m+1:2*m);
x3(1:m)=y(2*m+1:3*m);
x4(1:m)=y(3*m+1:4*m);
% Include here how s1,s2,s3,s4 are calculated from x1 and x2.
dx1(1)=0.0; % assumes boundary value of x1 does not change with time
dx1(2:m)=s1(2:m)-(x1(2:m)-x1(1:m-1))/delz;
dx2(1)=0.0; % assumes boundary value of x2 does not change with time
dx2(2:m)=s2(2:m)-(x2(2:m)-x2(1:m-1))/delz;
dx3(1)=0.0; % assumes boundary value of x3 does not change with time
dx3(2:m)=s3(2:m)-(x3(2:m)-x2(1:m-1))/delz;
dx4(1)=0.0; % assumes boundary value of x4 does not change with time
dx4(2:m)=s4(2:m)-(x4(2:m)-x4(1:m-1))/delz;
dy(1:m,1)=dx1(1:m);
dy(m+1:2*m,1)=dx2(1:m);
dy(2*m+1:3*m,1)=dx3(1:m);
dy(3*m+1;4*m,1)=dx4(1:m);

Best wishes
Torsten.

Subject: Using the ODE solver in a FOR loop

From: Torsten

Date: 17 Jul, 2013 11:55:07

Message: 8 of 12

"Meiyour" wrote in message <ks5guk$25e$1@newscl01ah.mathworks.com>...
> I am trying to solve the coupled equations
> dx1/dt + dx1/dz = s1;
> dx2/dt + dx2/dz = s2;
> dx3/dt + dx3/dz = s3;
> dx4/dt + dx4/dz = s4
> where s1-s4 = f(x1,x2). Discretization of the equations (distance) in m steps gives
> dxn(i) /dt = s1 - (xn(i)-xn(i-1))/delz where n=1-4, i = 1-m
>
> Need to solve the equations for various times and distances. Shampine does not incorporate a source term (very complex one at that).
>
> I have attempted to solve all the equations simultaneously, giving me 4m equations in total. However, i have struggled to implement this on MATLAB.
>
> Any advice you can give me? Thanks.

If you have access to MATLAB's NAG-toolbox, use
http://www.google.de/url?sa=t&rct=j&q=&esrc=s&frm=1&source=web&cd=1&ved=0CDEQFjAA&url=http%3A%2F%2Fwww.nag.co.uk%2Fnumeric%2Fmb%2Fmanual_21_1%2Fpdf%2Fd03%2Fd03pf.pdf&ei=U4XmUee1EoPi4QSMi4DICA&usg=AFQjCNGzbvIjmnqKjEOxIqS04Mb8cpSo5w&sig2=XozQAIp1WIO3E3wgNFAB7Q
instead of your 1st-order scheme for better precision of the results.

Best wishes
Torsten.

Subject: Using the ODE solver in a FOR loop

From: Meiyour

Date: 17 Jul, 2013 20:13:12

Message: 9 of 12

Thanks for your reply, which is similar to what i've actually done. However, i have two questions:

1)How do we define the initial conditions in the code?
2)What do the last four lines of your code do?

Thanks.

Subject: Using the ODE solver in a FOR loop

From: Marc

Date: 18 Jul, 2013 03:18:10

Message: 10 of 12

"Meiyour" wrote in message <ks6tsn$t8a$1@newscl01ah.mathworks.com>...
> Thanks for your reply, which is similar to what i've actually done. However, i have two questions:
>
> 1)How do we define the initial conditions in the code?
> 2)What do the last four lines of your code do?
>
> Thanks.

You set your initial conditions at t = 0. If your doing something with temperature, what is the temperature along your x and y axis? But I imagine you already know that.

The last four lines set up your system of discretized odes. Try it. Torsten set it up very nice as far as I can tell.

As for Shampine, yes those examples may be "simpler" but for MOL and Matlab, I have not found a better introduction.

This stuff is not simple. I commend you on you perseverance.

Subject: Using the ODE solver in a FOR loop

From: Torsten

Date: 18 Jul, 2013 06:27:11

Message: 11 of 12

"Meiyour" wrote in message <ks6tsn$t8a$1@newscl01ah.mathworks.com>...
> Thanks for your reply, which is similar to what i've actually done. However, i have two questions:
>
> 1)How do we define the initial conditions in the code?

They are set before calling the integrator.
Take a look at the examples under
http://www.mathworks.de/de/help/matlab/ref/ode45.html

It should go something like
x10(1:m)=something;
x20(1:m)=something;
x30(1:m)=something;
x40(1:m)=something;
y0(1,1:m)=x10(1:m)
y0(1,m+1:2*m)=x20(1:m);
y0(1,2*m+1:3*m)=x30(1:m);
y0(1,3*m+1:4*m)=x40(1:m);
[T,Y] = ode45(@myfun,tspan,y0);
for your case.

> 2)What do the last four lines of your code do?
>

The solution variables are handed to "myfun" in one single vector
and their derivatives with respect to time also have to be returned
in one single vector.
So if you have 4 solution variables, it is convenient to first split the solution vector
in four local variable vectors (x1,x2,x3 and x4), calculate the time derivatives of the local variables (dx1,dx2,dx3 and dx4) and finally recollect the time derivatives in one
single vector (dy).
   
> Thanks.

Best wishes
Torsten.

Subject: Using the ODE solver in a FOR loop

From: Meiyour

Date: 18 Jul, 2013 07:44:11

Message: 12 of 12

Thanks a lot for your help guys.

This was exactly how i felt the bcs should be defined. However, i felt matlab might have read it as y50, not an initial condition, which was why i asked the first question. i'm alright with that.

The second part i still don't understand. However, i will not bother you more over the issue, will just read up on it.

Thanks again. You've been a lot of help!

Regards,
Meiyour

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