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 !
Since you mention
ode45 is the ODE solver, but another one could be used.
And your concern (it seems to me) is having access to the history such that you can evaluate d^2x/dt^2
yourself, to introduce the new conditions, have you considered looking into dde23
(delayed diffeq)?
From the documentation, you specify a vector of lag times yourself, and have the option to specify history as "...the previous solution from an integration, if this call continues that integration" https://www.mathworks.com/help/matlab/ref/dde23.html.
Hopefully this is helpful since, at the very least you should be able to make a workaround by wrapping your own function that returns the values from the dde23 solutions and check the point you should introduce the new conditions yourself, and then specify the new function, compiling your results as necessary, essentially reducing the problem into more steps.