MATLAB Answers

running MATLAB ode45 back-to-back

43 views (last 30 days)
Taehun Kim
Taehun Kim on 18 Jan 2021
Commented: Taehun Kim on 18 Jan 2021
Hi All,
I am curious what happens with the first solution point when we run ode45 to integrate a right hand side function. The first state solution that is returned, is it the same as the initial condition? I would assume so because the associated time point for the first solution always seems to be a zero.
Next, suppose I am to run ode45 back-to-back because a physical phenomena abruptly changes. e.g. closing a valve in gas filled tank. In this case, a final solution for the previous ode integration becomes the initial condition for the next ode integration, i.e. successive iteration. Now, for the sake of continuity in time, I want to combine the solutions from two different ode integrations. What is the best way to do this? Should we remove the initial solution from the second integration and combine with the first solution? Also, for the corresponding time, should we consider the first solution time point of the second iteration to be exactly the same as the ending time point of the first integration?
Thank you so much!
-Taehun

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 18 Jan 2021
The first state solution that is returned, is it the same as the initial condition?
In my experience, yes. Sometimes it is the only solution if there are problems with the code and the other values are all not finite (NaN or ±Inf).
‘Now, for the sake of continuity in time, I want to combine the solutions from two different ode integrations. What is the best way to do this?
I vertically concatenate the time vectors, and vertiocally concatenate the integrated solution matrices.
Should we remove the initial solution from the second integration and combine with the first solution?
I generally do not, since the last row of the preceding integration likely overplots the first row of the next integration. It probably does not make any difference.
Also, for the corresponding time, should we consider the first solution time point of the second iteration to be exactly the same as the ending time point of the first integration?
It most likely will be. You can always check to be sure.
.

  2 Comments

Taehun Kim
Taehun Kim on 18 Jan 2021
Dear Star Strider,
Thank you so much for sharing your experience with me and the community! This is my second post to the community and I am constantly amazed how expedient way this has been as people really can provide quality answers to the posed questions fairly promptly...
I guess, I would heed your advice and try to see if I can get the result that I am looking for...
Thank you once again for your help! :)
Sincerely,
Taehun
Star Strider
Star Strider on 18 Jan 2021
As always, my pleasure!

Sign in to comment.

More Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 18 Jan 2021
Well if you have a problem like this (improvising for simplicity)
ode1 = @(t,y,alpha) alpha*y; % exponential growth
t_span1 = [0,3]
t_span2 = [3,12];
y0 = 1;
[t1,y1] = ode45(@(t,y) ode1(t,y,1),t_span1,y0);
[t2,y2] = ode45(@(t,y) ode1(t,y,-1/2),t_span2,y1(end));
t_both = [t1;t2];
y_both = [y1;y2];
You should be fine, if you want you can use unique to remove identical rows:
t_y = unique([t_both,y_both],'rows');
to get rid of the duplicate points. Or you can just remove either the last of the first or the first of the second outputs when concatenating.
HTH

  1 Comment

Taehun Kim
Taehun Kim on 18 Jan 2021
Dear Bjorn,
Thanks for your quick improvisation with the example that you provided. This example really does help me to better appreciate the special situation that I am currently delaing with!
Many many thanks for your help in this matter!
This means much to me!
Sincerely,
Taehun Kim

Sign in to comment.

Tags

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!