Tag Archives: simulation

Projectile Motion with Python, Desmos, and Monte Carlo Simulation

I've written about my backwards approach to to projectile motion previously here, here, and here.

I had students solving the warm-up problem to that first lesson, which goes like this:

A student is at one end of a basketball court. He wants to throw a basketball into the hoop at the opposite end.

What information do you need to model this situation using the Geogebra model? Write down [______] = on your paper for any values you need to know to solve it using the model, and Mr. Weinberg will give you any information he has.
Find a possible model in Geogebra that works for solving this problem.
At what minimum speed he could throw the ball in order to get the ball into the hoop?

The students did what they usually do with the Geogebra projectile motion model and solved it with some interesting methods. One student lowered the hoop to the floor. Another started with a 45 degree angle, and then increased the speed successively until the ball made it into the hoop. Good stuff.

A student's comment about making lots of guesses here got me thinking about finding solutions more algorithmically. I've been looking for new ways to play around with genetic algorithms and Monte Carlo methods since they are essentially guess and check procedures made productive by the power of the computer.

I wrote a Python program that does the following:

  • Get information about the initial characteristics of the projectile and the desired final location.
  • Make a large number of projectiles (guesses) with random values for angle and initial speed within a specified range.
  • Calculate the ending position of all of the projectiles. Sort them by how far they end up compared to the desired target.
  • Take the twenty projectiles with the least error, and use these values to define the initial values for a new, large number of projectiles.
  • Repeat until the error doesn't change much between runs.
  • Report the projectile at the end with the least error.
  • Report the entire procedure a number of times to see how consistent the 'best' answer is.

I've posted the code for this here at Github.

As a final step, I have this program outputting commands to graph the resulting projectile paths on Desmos. Pasting the result into the console while a Desmos calculator open, makes a nice graph for each of the generated projectiles and their intersecting at the desired target:
Screen Shot 2015-02-14 at 11.12.03 AM

This is also on a live Desmos page here.

This shows that there is a range of possible answers, which is something I told my physics class based on their own solutions to the problem. Having a way to show (rather than tell) is always the better option.

I also like that I can change the nature of the answers I get if I adjust the way answers are sorted. This line in the code chooses how the projectile guesses are sorted by minimizing error:

self.ordered = self.array.sort(key=lambda x: abs(x.error))

If I change this to instead sort by the sum of error and the initial speed of the projectile, I get answers that are much closer to each other, and to the minimum speed necessary to hit the target:

Screen Shot 2015-02-14 at 11.19.01 AM

Fun stuff all around.

Simulations, Models, and the 2012 US Election

After the elections last night, I found I was looking back at Nate Silver's blog at the New York Times, Five Thirty Eight.

Here was his predicted electoral college map:

Image

...and here was what ended up happening (from CNN.com):

Image

I've spent some time reading through Nate Silver's methodology throughout the election season. It's detailed enough to get a good idea of how far he and his team  have gone to construct a good model for simulating the election results. There is plenty of description of how he has used available information to construct the models used to predict election results, and last night was an incredible validation of his model. His popular vote percentage for Romney was predicted to be 48.4%, with the actual at 48.3 %. Considering all of the variables associated with human emotion, the complex factors involved in individuals making their decisions on how to vote, the fact that the Five Thirty Eight model worked so well is a testament to what a really good model can do with large amounts of data.

My fear is that the post-election analysis of such a tool over emphasizes the hand-waving and black box nature of what simulation can do. I see this as a real opportunity for us to pick up real world analyses like these, share them with students, and use it as an opportunity to get them involved in understanding what goes into a good model. How is it constructed? How does it accommodate new information? There is a lot of really smart thinking that went into this, but it isn't necessarily beyond our students to at a minimum understand aspects of it. At its best, this is a chance to model something that is truly complex and see how good such a model can be.

I see this as another piece of evidence that computational thinking is a necessary skill for students to learn today. Seeing how to create a computational model of something in the real world, or minimally seeing it as an comprehensible process, gives them the power to understand how to ask and answer their own questions about the world. This is really interesting mathematics, and is just about the least contrived real world problem out there. It screams out to us to use it to get our students excited about what is possible with the tools we give them.

On not getting in the way.

Today continued a run of some great class time working on electric circuits. Our whole class period (85 minutes) consisted of looking at the following six circuits:

Circuit 1

Describe what you would expect to happen when this circuit is connected.

A student blurted out this would be a short circuit, so things would get hot. Everyone immediately agreed (WHY DID YOU BLURT THAT OUT!). Still time to save it; I ask why it is a short circuit?

Answer: Because the path through the wire is shorter than traveling through the resistors.

I smirk.

Circuit 2

Groans from students...then a more refined answer about differences of resistance between the two branches of the wires.

Circuit 3

Tell me anything you can tell me about this circuit. If you see something to calculate, calculate it. Build the circuit on the PHET circuit constructor and show that your calculations confirm what happens in the simulator.

Insert student-centered-learning opportunities and fantastic conversations between students here. Students seeing a difference between their answers and what the simulation is telling them causes conflict that they help each other to resolve. Some students bring up the term 'parallel', which I've never said in class. Others don't understand what that is, so there is some fighting. One student describes qualitatively what should happen and then shows he is correct in the simulation, no calculations. Furthermore, this student usually is one that gets anxious when there are limited formulas to cling to, which is the norm in my class.

