How do I add an input in the middle of an ode45 integration?
8 次查看(过去 30 天)
显示 更早的评论
I have a group of odes that I'm integrating with ode45. Let's say from time 0 to 100. I want to add an instantaneous (or nearly) impulse at time 50. How would I do that?
I tried adding an if loop like below in the derivative routine, but I got the following error.
if t >= 50
original function + impulse
elseif t >= 50.1
original function
else
original function
end
Subscript indices must either be real positive integers or logicals.
2 个评论
Walter Roberson
2015-8-5
Please show the actual code you attempting to add.
original function
would be the code for calling a routine named "original" with a single argument which was the string named 'function', the same thing as
original('function')
采纳的回答
Joe
2015-8-5
You can use logical arithmetic to set conditions for your force. Taking the code snippet you posted above, you would assign "v" as follows:
v = 1/m*F + (t > Timp)*Fimp(:); % (t > Timp) evalutes to 0 if t is under Timp and 1 otherwise
Note that I made "Fimp" a column vector because you will get dimensional incompatibility otherwise.
2 个评论
Joe
2015-8-6
That is tougher, because you can't uniquely define "instant" with respect to ode45. It has an adaptive time-step routine that will most certainly never be exactly equal to "Timp." Worse, even if you specify an interval for the force to be active, i.e.
interval = 1E-3;
condition = (t > Timp) && (t < (Timp + interval));
If may still never trip, because the stepper might overstep that interval during the integration.
In this case, your best approach would be to look to Steven's recommendation and stop the integration just prior to your force application. Then, start a new run with the force applied over some interval. Make sure to conserve total impulse (Force*time) and that the interval is fast with respect to your system dynamics. When the time exceeds that interval, stop that run, and start up a third with no applied force.
The conditional approach which I initially suggested works fine when your force is applied over a long time interval, but for impulsive forces, your results can vary widely depending upon the timesteps which the integrator is taking, which you can't control. You can, however, guarantee a force application over a set period of time using the integration time command on ode45, so the separate integration runs is the way to go in that case.
更多回答(1 个)
Steven Lord
2015-8-5
I recommend solving your system of ODEs from t = 0 to t = 50, apply the impulse to the final results from that initial call to the ODE solver to generate new initial conditions, then use those new initial conditions to solve the system of ODEs from t = 50 to t = 100.
The BALLODE example does a variant of this (using the speed of the ball when it hits the ground to set its speed upward after it bounces) but since it doesn't know when the ball is going to hit the ground it needs to use an events function. You DO know when your "event" will occur, so you can just set up the ODE solver call to stop its first run at that time for adjustment.
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!