# Approximating derivative of numerical solution (using ode45) within event function

The issue I have is having to compute the derivative (in real time) of the solution produced by ode45 within the events function.

Some pseudo-code to explain what I'm mean is,

function dx = myfunc(t,x,xevent)
persistent xevent
% xevent is the solution at the event
dx(1) = x(2);
dx(2) = complicated function of x(1),x(2), and xevent;
end

function [value,isterminal,direction] = myeventfcn(t,x)
position = function of x(1), x(2), and dx(2);
isterminal = 1;
direction = 0;
end


I know that if I didn't need to use the solution at the event within myfunc I could just compute dx=myfunc(t,x) within my event function and use dx(2), yet since xevent is used within myfunc I can't input xevent.

I know there is a way of inputting constant parameters within the event function, but since it's the solution at the event location, which also changes, I'm not sure how to go about doing that.

My work around to this is to approximate dx(2) using the solution x. What I wanted to know is if it will be satisfactory to just use a finite difference approximation here, using a small fixed step size relative to the step size od45 takes, which is a variable step size.

Thanks for any help!

• Dense output functions should be able to calculate this for free (without an $f$ call) using the analytical solution for the derivative on the interpolant (just take the $\theta$ derivative, in the common notation). This is setup in DifferentialEquations.jl as integrator(t,Val{1}) and in Sundials with CVodeDKy: does MATLAB not have something similar? I would be surprised given the ease of implementation. If it has a dense output function you can call that's likely the most efficient way and should be similar order as the solution itself, or at least similar enough. – Chris Rackauckas Jul 11 '17 at 6:40
• I believe there is a way to calculate the derivative using diff when you have the computed solution. I need to be able to approximate the derivative while ode45 is running. – Shant Danielian Jul 11 '17 at 18:21
• Well diff is using the discrete values. I'm talking about the dense output function. – Chris Rackauckas Jul 11 '17 at 18:22
• I'm not sure if there is such a function, albeit I'm probably not the best person to ask. I did a quick google search and found this paper which defines what a dense output is. homepage.divms.uiowa.edu/~ljay/publications.dir/… Is this what you mean? – Shant Danielian Jul 11 '17 at 18:33
• Yes, and that's written by the author of the MATLAB ODE suite. If you differentiate the $y_{n+\theta}$ equation by $\theta$ you get the free interpolation for the derivative. I learned this all and implemented it in the Julia suite using papers by Shampine, so I am quite certain this must be in MATLAB somewhere, but Google searches for "dense output ODE MATLAB" and "output function ODE MATLAB" aren't showing anything of use. But that's definitely where to look. – Chris Rackauckas Jul 11 '17 at 18:36