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.
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 . 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!
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.