MATLAB: How to find second derivative as output of ode45

derivativedifferential equationsMATLABnumerical derivativenumerical differentiation

I am using ode45 function to find numerical solution for my system of equations, where I have 4 equations and 4 variables, with command:
sol=ode45(@fun,[1 0],[1; 0; 0; 0])
where time span is going from 1 to 0, and initial conditions are 1,0,0,0.
So, I need to find numerically the first and the second derivative of my values according to time. For the first derivative I have verified code
[~,SXINT]=deval(sol,sol.x);
where in SXINT are stored first derivatives of all 4 variables.
But I don`t know how to find the second derivative numerically. I tried:
[~,SXINT2]=deval(SXINT,sol.x);
but I got error:
SXINT must be a structure returned by a differential equation solver.
How to make that SXINT be in that structure, or is there some other way to find the second derivative, but with the same precision as deval method is with ode45 solver?

Best Answer

To get the second derivative, the first derivative must be part of the solution "sol".
So you have to solve 8=4*2 equations, namely your original system and new algebraic equations given by
y(5)-dy(1) = 0
y(6)-dy(2) = 0
y(7)-dy(3) = 0
y(8)-dy(4) = 0
This way, "sol" has stored the first-order derivatives of y(1)-y(4) in y(5)-y(8) which can now be differentiated by a call to "deval".
Best wishes
Torsten.
Related Question