Change parameter value during ode15s solution
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm solving a set of differential equations and I'd like to change the value of a constant (Pin) at a specific time during the solution. Meaning that during the timespan, I'd like to change the value of Pin. So it time>10, Pin=70, else Pin=60.
This is how I formulated it but it's not working:
tspan=0:0.1:100;
cond= [...];
[t,y] = ode15s(@fun2,tspan, cond,[]);
function dydt= fun2(t,y)
for i=1:1:length(tspan)
if tspan(i)<10
Pin=60;
else
Pin=70;
end
end
...
end
0 个评论
采纳的回答
Torsten
2018-8-23
function dydt= fun2(t,y)
...
if t<10
Pin=60;
else
Pin=70;
end
...
Better use the EVENT-facility of the ODE solvers than the if-construction.
Best wishes
Torsten.
3 个评论
Steven Lord
2018-8-23
In this case I wouldn't use the events functionality. I tend to recommend that when you don't know at write-time when the events you're looking for will occur. In this case, you know exactly when it will occur: at t = 10.
Therefore I'd solve this problem twice. The first call to ode15s would solve from t = 0 to t = 10. I'd use the final result of that call to generate the initial conditions for the second call to ode15s, running from t = 10 to t = 100. Doing so would be easier with a slightly different function signature:
function dydt= fun2(t,y,Pin)
and specifying an anonymous function as the ODE function.
@(t, y) fun2(t, y, desiredValueOfPinForThisOde15sCall)
Replace desiredValueOfPinForThisOde15sCall with 60 or 70 depending on whether you're passing this anonymous function into the first or second ode15s call.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!