ODE45 function in C#

Isra Naz

New member
Joined
Aug 27, 2022
Messages
1
Programming Experience
3-5
In the Matlab ODE Solver has a way to terminate integration when an event triggers. It has a function called "Ode45" to solve differential equation using Runge-Kutta method and call an event to terminate the function as follows:
C#:
%Evaluate system of differential equations.
tspan=[0 t_max]; %Time span of simulation in sec
Opt = odeset('Events', @myEvent);
[t,x]=ode45(@EoM,tspan,x0,Opt); %Run the ODE solver

%% Termination function for ODE.
%Terminate when altitude is less than 0 meaning that the munition impacted
%the ground.
function [value, isterminal, direction] = myEvent(t, y)
    %value      = (y(11) < 0);
    value      = y(11);
    isterminal = 1;   % Stop the integration
    direction  = -1;
end
Is there a way to implement this kind of triggering in C#? how can I use Math.Net RungeKutta.FourthOrder() for this?
 
Last edited by a moderator:
You'll have to look at the documentation for that method to see if it has some kind of terminating condition function or callback that can be passed in.
 
Looks like there the function that you pass in is just purely a function. There's no side channel to tell the caller to terminate.

That's verified by looking at the source code. It just calls the function.

Looks like you'll need to either fork the code and a way to signal to terminate early; or you'll need just need to throw an exception from within your exception to force the caller to terminate. You'll just need to be able to catch that special exception that you threw. Although exceptions should not really be used for flow control, that maybe your only choice to if you don't want to take time to fork the code.
 
Back
Top Bottom