You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
ode in a for loop updating initial conditions
1 view (last 30 days)
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
because giri changes through time,
basically I have to solve the ode for:
giri = 55000 from 0s to 25s with the initial condition y1(0) and y2(0), then giri changes
then giri changes like this:
giri_medium = 55000; %throttle valve aperture
delta_giri = 400; %range between your gamma_T_max e gamma_T_min
giri_max = giri_medium+(delta_giri/2); %maximum aperture
giri_min = giri_medium-(delta_giri/2); %minimum aperture
A_rpm = (giri_max - giri_min)/2; %amplitude
b_rpm = (giri_max + giri_min)/2;
rpm_frequency = 1; %engine's frequency [Hz]
w_rpm = rpm_frequency*2*pi; %engine's frequency [radiants/s]
time_rpm=[25:0.01:30];
giri = A_rpm*sin(w_rpm*time_rpm)+b_rpm;
plot(time_rpm,giri)
and the only way I could think of was that one because every time the value of giri changes I need the previous initial conditions
Paul Rogers
on 7 Jan 2021
Edited: Paul Rogers
on 7 Jan 2021
every time giri changes I need as initial condition the last values of y evalueted at the step before (for the previuos value of giri).
I also did the code for just one value of giri = 55000 in a tspan 0s-50s, but now I need giri to be 55000 for the fits 25s, then to change like in the code previouslly written
Paul Rogers
on 7 Jan 2021
the thing I don't get is that the code works for the first loop, then it stops at the second and displays this message:
Error using odearguments (line 84)
The entries in tspan must strictly increase or decrease.
Paul Rogers
on 7 Jan 2021
the first cycle is from 0-25.0100 for the first value of giri.
for the second value of giri should be from 25.0100 to 25.0200.
and so on
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
now I get this:
Error using *
Inner matrix dimensions must agree.
Error in greitzer (line 34)
dy = [ B*(psi_c-y(2));
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode113 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in main (line 106)
[t,y]=ode113(@greitzer,[t0(i),tf(i)],[y1,y2],options); %I found ode113 is way more efficient
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
by doing this:
[t,y]=ode113(@greitzer,[t0(i),tf(i)],[y1(i),y2(i)],options,U1(i),U2(i),B(i));
and:
function [ dy ] = greitzer( t,y,U1,U2,B )
I get this the error:
Error using *
Inner matrix dimensions must agree.
Error in greitzer (line 35)
dy = [ B*(psi_c-y(2));
It stops when the second loop should start.
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
first thing I did in the morning was to follow yoour update.
I brought:
save main_parameters.mat
outside the loop and the simulation finally seems to goo until the end.
I also changed the lenght of the simulation by switching to:
save main_parameters.mat
for i=1:(length(giri)-2)
U1(i) = (D1*pi.*giri(i))/60;
U2(i) = (D2*pi.*giri(i))/60;
B(i) = U1(i)./(2.*wh.*Lc);
[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
t0(i+1) = time_rpm(i+1); %simulation's start [s]
tf(i+1) = time_rpm(i+2); %simulation's finish [s]
y1(i+1) = y(end,1); %initial condition 1
y2(i+1) = y(end,2); %initial condition 2
plot(t,y(:,2))
grid on
grid minor
xlabel('t [s]')
ylabel('\Psi')
hold on
end
by thesee littlle tweeks thanks to you I ccould finally get the result I expeccted.
Just one more thing now:
How can I store all the datas (t,y(:,1) and y(:,2)), since the function only shows the values from the last loop.
Paul Rogers
on 8 Jan 2021
thanks to you I solved the loop problem and I got the results I wanted, unfortnatley I can't see the "accepted answer button"
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)