CHAPTER 08.03: RUNGE-KUTTA 2ND ORDER METHOD: Midpoint Method
In this segment, we're going to take an example of the midpoint method of solving ordinary differential equations. So it's the midpoint method of solving first-order ordinary differential equations, and the midpoint method falls under the category of the Runge-Kutta second-order method of solving a differential equation of the form dy by dx is equal to some function of x and y, which is given to you, and y of 0 equal to y0. And the way the midpoint method works out, the numerical method, is y-sub-i-plus-1 is equal to yi, plus k2 times h, where k1 is the value of the function at the point where you are, which is xi, comma, yi, and k2 is the value of the function, f, at the midpoint, that's why it's called the midpoint method, because it's in between xi and x-sub-i-plus-1, halfway through, plus yi plus 1/2 k1 h. So let's go ahead and take an example, so let's suppose somebody gives us that, hey, I'm giving you the differential equation dy by dx is equal to 3 e to the power minus x, minus 0.4 y, and y of 0 is equal to 5. So this itself becomes your function x, comma, y which you need for solving the differential equation. If it were not in that form, then you will have to put it in that form. Then somebody is saying, hey, go ahead and calculate y of 3, when you know that the step size which you are supposed to take is 1.5, which basically means that, since I'm starting from 0, because that's where the initial condition is given, somebody is going to take a step size of 1.5, I'll go from 1.5 to . . . from 0 to 1.5 in one step, and to 3 in another step, so that's how the process is going to work, that I'll have to take two steps to be able to go from 0 to 3, while I'm taking a step size of 1.5. So let's go ahead and take this example and see how we can solve it by using the midpoint method. So I am going to take i equal to 0, so I'm going to choose i equal to 0, that gives me x0 is equal to 0, and gives me y0 is equal to 5, because that's the initial condition, the initial condition is given at x equal to 0, so y0 is 5. So let's go ahead and find k1, what is k1? k1 is the value of the function at x0, comma, y0, and keep in mind that 0 and 0 are going in the subscripts because of the fact that I have chosen i equal to 0. So that gives me the value of the function at x0, which is 0, y0 is 5, so 0, comma, 5 right there, and that will be 3 e to the power minus x, which is 0, minus 0.5 . . . sorry, minus 0.4, 0.4 times 5, because it is 0.4 times the value of y, so 0.4 times the value of y, and that's turning out to be 1. Now, when I want to calculate k2, k2 is the value of the function at halfway between x0 and x1, so it's x0 plus 1/2 h, y0 plus 1/2 k1 times h. So let's see what we get here, x0 is 0, h is 1.5, because that's given to us, that we're taking steps of 1.5, y0 is 5, plus 1/2, k1 is what? k1 is 1, h is what? 1.5. And when I do this, I get the value of the function is at 0.75 and 5.75, that's what I need to calculate the value of the function, f. So in order to be able to appreciate what these arguments mean is that 0.75 is in between 0 and 1.5, it's halfway through, and this is the approximate value of y at this particular point of x, because that's what you need in order to be able to calculate the function, f. So this becomes 3 e to the power -0.75, minus 0.4 times 5.75, and this value here turns out to be equal to -0.8829. So that's the value of k2, and I already found the value of k1, although k2 is the only one which is in the formula for calculating the next value of y, the reason why you need k1 is because you need the value of k1 here, because you have to find out this approximate value of y, so that's why k1 is also needed. So now I plug it into the formula here, I get y1 is equal to y0, plus k2 times h, y0 is 5, k2 I just found to be -0.8829, and h is 1.5, and this value here turns out to be equal to 3.676. So that's the approximate value of y at x equal to 1.5, because that's the value of y at x equal to 1.5, because what is x1? x1 is nothing but x0 plus h, x0 is 0, h is 1.5, so it's 1.5, because the argument of y should be x1, which is 1.5, but that's not what we want to find out. We want to find out what the value of y is at x equal to 3, so we need to take one more step here. So I'm going to assume i is equal to 1 now. When i is equal to 1, I get x1 is 1.5, y1 I just found out, which is 3.676. So based on these values now to start with, I'm going to find k1 and k2. So k1 is the value of the function at x1, comma, y1, because it's supposed to be the value of the function at xi, yi, since i is equal to 1, I need to find the value of the function at x1, comma, y1. That is saying that, hey, I need to find the value of the function, f, at 1.5 and 3.676, and that gives me 3 e to the power -1.5, minus 0.4 times 3.676, and this value here for k1 turns out to be -0.8009. So that's the value of k1, the slope at . . . some value of the slope at x equal to 1.5, but I need to use this value now to find k2, so that I can use it in the formula, so let's go ahead and do that. So k2 turns out to be the value of the function at x1 plus 1/2 h, comma, y1 plus 1/2 k1 times h, and what is x1? x1 is 1.5, h is 1.5, and then I need to find the argument of y, which will be comma, y1, which is 3.676 plus 1/2, k1 is how much? Which I just found out k1, -0.8009, times h, which is 1.5. So the arguments of the function, f, now turn out to be equal to 2.25 and 3.075. So the value of k1 which was needed here in the formula is mainly to be able to find some approximate value of y at this midpoint between 1.5 and 3, which is 2.25 in this case, and that's why we need the value of k1, so that will give me the value of k2. So y2 will be now, in the formula, will be y1, plus k2 times h . . . oh, I didn't write down what k2 is, so k2 will be 3 e to the power -2.25, minus 0.4 times 3.075, because 3 e to the power minus x, minus 0.4 times y, and that turns out to be -0.9138, so that's the value of k2. What is y1? y1 I just found out from the previous approximation to be 3.676, what is k2? I just found out to be -0.9138, and what is h? h is 1.5, which is the step size, and this value here turns out to be 2.304, and this is the approximate value of y at x2, which is same as the value of y at 3, because what is x2? x2 is nothing but x1 plus h, and what is x1? It's 1.5, h is step size 1.5, so I get 3.0. So that's the number which I was looking for, what is the approximate value of y at x equal to 3. Now, if you look at the exact value, so this is part of your homework which I am assigning to you, that the exact value in this case is 2.763, up to four significant digits, that's the exact value by using your knowledge of ordinary differential equations class, and then I'm also going to ask you to calculate the absolute relative true error in this case, which turns out to be 16.57 percent between exact value and the approximate value. So this is your approximate value, 2.304 here, this is your exact value, 2.763, so the absolute relative true error in this case turns out to be 16.57 percent. So that's an example of the midpoint method. And that's the end of this segment.