# Stopping ODE solver on zero crossing

1. Oct 27, 2009

### Lord Crc

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!

2. Oct 27, 2009

### matematikawan

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.

3. Oct 28, 2009

### Lord Crc

Yes, I'm writing my own RK4 routine in C#.

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