How to find second derivative as output of MATLAB ode45?

462 Views Asked by At

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?

1

There are 1 best solutions below

0
On

This is more of a general suggestion than any matlab specific advice: Let us say your ODE looks like this: $$y'(t)=f(y(t),t).$$ If you derive the whole equation by $t$ you get with chain rule $$y''(t)=\frac{\partial f}{\partial y}(y(t),t)\cdot y' + \frac{\partial f}{\partial t} (y(t),t).$$ Since matlab already gave you $y$ and $y'$ you can put it into this formula to obtain the second derivative.