ode in a for loop updating initial conditions

6 views (last 30 days)
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
Paul Rogers
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
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"

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2014b

Community Treasure Hunt

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

Start Hunting!