Using values from previous time step in ODE solver to use for next time step?
Show older comments
I have two differential equations, one for velocity and one for acceleration. The acceleration equation has terms that require the calculated velocity from the previous time step. I have a simplified version of my code posted below for explanation. F, L, CL are all dependent on the velocity from the previous iteration. How would one access these values? The example differential equations are:
- dx/dt = v
- dv/dt = (g/W)*(n*F-(W-L))
function [t,X1,X2] = myode(g,W,n,rho,s,Vr,x0,v0)
if v <= Vr
CL = 1;
else
CL = 1.6;
end
L = 0.5*CL*rho*s*v^2;
f = @(t,y)[...
v;
(g/W)*(n*F-(W-L))]
tspan = [0 10];
y0 = [x0 v0];
[t,y] = ode45(f,tspan,y0);
X1 = y(:,1);
X2 = y(:,2);
end
Answers (1)
Something like a "previous time step" or "iteration" is not meaningful in standard ODE integrators. The previously calculated step might be rejected by the step size controller. There is no way to control this inside the function to be integrated. I'm not sure if "F, L, CL are all dependent on the velocity from the previous iteration" is meaningful from the mathematical point of view also. Usually the current velocity is provided to the function, but why could an older value be of interest? Imagine that the step size controller can create steps in a size of e.g. 1 sec or 2 sec. But this would influence the actually used value. In consequence you would get different trajectories depending on the tolerance of the integration. I cannot imagine that this can have any physical meaning, but the calculated trajectory will be more or less random.
The posted code does not run: v is undefined. The function to be integrated:
f = @(t,y)[v; (g/W)*(n*F-(W-L))]
does not depend on t or y.
I have the impression, that you want to integrate a non-smooth system. This is not supported by Matlab's integrators, see http://www.mathworks.com/matlabcentral/answers/59582#answer_72047. If you are lucky, ODE45 stops with an error, but without luck you get a random result which is dominated by rounding errors and cannot be considered as a trustworthy result.
Please edit the question and post the mathematical system you want to solve.
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!