Original Post
Hi, I am after some help/advice on how to write a reasonably reusable integration function. The calculations for Euler integration are fairly simple: y(t + dt) = y(t) + dt * f(t, y(t)), where dt is the step size and f() is the derivative evaluated at t, y(t). To move a particle, the following steps are performed: position = position + velocity * dt velocity = velocity + acceleration * dt What I have so far is a function euler() which takes the following parameters: t, dt, y(t) and a function pointer to the function f(t, y(t)) which evaluates the slope at that point.
euler(t, dt, y(t), f(t, y(t)))
{
return y(t) + f(t, y(t)) * dt;
}
position = euler(t, dt, position, velocity());
velocity = euler(t, dt, velocity, acceleration());
In this simple case lets say we have a particle with initial a=10, v=0, p=0. The function acceleration simply returns the constant 10. Function velocity() return the current velocity. This works fine. But let's say we make things slightly more complex:
acceleration(t, y(t))
{
return -0.5 * y(t);
}
Now acceleration is no longer constant. My question is, what is the correct way to formulate the velocity function so that the proper velocity is returned? I would like the same function interface to work for other integrators so that when I decide to implement RK4 at some point I will only need to replace all instances of euler() with rk4(). A lot of examples that I've come across have all of the variables integration crammed into the one function. Thanks for your attention. [Edited by - _V_ on January 5, 2007 4:53:23 AM]