- [Voiceover] In the first video on Euler's Method
we walked through an example
where we were trying to approximate
the solution to y prime is equal to y
using the initial condition,
start when x is equal to zero y equals one.
And you're probably thinking to yourself,
well, it's very nice that Sal was doing that by hand
and it felt very home-brewed.
But this looks like something
that would be a good job for computers.
And if you were thinking that, you are right
and that is why I took a little time last night
to actually write a computer program
that implements Euler's Method.
And so, what I want to do in this video is one,
show you how to use this program I had
and it's sitting and I'll put an annotation on this video
so that you can click right to it,
but it's sitting on the tutorial on Khan Academy
that is about Euler's Method.
And so, let's first talk about how to use this thing.
So this first thing, this first variable right over here
xStep, this is how much we want to step.
This is how much we want to increase x by for each
when we want to find each next point
in our approximation.
So in our right here xStep is one,
we could make this point five,
we could make this two,
we could make this .001.
As we see in the first video,
the lower the number here
the more precise of an approximation we're going to get.
On these next two variables
are things that you might want to tweak.
These are the initial conditions.
This is the starting x.
And when we're at the starting x
we're assuming that the particular solution
will contain that starting y.
So, the way it's set right now
it says hey, the .01 is in the particular solution.
And if you go a little bit lower
this is the function slope at point.
I guess I could have called it derivative at point as well
or tangent slope at point.
Those would involve legitimate name for this function.
This is really where I define
what the differential equation is.
And so you see it says like,
it just says return y right over here.
What it's saying is what it's going to return
is the slope at that point
and you pass it at x and y
and then it will give you the slope of the point.
So this is saying that the slope of that point is equal to y
or another way interpreting it is
is that y prime is equal to y.
And so, this is the way it's defined now.
The differential equation that we looked at
in the first introductory video
that at any point the derivative is going to be equal to y.
And since we stepped by one we see that.
Where the .01, our slope is going to be one
until our next step, until we increase our x by one
we get to this point.
Now our slope is two.
We increase x by one and then our y goes up by two.
Now our slope is four.
We increase x by one, we increase y by four.
Now we could do a different function here.
You could say that dy, dx is equal to negative x
divided by Y.
And we would get something like this.
If our xStep is just one,
if we have this initial condition
our initial condition could be something like that
and you say even with an xStep of one
something as course as that
we're getting a pretty good approximation right over here
and if we want to get a better approximation
we can make this smaller.
0.5 is even better.
If we wanted to get even better, 0.1.
And you might have noticed something
that just happened here.
This thing got wacky as we got,
as we started dividing by,
as we started getting close to dividing by zero.
And this is a reminder
that this is really just an approximation tool.
We're not analytically finding the actual solution.
We're just stepping from, you know,
x equals zero to x equals point one to x equals point two,
and we're using the slopes at each of those points
and were you able to figure out the slope
based on this function
to figure out what their next x and y should be.
So I encourage you to play with this.
It's actually a lot of fun.
You can see how you get a much better approximation
as you, let me go back to what we originally had.
You can see how you get a much better approximation
as you get your xSteps being close and closer
and closer to zero.
So, this is you'll notice now when you just...
We have an xStep of 0.1 at x equals one.
This is getting pretty close to E.
It's probably doing a pretty good job
of simulating the exponential function y
is equal E, y equals E to the x.
So the main things to know about, this is your xStep,
these right over here are your initial conditions,
your x and y.
And whatever you put in here,
you're gonna say that dy, dx is equal to that.
Notice I say assumes that your differential equation
can be expressed as y prime
is equal to a function of x and y
which is also a function of x.
And so, whatever you put here,
that's essentially your function of x and y of x.
So if you just have y prime is equal to negative x or y
you put negative x divided by y,
negative x divided by y over here.
All of these is actually no way.
I'd have to raise the initial condition here
to do something like that.
And if you want to now know how this program works,
if you want to modify this
and I encourage you to modify it, make it better
because this I just kind of hacked this together last night.
I'll now explain what the rest of the program is doing.
So these are all the variables
for kind of establishing for drawing the graph paper.
This is our minimum x, minimum y,
maximum x and maximum y.
They kind of fall off the page right over here.
You have your x and y offset.
If you change it you'll notice,
if you change your xOffset it shows
how much we move to the right or the left.
The yOffset it moves everything up or down.
The background, that's just the background color
that we are...
The background color for our canvas.
Then the xScale is really just trying to say
well, how many of the pixels
result to kind of one incremental step in x
or one incremental step in y.
I'm sure I probably could have tweaked this
a little bit better to factor the offset
and that's probably one of the reasons why
I'm falling off the screen.
But anyway, you can modify it if you like.
These next two functions are really
to just map between x and y coordinates and pixels.
So that's that.
Then this is the color of the actual grid.
You can see I could change that.
I could change the color of the grid
to whatever I like.
I like something with a reasonable level of contrast
so I'll leave it there.
And then these two things,
this draws the vertical lines
and then labels the numbers down here.
You see this draws the vertical lines.
This does the labels down here.
Notice it starts at x min, it goes up to x max.
This does the same thing for the horizontal lines
so this right over here is going to go.
So, it starts at the y min, goes up to the y max,
draws a bunch of lines and also labels the y axis.
So that's really just kind of the setup
and then this is the actual function that we call
in order to draw our approximation.
And it makes use of slope at point.
So, for any point you're starting,
you start your x in initX, initial x.
You start your y in initial y.
And then you say look,
while x hasn't gotten to our maximum x yet
figure out or draw a point at that point,
and actually we might not even need that.
I can comment that out.
It doesn't change it really.
That's actually my first iteration,
actually let me take that out.
Let me take that one out
or I'll leave it here for now.
You could modify it as you see fit.
And then it figures out the slope.
It figures out the slope at that,
in that current x and y.
So it uses that function.
So it saying okay, let me input that x and y
and then it figures out
what the derivative is at that point.
And so that it uses that slope
to figure out what the next x and the next y are.
The next x is gonna be x incremented
by the delta that you passed to it.
The next y is gonna be the current y
plus the slope times the delta
and then it just draws a line
from the current x to the next,
or the current point, the x, y point
to the next x, y point.
And then it makes the next x one.
It makes the next x and the next y
the current x and the current y,
and then it just keeps on going and going and going.
And then finally, this is actually where we call,
where we call that function.
And I used the variable xStep
that we defined earlier on at you know, point one,
but if I change this to just...
If I just change this to one
you see that actually makes the change
and I could actually draw multiple approximations
on the same screen.
That actually might help us appreciate.
That actually might help us appreciate
how our approximation gets better
as we get a little bit more granularity.
So let's say let's draw an approximation.
Approximation.
And instead of having an xStep
or a delta x of one every time,
let's have a delta x of 0.5.
And so you see it there.
The delta x of 0.5
it gave me a more accurate representation.
Let me do one more.
Just show how all these works.
Stroke.
Let me change the color.
So for the next line of color let me make it,
I don't know, I'll make it this kind of
this green blue color.
Let me draw.
Whoops, let's draw.
Draw Approximation.
I'll just have to capitalize the A.
Draw Approximation
and I'll make this one even more fine.
So there you go.
That was even a better approximation.
So the blue one is the most accurate one
and you can do this with a bunch of ones.
We could do it with what we saw in the very first video
where we're approximating y is equal to E to the x.
Well that case, the slope at a point
is equal to the Y coordinate.
Y prime is equal to y
and our initial condition is 01,
and you see the blue one
is getting really close to y is equal E to the x
but you could play with all sorts of neat things here.
So, we could do things like...
So we could say that y prime is equal to
I don't know, x plus y.
What does that do?
What if we did x minus y?
Well, that's actually a pretty neat one.
We could change our range.
We can make our x max.
Let's say we want to like zoom in a little bit here.
Let's make our x max equal to four.
And now you get a little bit more fine grained here
and then you could...
Let's change our initial conditions.
What if when x is equal to zero y is a higher y?
And so you can see how your different approximations change.
Anyway, hopefully you found that interesting.
I encourage you to modify this and understand this
because the whole point of Euler's Method
it's a numerical method.
It's to use something like a computer
in order to approximate solutions.