MATLAB: Differentiation of the ODE solution

diff()differential equationsodeode15iode45plot

Hallo guys, I had ODE in my code. I solved it using an ode15i function. My code is such that the inputs of the ode15i function change when the output of the ODE reaches a limit. I did successfully solve the ODE and got the plot. Now I want to differentiate the solution to get the accelerations. I tried using diff() operator and passed the numerical outputs of the ODE but the graph seems incorrect (seems discontinous with sudden peaks and jumps).
Is there any way that I can differentiate the numerical solution / plot obtained from the ODE .

Best Answer

There are several possibilities, depending on what your ODE function is. The easiest way is to give your integrated solutions to your ODE function, since it codes for all the derivatives.
For example (using an anonymous function for the ODE function):
ode_fcn = @(t,y) ...; % ODE Function
[t,y] = ode15i(ode_fcn, ...); % Integrate ODE
dy = ode_fcn(t,y); % Calculate Derivatives
That is how I calculate the derivatives.