Second derivative of solution from ode45

I have a system of 2 second order ODEs, which i converted to a system of fist order ODEs, which ode45 (or ode15s) needs as an input
my system input looks like this:
as an output i get the vector , but I need the derivatives and . How can i get those?
I tried using the gradient function as suggested by Star Strider in this question: How to output derivative from ODE 45 but unfortunately that did not help.
Thanks in advance!

 Accepted Answer

After integrating your ODE-system you will have for some , and you have the function for your ODE-equations, f(t,X), one way to calculate the accelerations at times will be to simply call:
dxd2xdyd2ydt(i,:) = f(t_i,X(i,:));
Typically I use a regularly space array for the time in the call to ode45 and use the gradient-function, but the above should work - that is after all what we expect odeNN: to give us a solution X where that is true...
HTH

8 Comments

e_frog
e_frog on 4 Nov 2020
Edited: e_frog on 4 Nov 2020
ty for your answer! I dont quite understand what you mean by "function for your ODE- equations" or what the f would be. Could you please elaborate give me simple example?
You have expressions for ODE1 and ODE2. Simply plug the solution from ode45 into those expressions to get your 2nd derivative values.
@e_frog: Do you wonder how to write your ODE-function?
If so, let's make a 1-D harmonic oscillating ODE, i.e.
That would become a function something like this:
function dxdtd2xdt2 = ode_ex_ho(t,x,k)
dxdtd2xdt2 = [x(2);
-k*x(1)];
end
Such a function you can adjust as you need.
HTH
@Bjorn
Thats exactly how I converted my second order ODEs to a first order system. I would call the example function like this:
[t,X] = ode45(@(t,X) ode_exo_ho(t,X,k),tspan,y0)
The Problem with that is, that the coutput vector X
would in this example be
but I need
(respectively for my case with 2 ODEs I would need and )
Please show us the expressions for ODE1 and ODE2. I still don't understand why you are having trouble plugging x and xdot into these expressions to get xdotdot. Why can't you just call ode_exo_ho( ) with the appropriate inputs to get what you want?
@james
do you mean I can just insert the double vectors in the ODE functions and calculate the result? wow.. that would be too easy... :'D
If the ode_exo_ho( ) function is vectorized (meaning it will accept vectors and properly calculate a vector result), then yes it is just this easy. If ode_exo_ho( ) is not vectorized, then you would have to call it in a loop, but still pretty easy.
The ease of vectorizing your function depends on what it is doing. Sometimes it is just a simple matter of replacing the operators * and / and ^ with their element-wise counterparts .* and ./ and .^
ty it worked!

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!