Let's talk about ways to solve ordinary first-order differential equations numerically.
The easiest way is Euler's method.
So first we need to set up our differential equation.
Our differential equation is going to be defined by some expression dx/dt equals some function
of both x and t.
For a function of only t we could solve it with subset zero or some other numerical integration formula,
but it's a function of specifically x and t.
I noticed that there's only one derivative, we got one independent variable t, and then
the dependent variable x.
We specify the main part of the differential equation, but we still need to specify an
initial condition since it is a differential equation.
We got our initial condition, there's some t(0), it doesn't have to be 0, it could be
any value, and we know the value of f(x(t)) at that point.
We pick our t=t(0), and we know the value of the function right there.
Now we want to draw the entire function line, but for now we just have a single point.
So we want to know how can we extend our knowledge of this function from a single point to the
rest of the curve.
Well, we can do a Taylor Series expansion of the value of the function around the point
t(0), and so if we just do a first order expansion we get x(t(0))+t is going to be equal to x
at t(0) plus the dx/dt times some small interval, delta(t).
Now the thing is we know what dx/dt is, it's just the function that's defined in our differential
equation, so this is very easy to calculate once we have our initial point.
We calculate what that derivative is, and the derivative is the slope, so we know the
slope of the function and we follow that slope of the function up and see how the function
changes over some interval delta(t).
So go over delta(t) and we have a new point.
That point right there is just x(t(0))+f(x,t)*delta(t).
So that is our guess for the next point of the function.
Now we're there we can find the derivative at that point by plugging into our function
and that's it, we just keep repeating that, we keep repeating.
We can now go out to t(0)+2*delta(t), and keep on going as long as we want to.
Now of course this is not going to be a great estimate unless delta(t) is quite small.
It turns out this method is only border delta(t), so we'll approximately have the error when
delta(t) is produced by a half.
That is the global error overall, but if you get off a little bit the error keeps accumulating
at each point, so you do need to have a fairly small delta(t) in order to get a good answer.
Let's take an example, let's define our f(x,t)=3*x^2+t^3, and we'll say OK, our initial point is t=0.2,
and we'll say at that value the function is 0.1.
We can relate this to anything we want to, the specification of the initial point is
independent of the definition.
We got that so we choose some delta(t), let's choose 0.1, and then what we get is, what
we're solving for is x at 0.3, we know x at 0.2 we want to solve it at 0.3.
So then we evaluate the function at x(0.2), and multiply by delta(t).
Now of course x at 0.2 that's what our initial specification of the problem is, and so that's
just 0.1, so we just need to plug 0.1 and 0.2 into that function, 3*x^2+t^3.
So we can plug that in and we get a value.
Notice that's what's in the square brackets is the slope at that initial point, so we
multiply the slope times delta(t) plus 0.1 we get a solution for the first point using
Euler's method.
So that's great, that's the simplest way, but it turns out to be not efficient.
It's not that efficient because no, we're not using a very good guess for the slope.
So we want to look at ways we can use better guesses of the slope, algorithms that are
order delta(t)^2, it'll scale with the square of the change in our delta(t).
So Heun's method is one fairly straightforward second-order method where essentially including
the quadratic terms.
It's not that clear from the definitions that the quadratic terms are being included, but
you can see they are in the end.
We'll talk a little bit more about it when we talk about general Runge-Kutta methods.
Again, we specify the problem.
Same as before, we have some dx/dt, first-order differential equation is equal to some function
f(x,t), and we have some initial conditions.
So we start out with our initial conditions, we start out with our single point that we
know, we want to sketch in the rest of the function.
We start out in exactly the same way as Euler, we say OK, the value of the function at this
new point, at t(0)+delta(t) would just be equal to the value of the function at that
point plus the derivative, which is just f(x,t) times dt times our change in our independent
variable.
We plug that in, we find the value of the slope, and then we go out to the new point,
find the new point, and for Euler that's where we'd stop, but for Heun's that's just the
first step.
This is the predictor half, and then we still need a corrector half where we go back and
correct to get a more accurate value of the slope.
So what could we do?
We could use our function again, we're using delta(t) equals 0.1 as well, and so x(0.3)
is equal to what it was before, remember this first step is exactly what we did for Euler,
and we get a first guess for x(0.3) is, but we want to refine that guess with a better
estimate of slope.
So what we do is we take the value of the slope at that new point and then we average
the new slope, our blue line, with our old slope, the red line, and get a third slope,
the average of those two.
Since the average of those two slopes at the two different points, and since those are
the slopes at the end points the average is a better estimate for the slope in the middle.
We take that average slope and we take a full step, so it's the same t(0)+delta(t) that
we used to evaluate Euler's, but now we're using this more accurate slope.
So plugging that in we need to know what the value of the derivative is at that new point,
and can we get that out, you'll notice that the derivatives increased a little bit, the
functions increasing.
Then we average the two slopes together.
The first slope is the slope we got before, the second slope is the new slope at the end
point, so we average those together, multiply by our delta(t), grind through the calculations,
we have our average slope, 1.15, which is larger than the initial slope that we guessed,
0.9567, and we get our new value.
Great, so this should be more accurate because we're using a more accurate value of the slope.
Now there are more than one methods that are second-order, so let's take a look at the
midpoint method, which is another second-order method.
So again we have our definition of our differential equation, we got dx/dt=f(x/t), we have our
initial point exactly the same as before, and so we got that point, and we're now going
to try to solve for the rest of the plots.
So we start out the same idea, we do a Taylor Series expansion, we want to find out the
value of the function slightly further a long, so we got that, but what we do is we don't
go out for the full step, we go out only half way, so we go out to a half step and we find
the point of that half step and then evaluate the slope at that half step.
Now instead of doing a full step of Euler we're doing a half step of Euler, and notice
the slope is the same, as if we're doing the full step, we're still on the same line, but
we're only going half as far a long that line.
So we get a value of the function at the half step, and now we can compute what the derivative
is at that half way point, so this should be a more accurate value of the derivative
than when we're starting out because we're including some of the change that happens
a long the slope of the curve, so we got that half step and now what we do with that slope
is compute it at the half step, so we go through and if you recall the average, the beginning
and the end slopes in Heun's then was 1.15, so this is actually fairly close to the slope
we're using for Heun's, and so then we can just move that slope down, and use the same
slope we computed at the midpoint, but use it from the first point, and then we take
a full step with that slope measured at the halfway point.
Plug it in, get that, just grind through the math, and we got our new slope times 0.1, plus
the initial value, and we get another, also more accurate, value for the function at 0.3.
This is also a second order quadratic method because we're using an in between estimate
of the slope, we're using information in the middle of the segment instead of at the beginning
of the segment.
If you recall the central difference, when we took a look at the slope of the middle
we were able to include the second-order effects, so if we compare this to Euler we can see
it's a little different from Euler.
If we compare it to Heun's, which is also a second-order method, we can see we get a
value pretty close to Heun, so those two more accurate methods agree with each other, we
can see that we're getting fairly close to the right answer now, these two different
methods agree fairly closely.
So that's a basic walkthrough of the graphics, of the visual display, and some examples of
these three methods, the Euler method, the simplest method because all we're doing is
a linear Taylor Series expansion, and then Heun's and the midpoint methods which are
both quadratic methods.
We didn't explicitly include the second order derivatives, but as we'll see next time we
sort of implicitly included them, wrapped them into this by using information of the
slope half way across.