Circuit 4

Repeat the same procedure. Calculate what you can calculate. Build the circuit in the simulator and verify. Explain away differences, or see if there is something you are missing.

Continued progress in recognizing this is a combination of series and parallel resistors, but I don't make a big deal out of this. A couple students look up formulas and discover the idea of finding equivalent resistance (which I have never mentioned to them). This helps, but the simulator telling them what the correct answers are is key. They are compelled to get the simulator's answers through calculation - it's almost as if they feel the simulator is cheating by giving them the answers, so they must understand how to get it on their own. Eventually, they are convincing each other why they are right.

Circuit 5

Same as before, tell me anything you can tell me about this circuit. If you see something to calculate, calculate it. Build the circuit on the PHET circuit constructor as a last resort and to show that your reasoning has led to a correct analysis of the entire circuit.

This time the students hit a wall. Some continued finding equivalent resistance and the battery current, but weren't sure how to find the current through the 10 ohm and 30 ohm resistors. One reasoned it would split proportionally, and confirmed the answer using the simulator. Another measured the voltage across one of the 10 ohm resistors using the simulated voltmeter, measured the current, and then calculated the voltage difference across it. Repeating for the other resistor, they figured out the voltage difference across the parallel resistors, which then led to a current calculation. Again, I only had to tap students in the right direction - the rest was them helping each other.

Circuit 6

Try to analyze this circuit completely using what you have learned today. Once you are convinced you have accounted for all voltage differences and all current, build it in the simulator to confirm your answers. Find a way to calculate the power used by the 20 ohm and the 10 ohm resistors separately - look it up if you want.

They did a fantastic job of figuring this out - some very quickly and quantitatively. One student that oftenstruggles with concepts figured out how the current between the different branches would compare, and reasoned which ones would have the greatest voltage difference across them.

Then I started lecturing about the equivalence of electrical power and mechanical power, and the magic disappeared. They stopped talking and returned to compliance mode. I saw that happening, so I stopped. Anything I could do at this point would only ruin what was quite possibly a perfect learning experience for them.

When I taught AP Physics, we spent a day on series circuits and deriving resistance formulas, a day on parallel circuits and deriving equations for parallel resistors, and then another day on analyzing circuits that have both. Before today, I had never used the term 'parallel' with my students. This time they brought it up. They now have the ability to analyze the same level of circuits as my former AP students, but this group was able to figure much of it out on their own, with no mention of memorization of formulas and no extended periods spent listening to me blabber on about how 'going through the theory helps you understand'.

There is lots I could say about this, but I think the points made are pretty clear. Let's just say that I'm really proud of my students work today.

The TacoCopter? - a gimmick for integration review

I received an email sending me to this site yesterday about the TacoCopter, which of course was spot on given my interest in all things robotic. I also had PID control on the brain thanks to my course on driving a robot car from Udacity. Bits of python code were in my head already, and I had a strong need to put it all together. Given that it was also Sunday (a workday for most teachers) I had to plan for classes tomorrow, specifically Calculus and Physics.

All of this was in the context of the beautiful afternoon I spent on the balcony of the apartment looking out at the warmest, bluest Hangzhou skies of the year so far. It put me in the mood to do something a bit different for tomorrow's Calculus class. The AP students will be reviewing related rates and implicit differentiation, but the regular students...they get to have a bit more fun.

This is the activity we will be looking at tomorrow in class: CW - TacoCopter Project

The full wiki page that students will be following is located here: http://wiki.hischina.org/groups/gealgerobophysiculus/wiki/42712/Calculus_Unit_8__The_TacoCopter.html

Some python code for simulating the TacoCopter rising to altitude, which can be found here at github.

Then Geogebra for plotting the data, which shows the lovely simulated accelerometer data with noise:

I don't really know how it will go. At least students will have an excuse to grin as they review.

From projectile motion to orbits using Geogebra

I was inspired last night while watching the launch of the Mars Science Laboratory that instead of doing banked curve problems (which are cool, but take a considerable investment of algebra to get into) we would move on to investigating gravity.

The thing that took me a long time to wrap my head around when I first studied physics in high school was how a projectile really could end up orbiting the Earth. The famous Newton drawing of the cannon with successively higher launch velocities made sense. I just couldn't picture what the transition looked like. Parabolas and circles (and ellipses for that matter) are fundamentally different shapes, and at the time the fact that they were all conic sections was too abstract of a concept for me. Eventually I just accepted that if you shoot a projectile fast enough tangentially to the surface of the Earth, it would never land, but I wanted to see it.

Fast forward to this afternoon and my old friend Geogebra. There had to be a way to give my physics students a chance to play with this and perhaps discover the concept of orbits without my telling them about it first.

You can download the sketch I put together here.

The images below are the sorts of things I am hoping my students will figure out tomorrow. From projectile motion:

...to the idea that it is still projectile motion when viewed along with the curvature of the planet:

Continuing to adjust the values yields interesting results that suggest the possibility of how an object might orbit the Earth.


If you open the file, you can look at the spreadsheet view to see how this was put together. This uses Newton's Law of Gravitation and Euler's method to calculate the trajectory.You can also change values of the variable deltat to predict movement of the projectile over longer time intervals. There is no meaning to the values of m, v0, or height - thankfully the laws of nature don't care about units.

As is always the case, feel free to use and adjust this, as well as make it better. My only request - let me know what you do with it!