ode in a for loop updating initial conditions
Show older comments
I created the files in attached, main and @greitzer (function). In main I call the function in @greitzer.
Now I would like to put the ode in a for loop to solve the problem for each values of giri.
I'd like the first loop to be from:
t0(1)=0
to the second value of time_rpm(i)
time_rpm(2)
with initial condition y1(1)=0 and y2(0).
Now the second loop, for the second value of giri, so
giri(2)
should be in the interval
t0(2)=time_rpm(2)
tf(2)=time_rpm(3)
with the initial condition to be the last evaluation of the previous loop
y1(i+1) = y(end,1); %initial condition 1
y2(i+1) = y(end,2); %initial condition 2
12 Comments
Cris LaPierre
on 7 Jan 2021
Edited: Cris LaPierre
on 7 Jan 2021
What is the reason you want to solve it this way? Knowing that, there might be a better approach we could suggest.
Paul Rogers
on 7 Jan 2021
Paul Rogers
on 7 Jan 2021
Edited: Paul Rogers
on 7 Jan 2021
Paul Rogers
on 7 Jan 2021
Paul Rogers
on 7 Jan 2021
Cris LaPierre
on 7 Jan 2021
Edited: Cris LaPierre
on 7 Jan 2021
I think the issue is you need to index the t0 and tf values in your call to ode113. In the first loop, they are scalars, in loops 2+, they are vectors. Just pass in the current values each time.
[t,y]=ode113(@greitzer,[t0(i),tf(i)],[y1(i),y2(i)],options);
Paul Rogers
on 7 Jan 2021
Cris LaPierre
on 7 Jan 2021
Edited: Cris LaPierre
on 7 Jan 2021
That error is a symptom of the same issue. You really want U1(i),U2(i), and B(i) in your equations, but you use the entire vector.
You might be interested in this example, which shows how to pass extra parameters to your odefun.
Saving and loading a mat file is not a good solution.
I'd call ode113 with the following syntax
[t,y]=ode113(@greitzer,[t0(i),tf(i)],[y1(i),y2(i)],options,U1(i),U2(i),B(i)); %I found ode113 is way more efficient
This means updating your function declaration accordingly.
function [ dy ] = greitzer( t,y,U1,U2,B )
Paul Rogers
on 7 Jan 2021
Edited: Paul Rogers
on 7 Jan 2021
Cris LaPierre
on 8 Jan 2021
Edited: Cris LaPierre
on 8 Jan 2021
You overlooked the other comment I made about saving/loading your other parameters from a mat file. This is replacing the values you are passing in with the ones loaded from the mat file. At the least, do not include U1, U2 or B in your save. That means at least removing the save from your for loop and uncommenting the one earlier in your code.
Just a heads up, you're going to get another error once you fix this one.
Paul Rogers
on 8 Jan 2021
Edited: Paul Rogers
on 8 Jan 2021
Paul Rogers
on 8 Jan 2021
Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!