By the end of this section, you will be able to:
Euler's method (pronounced "Oi-lers") is a powerful tool for applying physics to real world problems. Often times we know how a system changes, and we want to figure out what the final state of the system will be. For example, often we know the acceleration of an object (that may not be constant) and we want to determine how the object will move through time. Maybe we have a system that has a large number of objects in it and we need to be able to keep track of how they interact with each other. Euler's method is a way that we can break down a hard problem into smaller easier problems. By doing so, we can apply the principles of physics to more realistic situations. In this section, we will explore Euler's method in 1 dimension.
In essence, Euler's method is about breaking a non-constant acceleration problem into many small constant-acceleration problems. Suppose we have an object and we know it's acceleration (that may not be constant). We want to determine the final position of that object after a certain amount of time. With Euler's method, we break our time interval into many smaller time intervals. We choose these intervals to be small enough that we can consider the acceleration to be approximately constant, for a small change in time (or position). In fact, this small time interval is so small that velocity is approximately constant as well. By breaking down the problem into many small intervals, we can use the simple equations and to figure out the new velocity and position. We then move to the next small interval, using the answer from the previous as the initial conditions for the next.
There's one more tool we need to introduce in Python before we can write an Euler's method code. Here we introduce loops. Specifically, we will use a while loop. A while loop is a command we use in Python to run a certain piece of code over and over until a certain criteria has been met. For example, we might repeat a certain code until the object we are considering has reached a certain point, or until the time has reached a certain value. Below is an example of a while loop that keeps adding 0.1 to a number until that number is greater than 5.
Notice that we can write over a variable that we have already set. (We have often done this when doing unit conversions.) The code inside the loop is the code that is indented with 4 spaces.
Here the code inside the while loop runs multiple times, until t
has been written over enough so that it is greater than 5. As you might expect, unindented code after the while loop runs after the while-condition becomes no longer true.
Be careful that when you make a while loop that it will eventually end. For example, if you were to call while 1 < 2: the code inside the while loop would run forever. In this textbook,
if you ever encounter an infinite loop, you can reset it by reloading the page. Running an infinite loop might slow down your browser though so it's best to avoid it.
We are now ready to write a code for Euler's method. We will start by defining a function that determines the acceleration. As an example we will consider a ball thrown upward with acceleration .
After defining the acceleration, we need to give the starting values for time, position, and initial velocity. Suppose the ball starts with a height of 1 meter and an initial velocity of 3 m/s.
We want to determine the time it takes for the ball to hit the ground, so for our while loop condition, we will run as long as y > 0.
Inside the while loop we are solving a small constant acceleration problem. This small constant acceleration problem is calculating how the values will change over a small time interval, which we will call dt, also known as the step size.
The quality of our answer will be better if dt is sufficiently small. This is an unusual constant acceleration problem where velocity is also costant.
We will use the current velocity to calculate the new position of the ball (after that small interval) using the equation .
Then in a similar manner, we will use the acceleration function to calculate the new velocity of the ball using the equation .
The new velocity will then be used the next time the loop is run. We also need to update time as we go with .
Let's see how that works.
To summarize, we started by defining the acceleration function (the way the system changes). Then we defined our initial conditions. While we might have labeled initial values with y0 and
v0 earlier in the chapter, here we just called them y and v since we are going to write over those values later on.
After defining the initial conditions, we looped for over several small constant acceleration problems, using velocity to update position, and acceleration to update time. Notice how in the code, we
need the parenthesis and the function input (when we write v + a(t)*dt) since acceleration is a function. Doing it this way makes it easier when we want to start doing more complicated accelerations.
Velocity did not need parenthesis (we wrote x + v*dt) because velocity is just a number (not a function).
Try changing the acceleration and the initial conditions. Try solving a constant acceleration problem you have done earlier using the code above.
Now that we've used Euler's method to solve a problem, let's see how we can modify our code to create a plot of position vs time. Recall that to make a plot,
all we need is a list of x points and y points. We will do this using lists (not arrays). In python, you can add a point to a list by calling mylist.append(newitem).
The thing you put inside the parenthesis will be added to the list. Since we have a loop in our Euler's method code, all we need to do is add a point each time we run through the loop.
We will modify the previous code by creating two empty lists at the beginning. Then inside the loop we will add the current values to our lists. To make things interesting, we will create lists for time and position, but you could make a list for velocity and acceleration too if you wanted. Try the code below.
Using some of the fancy plotting code we used in the numerical methods example from section 3.3 , you could make a plot of position, velocity, and acceleration all next to each other. All you would need to change in the Euler's method code is to add lists for velocity and acceleration as well.
Suppose we have a rocket whose acceleration is given by . The rocket is launched from the ground and starts at rest. Our task is to determine how high it travels, and how long it will take to return to the ground.
We can modify the Euler's method code above to find the time it takes the rocket to hit the ground again, and to plot it's height vs time graph. We will have to change the acceleration at the top of the code. Also since the rocket is starting at , we need to modify the condition on the while loop so it still runs if , but not when . The time step is also plenty small and can probably be made larger. That way the code doesn't take quite so long to run because it won't have as many constant acceleration problems to solve.
By reading the graph, we can see that the rocket traveled about 3.7 km before returning back to earth, and it reached the ground after about 70 seconds.
We saw that with relative ease, we could modify an existing code to apply to a new problem. This time we had a problem where the acceleration was not constant (a little more realistic). We could continue to add details to the acceleration to make it even more realistic. Later in this course we will learn what kinds of things cause different accelerations, and that will help us in improving our model.
We have seen that Euler's method allows us to computationally simulate a real world scenario. The is really no limit to how much detail we can put into our model once we have it written as code. We can make acceleration change depend on time, position, or anything else.
Remember that we simplified the problem by picking a time interval that was very small, and assuming acceleration and velocity were constant. You should recognize that this can't actually be true. Velocity cannot be constant unless acceleration is zero. While this tool will never give us the exact answer, it can still be very powerful. In physics our job is often not to get the right answer, but to get the one that is close enough to be useful.
Hopefully you can now see how being able to do physics in python expands our ability to tackle difficult problems. As we move through the rest of the book, we will learn more principles of physics. Each of these principles are relativiely simple on their own, but being able to put them together to solve a difficult problem is what makes them useful, and frequency code is the tool that we use to do so.