# Runge Kutta (RK4) help

• Dec 19th 2009, 05:24 PM
BWilson
Runge Kutta (RK4) help
I am having difficulty approximating the solution for y' = x^2 using the RK4 method. For some reason, I don't understand how to go about the method for equations such as this one, although it seems more straight forward when there are two clear variable in the equation (ex: x^2 + e^y). Anyway, if someone could show me how to approximate y' = x^2 with the RK4 method, that would be highly appreciated. The more work shown, the better off I would be (because I am truly confused by this.) Thank you very much for your time.
• Dec 19th 2009, 07:47 PM
AnonymitySquared
Quote:

Originally Posted by BWilson
I am having difficulty approximating the solution for y' = x^2 using the RK4 method. For some reason, I don't understand how to go about the method for equations such as this one, although it seems more straight forward when there are two clear variable in the equation (ex: x^2 + e^y). Anyway, if someone could show me how to approximate y' = x^2 with the RK4 method, that would be highly appreciated. The more work shown, the better off I would be (because I am truly confused by this.) Thank you very much for your time.

If you have an equation $\frac{dy(x)}{dx} = f(x,y)$, and you want to integrate it to find $y(x)$ with a stepsize $h$, then the equations for 4th Order RK are as follows:

$k_1 = h f(x_i,y_i)$

$k_2 = h f\bigg(x_i + \frac{h}{2}, y_i + \frac{k_1}{2}\bigg)$

$k_3 = h f\bigg(x_i + \frac{h}{2}, y_i + \frac{k_2}{2}\bigg)$

$k_4 = hf(x_i + h, y_i + k)$

$y_{i+1} = y_i + \frac{1}{6}(k_1 + 2k_2+2k_3+k_4)$

So, in your case $f(x_i,y_i) = f(x_i)=x_i^2$.
• Dec 19th 2009, 08:16 PM
BWilson
thank you very much
That was a very helpful post. So, if I am doing this right, I can estimate
the following for a time step and init. value of 1:
k1 to be the initial value of x^2 (0 to begin with) which is 0;
k2 to be f(0+1/2) = .5^2 = .25;
k3 to be f(0+1/2) = .5^2 = .25 (so previous k's aren't even affecting values?)
k4 to be f(0+1) = 1^2 = 1
final answer to be 1 + (1/6)(0 + .25(2) + .25(2) + 1) = 4/3

I guess what confused me was that, for example, k2 had no effect on k3 in
this particular case. Anyway, thank you for clarifying. I'm still not sure if I've got this down, so if anyone would care to correct my work above, please do.
• Dec 20th 2009, 10:44 AM
AnonymitySquared
Quote:

Originally Posted by BWilson
That was a very helpful post. So, if I am doing this right, I can estimate
the following for a time step and init. value of 1:
k1 to be the initial value of x^2 (0 to begin with) which is 0;
k2 to be f(0+1/2) = .5^2 = .25;
k3 to be f(0+1/2) = .5^2 = .25 (so previous k's aren't even affecting values?)
k4 to be f(0+1) = 1^2 = 1
final answer to be 1 + (1/6)(0 + .25(2) + .25(2) + 1) = 4/3

I guess what confused me was that, for example, k2 had no effect on k3 in
this particular case. Anyway, thank you for clarifying. I'm still not sure if I've got this down, so if anyone would care to correct my work above, please do.

Your working here is correct, but your solution isn't really very useful. What did the original question ask you to do? Just to find the solution at x=1?

If so, then what you have done is correct. However, it is common practice to use a stepsize that is lower than 1. The error of the 4th order Runge-Kutta method is of the order of $h^4$. For comparison, the error of the Euler method is the order of h. So, if you use h = 1, then for both the Euler method and the RK4 method, your error is of the order of 1. So really, by using RK4 instead of Euler with a stepsize of 1, you are giving yourself 4 extra calculations but not getting any benefit from them.

If, however, you used a stepsize of say, 0.5, then the error of RK4 would be of the order of (0.5)^4 = 0.0625, whereas the error of Euler would be of the order of 0.5, and hence, with h = 0.5, you can see that there IS a benefit of using RK4 instead of Euler. The only pain is that, if you're doing this by hand, using h = 0.5 means you have to iterate the whole process twice to get to x = 1.

In this particular instance, the step size doesn't matter because dy/dx is not a function of y, only x. But bear in mine that in any other instance, you would accumulate large errors using h = 1.

Numerical methods like these are usually implemented in computers which are able to perform the calculates very quickly, and over a large integration interval. An integration interval of [0,1] isn't terribly useful!
• Dec 20th 2009, 11:54 AM
BWilson
Thanks again
Thanks for explaining the error - I never really thought about that. Indeed, integrating over that interval isn't useful at all. The real intent of this was just to get an understanding of the method. It's funny that you mentioned their applications in computing, because that is exactly where I plan on implementing this (I was working on a C# struct to do factorials and the gamma function when I realized I had no way of integrating it). I have no idea if an RK4 method is the best solution for this problem, but it seems it will work passably well. Thanks for your help once again!
• Dec 20th 2009, 12:10 PM
AnonymitySquared
Quote:

Originally Posted by BWilson
Thanks for explaining the error - I never really thought about that. Indeed, integrating over that interval isn't useful at all. The real intent of this was just to get an understanding of the method. It's funny that you mentioned their applications in computing, because that is exactly where I plan on implementing this (I was working on a C# struct to do factorials and the gamma function when I realized I had no way of integrating it). I have no idea if an RK4 method is the best solution for this problem, but it seems it will work passably well. Thanks for your help once again!

RK4 is a relatively high order method (in fact, most of the inbuilt integration functions in numerical software packages such as MatLab use variations of RK4). So, with a sufficiently small stepsize, RK4 will work almost perfectly for most integration problems.

You can extend RK4 to RKn, i.e., you can get pretty much any order you desire with enough k equations.

However, if you are only really looking at integration, rather than integration of differential equations, then you might want to look at quadrature methods of integration. For example, if you have a function that you know analytically (which I assume you do since you're working with the Gamma function), and you want to integrate it, the Gauss formulas provide a method where YOU choose the accuracy, and in fact, you can eliminate the error ENTIRELY, and it's actually rather an easy method to implement, and not nearly as computationally expensive as some of the higher order methods for ODEs.