This screencast is going to show you the basics of the Euler method and how we can solve the
Euler method using Excel and Matlab. The Euler method is used to solve equations of the form
dy/dt equals some function of those two variables. What we are trying to do is determine "y"
as a function of time. There are a couple of key concepts when we talk about the Euler
method. The first is that delta "y" over delta "t" is an estimate for the derivative of dy/dt.
The second concept is that delta "t" (the time step we are going to take) has to be
small. As delta "t" becomes small this method becomes more accurate. We have to know that
function on the right hand side and finally we have to know an initial condition. Usually
time 0 is chosen to be a time of 0 but it does not necessarily have to be. We have to
know the function value. Let's start with dy/dt is some function of "t" and "y". We
approximate dy/dt as delta "y" over delta "t". Delta "y" can be written as y_i+1 ("i"
represents iterations that we are going to take) minus y_i. This is the next step of
the function value minus the current value. Delta "t" is constant so it is going to be
something we choose and that is equal to the next time minus the current time. We can rewrite
it as delta "y" equals delta "t" times the function at the current time step and function
step. If we expand that delta "y" and bring over the y_i we can write it in this form.
y_i+1 is the current function value plus the times step times the current function value.
Let me show you this graphically. The way this works is we must start with the initial
conditions. That is this point here at t_0 and y_0. We are going to choose a very small
delta "t" and the first step using this formula on top is to calculate the first point besides
the starting point. y_1 is equal to y_0 plus delta "t" times the function value at our
current combination of time and "t". The next time is just equal to the current time plus
delta "t". When we do that we can calculate y_1 and t_1. We can plot that and we are just
basically finding an estimate for that next function value "y" at the next time step.
We do this now for a second time step. y_2 is equal to y_1 plus delta "t' times the function
value now at t_1 and y_1. That is our t_2 and y_2. We can do this for a third point
and we can keep on doing this. Essentially what we are doing is we are building this
function "y" as a function of "t". We are doing this just based upon the fact that we
know the equation for the slope (dy/dt) at any given time. If we zoom way into the function
at any given time point and the next time point which estimating the slope as delta
"y" over delta "t" and as shown here we are going to be calculating this value. Because
that value depends upon the current. This is what we currently know. We are projecting
the next value. We are assuming that it is a triangle. We know the slope because the
slope is estimated as delta "y" over delta "t" and that is equal to the function that
we have. This technique gets very accurate as delta "t" gets smaller. We can take very
very small times steps. Then each consecutive time step you are calculating a slightly different
slope and so you can estimate the actual function knowing the derivative. Let's go through an
example. We have dy/dt is equal to 14t-3. We have our initial condition. We know that
at time 0 our function value is equal to 5. If you have taken calculus you can easily
integrate this. In general you are going to have a very complicated formula but that is
the utility of the Euler method. We might ask something like "at what time t will the
function value y be equal to 300". We are going to use the Euler method for this. I
have written my function here of dy/dt is equal to 14t-3. The Euler method we estimate
dy/dt as delta y over delta t. That is equal to 14t-3. You notice here that I have added
this subscript "i". We want to calculate that at the current time value. If I expand this
and solve for y_i+1 we see that is equal to y_i plus our delta t ( in this example I am
just going to choose that to be 0.1) times 14t_i plus 3. We have been given an initial
condition so at time is equal to 0 our function value is equal to 5. If I write the first
time step y_1 is equal to y_0 (we know y_0 because that is 5) plus our time step times
14 times our initial time (which is 0) minus 3. Knowing y_0 our initial "y" I can calculate
the next "y" as 4.7. The next time step, y_2, is equal to 4.54. I can keep going with this
for y_3, y_4 and so on to build this function. Obviously when we decrease our time step this
becomes more accurate but it does consequently lead to more computing time. I have this set
up in excel. I have my delta "t", I have my formula and initial conditions. I am going
to auto fill the time step down to about 10 seconds. Now using the equation that we just
derived I end up with this equation where I have delta "t" set up here. The time has
to be the previous time and I can copy that down. Now if I wanted to I could plot this
and compare to the actual solution. I have set up a column here of the actual values
based upon our actual solution from calculus. Now I can put in an error which is just the
difference between the actual and our estimated using the Euler method. You see that once
I calculate the error it becomes pretty big once our time step is really large. If I wanted
to I could change this to a much smaller value like 0.02. Having a smaller time step will
lead to a decrease in error. Finally I want to show you how to do this in Matlab. I have
set up my simple equation as a function here. Keep in mind that this equation is the right
hand side so dy/dt is equal to 14t-3. I have also set up a function called Euler. It is
a function of a function because we want to write it so that we can put any function into
Euler and it will spit out the result. I have a function here which is going to be something
the user puts into the arguments of the Euler function. I have y_0 which is our initial
"y". I have the time step that the user can choose and I have the final time. I have created
a vector here of time. y_1 is our first element of the y vector which is just y_0. For "i"
is equal one to length I am putting in that equation. Next the function value is equal
to the current function value plus our time step times evaluating our function at the
current time step. Then I am plotting this and labeling it. I can type into the command
window Euler at our equation, 5 (our initial function value), 0.5 (time step), 10 (final
time). I can run this function and plot it. Along with that Euler plot I am going to plot
the actual solution from0 to 10. You will notice that the actual which is in red here
is quite a bit higher than the blue one. I can reduce that error by increasing the time
step. Now I have reduced the time step to 0.05 and replotted this. You can see that
our estimate using the Euler method is pretty much spot on to the actual solution. This
how you can solve simple equations using the Euler method. It can get a lot more complex
than this.