(fill in your two names here)
Facilitator: (fill in name)
Spokesperson: (fill in name)
Process Analyst: (fill in name)
Quality Control: (fill in name)
If there are only three people in your team, have one person serve as both spokesperson and process analyst for the rest of this activity.
At the end of this Lesson, you will be asked to record how long each Model required for your team. The Facilitator should keep track of time for your team.
When we make models and approximations, we often have either expected outcomes or real data that we want to compare. Sometimes we want to look at several models to see which is the best fit to the properties of the data. We will practice this using the free fall scenario that we developed in Lesson 23 including our exact solution, Euler's Method approximation, and we will add another approximation to compare and see which better represents reality.
paste and run your working, docstrung, and commented code from Lesson 23 Team below
In [ ]:
## replace with your working, docstrung, and commented Lesson 23 Team code
Let's run the simulation through a few sets of test values to see what happens.
## generic form (with parameter explanations):
# y0 is y position at time=0
# v0 is velocity at time=0
# tmax is maximum time to run simulation
# dt is the change in time for one time step
FreeFall(y0, v0, tmax, dt)
## for example
simulation = FreeFall(100,1,30,1)
test your code - run the simulation and plot approximate and exact values to compare - using the following values:
y0 | v0 | tmax | dt |
---|---|---|---|
100 | 1 | 30 | 1 |
100 | 1 | 30 | 0.5 |
100 | 1 | 30 | 0.2 |
100 | 1 | 30 | 0.1 |
1000 | 1 | 30 | 0.1 |
1000 | 1 | 30 | 1 |
In [1]:
## simulation runs and plots here
1. Compare your results with the other people at your table. If you don't have enough people at your table, talk to a neighboring table. Do your FreeFall
simulations generate the same results? If not, which simulation is correct, and how do you know?
2. Now examine each team's code. Just this once, you should compare each other's code, line by line if necessary. As you are looking through the code, confirm that no one has any off-by-one errors. For example, the first simulation (30 time units, with a dt of 1) should have 31 positions (starting at a time = 0, and running to a time = 31).
2a. Which program is easier to understand, and why? (It's likely that there are advantages to each of the programs that are listed - if so, explain any features that make the code easier to understand.)
2b. List all the instance variables used in your program, and compare them to the list for the other people at your table. Confirm that you are using local variables (those not preceeded by self) where they are appropriate, and not over-using instance variables (those preceeded by self). If you have some instance variables that should have been local variables, make that note here.
2c. Explain when you should use a local variable (as opposed to an instance variable).
3. What happens to the numerical (approximate) results using the Euler method, when compared to the analytic (exact) results, as $\Delta t$ is made smaller? Explain and justify your answer.
Make sure that as you work on these programming tasks, you are using your best pair programming technique (both driver and navigator) and switching drivers frequently.
Your code must have docstrings and comments for you to receive full credit.
Now, let's go beyond visual analysis and eyeball error rates:
A. write a method called calculate_error
that:
Calculating the difference is much easier than a loop since both sets of results are arrays.
In [ ]:
Test your calculate_error
method and briefly discuss the results
In [ ]:
B. write a method called max_error
that:
Be sure to use the absolute value of the difference to calculate maximum error.
In [ ]:
Test your max_error
method and briefly discuss the results
In [ ]:
C. write a method called plot_error
that:
In [ ]:
Test your plot_error
method and briefly discuss the results
In [ ]:
test your code - each row of values in the table below, do the following:
y0 | v0 | tmax | dt | max error |
---|---|---|---|---|
100 | 1 | 30 | 1 | |
100 | 1 | 30 | 0.5 | |
100 | 1 | 30 | 0.2 | |
100 | 1 | 30 | 0.1 | |
1000 | 1 | 30 | 0.1 | |
1000 | 1 | 30 | 1 | |
10000 | 1 | 30 | 1 |
Also, fill in the table above with column that lists the maximum error for each simulation.
4. Explain the relationship that you see between y0
and error.
5. Explain the relationship that you see between $\Delta t$ (dt
) and error.
6. Discuss your overall conclusions about the behavior of the Euler Method (especially with respect to how its error behaves).
As we saw in the previous Free Fall lesson, Euler's method can give us a numerical approximation of a differential equation. The equations that our Euler method Free Fall approximation used are for position:
$$y(t+ \Delta t) = y(t) + v(t) \cdot \Delta t \hspace{30mm} (1)$$ and for velocity:
$$v(t+ \Delta t) = v(t) + g \cdot \Delta t \hspace{35mm} (2)$$
and since $g = -9.8 \ m/s^2$: $$v(t+ \Delta t) = v(t) + -9.8 \ m/s^2 \cdot \Delta t \hspace{18mm} (3)$$
Main idea: use the current position and velocity to predict the next position and velocity.
The Euler Method can be an OK approximation, but there are limitations. It is especially error prone if the time steps are large, the rate of change is large, and/or there are changes in the sign of the slope.
The Euler-Richardson method calculates the velocity at the middle of the interval, instead of at the beginning or at the end of the interval. This algorithm then uses this $v_{midpoint}$ value for velocity and the regular Euler method to approximate the next values. This method works especially well for forces that depend on velocity.
First, we need to calculate the veloctiy at the midpoint($v_{mid}$):
$$v_{mid} = v(t) + \frac{1}{2} \cdot a(t) \cdot \Delta t$$where $a(t)$ is the acceleration, and in this case, we're lucky since that is the same value for gravity that we've been using so far - $g = -9.8 \ m/s^2$. So we can simplify this equation to:
$$v_{mid} = v(t) + \frac{1}{2} (-9.8 \ m/s^2) \cdot \Delta t \hspace{24mm} (4)$$Now that we have $v_{mid}$, we will use this equation for the next position:
$$y(t+ \Delta t) = y(t) + v_{mid} \cdot \Delta t \hspace{30mm} (5)$$
The equation for the next velocity is: $$v(t+ \Delta t) = v(t) + a_{mid} \cdot \Delta t$$
where $a_{mid}$ is the acceleration at the midpoint, which again is just $g = -9.8 \ m/s^2$, so we can simplify this equation to:
$$v(t+ \Delta t) = v(t) + -9.8 \ m/s^2 \cdot \Delta t \hspace{23mm} (6)$$
Main idea: use the current position and the velocity $\frac{1}{2}$ way between the current point and the next point to predict the next position and velocity.
Every method has disadvantages as well as advantages. An alternative method that is a common "go-to" numerical approximation method is the RK4 method from the family of Runge–Kutta methods.
Make sure that as you work on these programming tasks, you are using your best pair programming technique (both driver and navigator) and switching drivers frequently.
Your code must have docstrings and comments for you to receive full credit.
Now it's time to add the more accurate Euler-Richardson method to our FreeFall
class, and develop some methods to compare approximation methods.
D. Add a free_fall_euler_richardson
method to your FreeFall
class that implements the Euler-Richardson method of approximation for our free fall problem. It should:
free_fall_approx
plot_results
method on the same axes as your Euler's method Don't forget to modify your plot_results
method.
In [ ]:
Test your Euler-Richardson method and briefly discuss the results
In [ ]:
E. Modify your calculate_error
, max_error
, and plot_error
methods to include your Euler-Richardson results:
max_error
should print the maximum error for each method and the time step infoplot_error
should have both sets of errors on the same axes so they can be compared
In [ ]:
Test your updated max_error
and plot_error
methods and briefly discuss the results
In [ ]:
F. Clean up your code! This is object oriented code after all, so you should:
__init__
method (constructor) or make a wrapper function to run the simulation, plot results, print and plot the error.
In [ ]:
Test your cleaned up code and briefly discuss the results
In [ ]:
test your code with each row of values in the table below.
Your code should do the following:
y0 | v0 | tmax | dt | Euler max error | E-R max error |
---|---|---|---|---|---|
100 | 1 | 30 | 1 | ||
100 | 1 | 30 | 0.5 | ||
100 | 1 | 30 | 0.2 | ||
100 | 1 | 30 | 0.1 | ||
1000 | 1 | 30 | 0.1 | ||
1000 | 1 | 30 | 1 | ||
10000 | 1 | 30 | 1 |
Also, add two columns to the table above - one column that has the maximum error for Euler's method and a second column that lists the maximum error for the Euler-Richardson method.
In [ ]:
## simulation runs and plots here
In [ ]:
7. Discuss your overall conclusions about the behavior of the Euler-Richardson Method (especially with respect to how its error behaves).
8. Compare the errors of the two methods. Which is the better approximation of our free fall scenario? Provide at least 2 reasons for your selection.
How much time did it require for your team to complete each part?
Analysis 0:
Team Programming 1:
Analysis 1:
Team Programming 2:
Analysis 2: