CHAPTER 08.03: RUNGE-KUTTA 2ND ORDER METHOD: Heun's Method

 

We're going to take Runge-Kutta second-order method of solving ordinary differential equations, and we're going to take an example of how to solve a problem by using Runge-Kutta second-order method. We're going to use Heun's method as an example.  There are other two methods, which are Ralston's method and midpoint method, but once you understand one of the methods, the other methods are very easy to apply.  So we're going to take the differential equation dy by dx is equal to 3 e to the power minus x, minus -0.4 y, y of 0 is given as 5.  So what we want to do is we want to solve this differential equation here, we want to find out what the value at y at 3 is. What is the value of y at 3 by using Heun's method?  And we're going to use a step size of 1.5, which basically means that, if I'm going to start my initial value from 0, I will go in one step I'll be at x equal to 1.5, and in two steps I'll be at the required point of 3.0.  So let's recall our equations for the Heun's method are as follows, y-sub-i-plus-1 is equal to yi, plus 1/2 k1 plus 1/2 k2, times h, where k1 is the value of the function at xi, comma, yi, and k2 is the value of the function at xi plus h, comma, yi plus k1 times h.  So what the . . . what the Heun's method application will basically involve is that we're going to choose a value of i, let's suppose 0 to begin with, i equal to 0, we're going to calculate the value of the function of k1, we're going to calculate the value of the function of k2, plug it into here, and then take half of each of those, multiply by the step size, we know what the previous value of y is, and we'll be able to get the new value of y.  So that's how it's going to work out, I'm going to show you the two steps, and let's take it from there. So I'm going to choose i equal to 0, so if I choose i equal to 0, I need to know what x0 is. x0 is 0, and y0 is 5, and this is because this is the starting point which is given to me, this is from the initial condition, that the initial value of y at x equal to 0 is 5, so I know those quantities.  Now I will need to calculate my function, f, x, comma, y, is nothing but 3 e to the power minus x, minus 0.4 y, and that's from the form of the differential equation which is given to us.  So I need to calculate my k1. So k1 is, at i equal to 0, it'll be at the value of the function at x0, comma, y0, and what is x0?  x0 is 0, y0 is 5, so that is same as 3 e to the power -0, minus 0.4 times 5, and that gives me 1.  So that's the value of k1 which I am getting for the first step.  Now I need to calculate what the k2 is.  k2 is the value of the function at x0 plus h, comma, y0 plus k1 times h, and what is x0? x0 is 0, h is 1.5, y0 is 5, k1 I just calculated, which is 1, and h is 1.5, so that means that, in order to calculate k2, I need to calculate the value of the function at 1.5 for x, and the value of y being 6.5  You do need to understand this 6.5 is an approximate value of y at this particular point, because in order to be able to calculate the value of the function, f, which is the slope of the dependent variable with respect to the independent variable, you do need to know some value of y.  This is not an exact value of y, some approximate value of y which is almost like an Euler's method, which is an Euler's method approximation here, but you do need some value of y to calculate the value of the function, f, there.  So this becomes 3 e to the power -1.5, 3 e to the power minus x, x is 1.5, minus 0.4 times y, which is in this case, 6.5, so the value of k2 is turning out to be -1.9306. So we have calculated the values of k1 and k2 starting at the point x0, and then we can just simply calculate what the value of y1 is.  So y1 is nothing but 1/2 . . . it's y1 is nothing but y0, plus 1/2 k1 plus 1/2 k2, times h. The starting value of y0 is 5, the value of k1 which we calculated was 1, the value of k2 we just calculated was -1.9306, and the step size is 1.5, which is h. So you're taking the average value of the slope at the point where you are and the slope where you want to go, and taking half of those quantities, and this turns out to be 4.302.  So that's the value of y1, which is basically saying that, hey, this is the approximate value of y which you get at 1.5, because y1 is nothing but x1, x1 is nothing but 1.5 points . . . units ahead of the starting point, which was 0, so that's the approximate value of y at 1.5, but that's not what we want to find out.  We want to find out what the value of y is at 3.  So in that case, I'm going to choose i equal to 1, x1 is now 1.5, and y1 is nothing but 4.302, because I just found that out.  So what is k1? k1 is the value of the function at x1, comma, y1, and that's the value of the function at 1.5, comma, 4.302, and that is 3 e to the power minus x, which is 1.5, minus 0.4 times y, which is 4.302, and this value of k1 turns out to be -1.0519. So that's some approximate value of the slope which I am getting at this point, 1.5.  Similarly, I'll calculate k2. What will be k2?  k2 will be the value of the function at x0 plus . . . sorry, not x0, but x1 plus h, comma, y0 . . . y1 plus k1 h.  It's very important to write down all these steps, because then you understand what is going on, x1 is 1.5, h is 1.5, y1 I calculated from the previous step, which was 4.302, k1 I just calculated, which is -1.0519, h is 1.5.  So the value of the function which I have to calculate will be at the argument of value of x equal to 3, and the value of y being at 2.726, that's what I'm going to get for k2.  So from here, again, I will be able to calculate y2 is y1, plus 1/2 the value of k1, plus 1/2 the value of k2, times h.  So you've got to understand, k1 is some estimate of the slope at x equal to 1.5, and k2 is some estimate of the slope at x equal to 3.  I didn't write this number down, it's -0.9406.  So y1, which I calculated from the previous step, is 4.302, plus 1/2 k1, k1 is -1.0519, plus 1/2, the value of k2 which I calculated is -0.9406, times h, which is 1.5, and this value here turns out to be 2.808, and that is the approximate value of y at 3.0 which we are looking for in our . . . in our solution.  The exact value, which you get from using your knowledge of ordinary differential equations course, is y at 3 is exactly equal to 2.763, up to four significant digits, and this gives us an absolute relative true error of 1.63 percent, that is the difference between this number and this number, turns out to be the absolute relative true error of 1.63 percent.  So that's how we can . . . if you want to get . . . how we can use the Heun's method, if you want to get more accurate results for y of 3, you just make your step size to be half of what it was before, which will be 0.75, in that you're going to take four steps, and you will see that your relative true error is going to decrease in that case.  And this is the end of this segment.