Stopping ODE solver on zero crossing

  • Thread starter Thread starter Lord Crc
  • Start date Start date
  • Tags Tags
    Ode Zero
Lord Crc
Messages
341
Reaction score
47
Hi,

I'm trying to solve an initial-value ODE using RK4, say dx/dt = f(x,t), x(0) = x0. I want to find t_end so that x(t_end) = 0.

The basic RK4 implementation works fine, however I'm having some difficulties with stopping it in time. I've tried googling around but all I could find was references to MATLAB or similar, which doesn't say anything about how the zero crossing is detected and handled.

The immediate way I can think of is to check after each integration step if x(t) has crossed, and if so restart the step using say half the step length, repeating until it no longer crosses, x(t) = 0 or the step length is below some minimum. Is this an acceptable solution? Are there some better ways?

Cheers!
 
Physics news on Phys.org
Do you write your own RK4 program or you use some sort of built-in functions like ode23 in matlab, etc.

If approximate answer is acceptable, I suggest you use linear interpolation. Suppose that the last coordinate is (T, x(T)) before the graph cross the axis. Let find dt such that x(T+dt) = 0.

By Taylor expansion, x(T) + x'(T) dt = 0.
Hence, T+dt = T - x(T)/x'(T) is the approximate time for the zero crossing.
 
Yes, I'm writing my own RK4 routine in C#.

Thanks for the linear interpolation suggestion. I'll check it out :)
 
Back
Top