matlabsolverode45

How to use ODE45 to solve for a system of differential equations with a difficult end condition?


My system of equations can be written down as-

y1' = F_1(x,y1,y2)

y2' = F_2(x,y1,y2)

where F1 and F2 are some functions of x, y1, y2. y1 and y2 are functions of x and y1'=dy1_dx and y2'=dy2_dx.

The initial condition is at x=0, y1=y2=0. However I need the solution at y1=1, i.e. as soon as y1=1 is reached, the iteration needs to stop. I want to solve it using a proper solver like ODE45 or ODE15s in MATLAB. However I am not sure how to set the x_span i.e. initial and final values of x. Please guide me.

Note that, setting x at a large value say 1000 will not help, because I don't know where y1=1.


Solution

  • Use an event function (typically equal to y1-1) that you give to the ODE solver: https://fr.mathworks.com/help/matlab/math/ode-event-location.html

    As soon as the solver detect a sign change of your event function, it stops.