### Applying Runge-Kutta method to circular motion

Hey People!

I'm trying to approximate circular motion applying the Runge-Kutta method. I use the following functions as my derivatives for the velocities vel_x and vel_y:

 ``123456789101112131415`` ``````double accx(double x, double y,double vel_x, double vel_y) { double vv = vel_x*vel_x + vel_y*vel_y; double r = sqrt(x*x + y*y); double acc = vv/r; return acc * -1 * cos(angle(x,y)); } double accy(double x, double y, double vel_x, double vel_y) { double vv = vel_x*vel_x + vel_y*vel_y; double r = sqrt(x*x + y*y); double acc = vv/r; return acc*-1 * sin(angle(x,y)); }``````

And here I perform an iteration:

 ``123456789101112131415161718192021222324252627282930313233`` `````` double kx[4]; double ky[4]; double tempx; double tempy; //K0 kx[0] = accx(x,y,vel_x,vel_y); ky[0] = accy(x,y,vel_x,vel_y); //K1 tempx = x + dt/2*(vel_x+dt/2*kx[0]); tempy = y + dt/2*(vel_y+dt/2*ky[0]); kx[1] = accx(tempx,tempy,vel_x + dt/2*kx[0],vel_y+dt/2*ky[0]); ky[1] = accy(tempx,tempy,vel_x + dt/2*kx[0],vel_y+dt/2*ky[0]); //K2 tempx = x + dt/2*(vel_x+dt/2*kx[1]); tempy = y + dt/2*(vel_y+dt/2*ky[1]); kx[2] = accx(tempx,tempy,vel_x + dt/2*kx[1],vel_y+dt/2*ky[1]); ky[2] = accy(tempx,tempy,vel_x + dt/2*kx[1],vel_y+dt/2*ky[1]); //K4 tempx = x + dt*(vel_x+dt*kx[2]); tempy = y + dt*(vel_y+dt*ky[2]); kx[3] = accx(tempx,tempy,vel_x + dt*kx[2] ,vel_y+dt*ky[2]); ky[3] = accy(tempx,tempy,vel_x + dt*kx[2] ,vel_y+dt*ky[2]); vel_x += dt/6*(kx[0]+2*kx[1]+2*kx[2]+kx[3]); vel_y += dt/6*(ky[0]+2*ky[1]+2*ky[2]+ky[3]); x+=dt*vel_x; y+=dt*vel_y;``````

However, if I analyse the results, I still get a diverging circle. Is it possible to make an accurate approximation using this method? If it is, what am I doing wrong?

Thanks!
Hey people,

For anyone interested: I figured it out. Problem is that you need to apply Runge - Kutta to both the velocity and position. This can be done by using a class that holds all the information on a certain 'state', and as such the method can be generalized to any problem.

Cheers
Topic archived. No new replies allowed.