while in ode45
57 次查看(过去 30 天)
显示 更早的评论
Let me solve an ODE .
dy/dt=y.
y(0)=1.
Now i want to just solve the ODE for 5 seconds. Let y(5) denote the value of state 'y' at time =5.
At t=5 I want to update the initial condition as: y(5)=0.5*y(5) and again solve the given ODE for next 10 seconds.
I want to use while statement for this work. Can I do that ? If yes, please provide some outline of the code.
Thanks
2 个评论
James Tursa
2024-11-30,18:34
What have you done so far? What specific problems are you having with your code? Can you code up the first part, without any while loop, to just solve for the first 5 seconds?
采纳的回答
Cris LaPierre
2024-11-30,18:35
One of your inputs to ode45 is tspan. If I were going to do this, I would look at building is so that my loop calls ode45 with the desired inputs rather than trying to do it inside the odefcn.
Here is an outline (untested)
y0 = 0;
tspan = [0 5;5 15]
y_out = [];
for ts = 1:sizse(tspan,1));
[t,y] = ode45(odefcn,tspan(ts,:),y0);
y0 = o.5*y(end);
y_out = [y_out;y];
end
9 个评论
Torsten
2024-12-1,10:39
As you are a beginner in MATLAB, use MATLAB Onramp to learn the basics of the new language:
Concerning your question, it will help to study the "ballode" example here:
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!