Periodic input to ODE solver
Show older comments
I would like to run an ODE solver over a time span but provide an additional jump in initial condition on a periodic basis (every day for 112 days). I attempted to do this by using mod(t,1) and setting the time span to be 0:0.1:112, so that every ten steps I could add a static amount to the ODE, and in between it would just have a simple decay term.
t = [0.1:.1:112];
z0 = 0.1;
[t,z] = ode23s(@dInput,t,z0);
plot(t,z);
function zr = dInput(t,z)
dCycle = mod(t,1);
if dCycle == 0
input = 1;
else
input = 0;
end
zr = input-0.1*z;
end
mod(t,1) is producing irregular results, sometimes failing to set input for long stretches and other times setting it too frequently. And even when it is set to 1, the output for the equation does not change accordingly.
Is it possible to provide a periodic input to this kind of solver?
Accepted Answer
More Answers (1)
Walter Roberson
on 2 Aug 2022
1 vote
No, that violates the mathematics that the solver uses. You need to stop the function every time there is a discontinuity and restart it. And remember that the solver does not take fixed time steps so do not assume that it will evaluate exactly at the integers.
In your case the discontinuity is predictable just from the time, so loop running first 0.1 to 1, then adjust the boundary conditions then 1.0 to 2.0, adjust conditions, then 2.0 to 3.0... and so on.
Note that when you pass in a vector tspan with more than two elements then the ode*() functions continue to evaluate at whatever times they feel is appropriate to meet the accuracy, but they will only report the results at the time entries you provide.
1 Comment
Eric Scott
on 2 Aug 2022
Categories
Find more on Ordinary Differential Equations 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!


