Approximating derivative of numerical solution within event function
10 views (last 30 days)
Show older comments
Shant Danielian
on 10 Jul 2017
Commented: Shant Danielian
on 24 Jul 2017
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!
10 Comments
Walter Roberson
on 24 Jul 2017
Change CachePolicy to CachingPolicy
See toolbox/matlab/lang/+matlab/+lang/MemoizedFunction.m near line 138
Accepted Answer
Walter Roberson
on 10 Jul 2017
parameterize both the ode function and the event function to pass in xevent
Do not make xevent persistent within the ode function: when you do that, the variable's identity as persistent overrides the value passed in as a parameter.
If xevent is something being calculated inside the ode function rather than something being passed in, then do not try to remember it for later use in the event function by using persistent or global or a shared variable: There are circumstances under which ode45 might not call the event function for a while, and there are circumstances under which ode45 might call the event function several times in a row with different parameters without calling the ode function between. Therefore any value that you calculate in the ode function that you would like to also use in the event function needs to be recalculated in the event function.
3 Comments
Walter Roberson
on 10 Jul 2017
If xevent is changing during any one call to ode45() then you run the risk that you are introducing a discontinuity in the calculation or in one of of the next two derivatives of the calculation.
Discontinuities in the next one derivative will typically be detected and ode45 will narrow in on the time it happens at, trying to find a smooth transition and being unable to find it.
Discontinuities in the second derivative after what is directly calculated will not necessarily be detected by ode45 but will typically lead to errors in calculations.
Therefore if you have a parameter that is changing as it appears you are saying it might, then you should use an event function to detect the circumstances under which the transition should be made and terminate the ode45, and then call ode45 again with the rest of the timespan and with the boundary conditions set to the output of the previous call, but with the changed value of the parameter. Which you would pass in by parameterizing the call like I linked to.
More Answers (0)
See Also
Categories
Find more on Platform and License in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!