Rung Kutta 4 in more than 1 dimension of space

What did you draw?
Post Reply
AlternateGravity
Posts: 80
Joined: Thu May 15, 2014 5:45 pm

Rung Kutta 4 in more than 1 dimension of space

Post by AlternateGravity »

I understand that for one dimension of space a Rung Kutta 4 Formula for approximating the equations of motion given some force that depends only on the location of an object is

k1xn=vn
k2xn=vn+h*k1vn/2
k3xn=vn+h*k2vn/2
k4xn=vn+h*k3vn
xn+1=xn+h/6(k1xn+2*k2xn+2*k3xn+k4xn)

k1vn=f(xn)=acceleration
k2vn=f(xn+h*k1xn/2)
k3vn=f(xn+h*k2xn/2)
k4vn=f(xn+h*k3xn)
vn+1=vn+h/6(k1vn+2*k2vn+2*k3vn+k4vn)

h=Deltat

I tried using the above formula with the initial values being x=1, and v=0, the acceleration being given by the equation F=-x, and h=0.3.
Rung Kutta verses Analytic.png
Rung Kutta verses Analytic.png (206.39 KiB) Viewed 12840 times
and found that the Rung Kutta 4 approximation is close to the analytical solution of x=cos(t) even after nearly 3000 time steps despite having such a large time step.

For comparison when I used the same initial conditions, same equation for acceleration, and same time step law, using a method where I approximate the differential equation using polynomials of the same degree as the order of differential equation, which I will just call the polynomial method in this thread, using the formulas

xn+1=1/2anh^2+vnh+xn
vn+1=anh+vn
Polynomial Method.png
Polynomial Method.png (279.07 KiB) Viewed 12840 times
The amplitude is already several times greater than that of cosine after only a few cycles.

I also tried using both the Rung Kutta 4 method and the polynomial method mentioned above to approximate the case where the initial position is 1, initial velocity is 0, and with a=sin(x).
Rung Kutta verses Polynomial Simple.png
Rung Kutta verses Polynomial Simple.png (351.78 KiB) Viewed 12840 times
For a time step of 0.006 I found that both formulas give nearly the same result even after nearly 3000 time stems.
Gravitons would be my favorite particle as their existence could prove extra dimensions.
AlternateGravity
Posts: 80
Joined: Thu May 15, 2014 5:45 pm

Re: Rung Kutta 4 in more than 1 dimension of space

Post by AlternateGravity »

I tried changing the time step to 0.3
Polynomial.png
Polynomial.png (269.32 KiB) Viewed 12839 times
and found that with the polynomial method the x value doesn't even complete 1 cycle before rushing towards negative infinity
Rung Kutta.png
Rung Kutta.png (138.39 KiB) Viewed 12839 times
However for the Rung Kutta 4 method the amplitude is nearly the same as it was at the beginning implying that the Rung Kutta method I chose is more accurate than the polynomial method.

I know that for the polynomial method I mentioned the way to extend the method to two spatial dimensions is to use the formula

F=r/||r||*f(||r||)

So I though I could just change the formulas in the Rung Kutta 4 method to

k1rn=vn
k2rn=vn+h*k1vn/2
k3rn=vn+h*k2vn/2
k4rn=vn+h*k3vn
rn+1=rn+h/6(k1rn+2*k2rn+2*k3rn+k4rn)

k1vn=rn/||rn||*f(||rn||)
k2vn=rn+h*k1vn/2/||rn+h*k1vn/2||*f(||rn+h*k1vn/2||)
k3vn=rn+h*k2vn/2/||rn+h*k2vn/2||*f(||rn+h*k2vn/2||)
k4vn=rn+h*k3vn/||rn+h*k3vn||*f(||rn+h*k3vn||)
vn+1=vn+h/6(k1vn+2*k2vn+2*k3vn+k4vn)

I tried this approach on the inverse square law
Rung Kutta verses Polynomial 2d.png
Rung Kutta verses Polynomial 2d.png (355.92 KiB) Viewed 12839 times
And found that the method I chose gave a worse answer than the polynomial method I mentioned making me think I chose the wrong way to generalize the Rung Kutta 4 method as it should be more accurate than the polynomial method I mentioned.

What is the correct way to generalize the Rung Kutta 4 method to more than 1 dimension of space?
Gravitons would be my favorite particle as their existence could prove extra dimensions.
AlternateGravity
Posts: 80
Joined: Thu May 15, 2014 5:45 pm

Re: Rung Kutta 4 in more than 1 dimension of space

Post by AlternateGravity »

Nevermind, it seems I had the correct formula and the inaccuracies must have been from careless mistakes.
Gravitons would be my favorite particle as their existence could prove extra dimensions.
User avatar
testtubegames
Site Admin
Posts: 1148
Joined: Mon Nov 19, 2012 7:54 pm

Re: Rung Kutta 4 in more than 1 dimension of space

Post by testtubegames »

That's super cool, I've been wondering about the difference between RK4 and the Euler method in GSim. I know they are off, just as you show in your first post, and the Euler method (can) diverge much more quickly. But on the other hand, I've been wondering about the best way to maybe do a side-by-side comparison. (Even with change the timestep, for that matter).

Your graphs do a nice job showing off that difference
Post Reply