I have this 2nd order ODE to solve in Matlab:
(a + f(t))·(dx/dt)·(d²x/dt²) + g(t) + ((h(t) + i(t)·(d²x/dt² > b·(c-x)))·(dx/dt) + j(t))·(dx/dt)² + k(t)·(t > d) = 0
where
a
,b
,c
,d
are known constantsf(t)
,g(t)
,h(t)
,i(t)
,j(t)
,k(t)
are known functions dependent on t
x
is the positiondx/dt
is the velocityd²x/dt²
is the accelerationand notice the two conditions that
i(t)
is introduced in the equation if (d²x/dt² > b·(c-x))
k(t)
is introduced in the equation if (t > d)
So, the problem could be solved with a similar structure in Matlab as this example:
[T,Y] = ode45(@(t,y) [y(2); 'the expression of the acceleration'], tspan, [x0 v0]);
where
T
is the time vector, Y
is the vector of position (column 1 as y(1)
) and velocity (column 2 as y(2)
).ode45
is the ODE solver, but another one could be used.tspan
,x0
,v0
are known.the expression of the acceleration
means an expression for d²x/dt²
, but here comes the problem, since it is inside the condition for i(t)
and 'outside' at the same time multiplying (a + f(t))·(dx/dt)
. So, the acceleration cannot be written in matlab as d²x/dt² = something
Some issues that could help:
once the condition (d²x/dt² > b·(c-x))
and/or (t > d)
is satisfied, the respective term i(t)
and/or k(t)
will be introduced until the end of the determined time in tspan
.
for the condition (d²x/dt² > b·(c-x))
, the term d²x/dt²
could be written as the difference of velocities, like y(2) - y(2)'
, if y(2)'
is the velocity of the previous instant, divided by the step-time defined in tspan
. But I do not know how to access the previous value of the velocity during the solving of the ODE
Thank you in advanced !
First of all, you should reduce your problem to a first-order differential equation, by substituting dx/dt with a dynamical variable for the velocity. This is something you have to do anyway for solving the ODE and this way you do not need to access the previous values of the velocity.
As for realising your conditions, just modify the function you pass to ode45
to account for this.
For this purpose you can use that d²x/dt² is in the right-hand side of your ODE.
Keep in mind though that ODE solvers do not like discontinuities, so you may want to smoothen the step or just restart the solver with a different function, once the condition is met (credit to Steve).