I’m embarrassed to admit how often I sit around wondering what a laser’s beam would look like if the room I was in was all reflective. Tonight I had some fun with Mathematica to see if I could see some of those cool patterns. I, of course, got off on a tangent, but it was still fun.
So how do we model bounces off a wall? There’s lots of ways to do it but I really didn’t want to have to constantly figure out the angle in (and hence the angle out) and also have to figure out what wall was going to be hit next. Instead I used the Lagrangian formalism to let Mathematica figure all that stuff out for me.
Before letting you know how I did it, here’s a cool animated gif to whet your appetite:
Note how angle out always equals angle in and the next wall is always accurately figured out.
How I did it
The Lagrangian formalism works in the following way: If you know an expression for the kinetic energy and potential energy of the particle, you take a couple of derivatives and, voila, you have the equation of motion. The rest is just putting the equation(s) of motion into a differential equation solver and make pretty pictures. Here’s the ugly math:
The first equation gives us the Lagrangian as the difference between the kinetic and potential energy (T and U respectively). The second equation (really it’s 2 because you need another one for y) is the equation of motion. Really this whole problem boiled down to a determination of the potential energy of the walls. All the rest – angles, who gets hit, etc – was done by the differential equation solver. Pretty slick, huh?
Potential of the walls
What I wanted was for the potential energy to increase linearly the more you penetrated into the wall. The problem was that I needed to figure out the distance normally into the (possibly slanted) walls. I decided to solve that problem with a cross product. Essentially I crossed a normalized vector along the wall with a vector from one point on the wall to the point in question. That gave me the distance into the wall. By using a cross product instead of a dot product I also got a sign difference between being in the wall and being out. I used a piecewise function to make sure that if you weren’t in the wall, there was no potential.
Here’s the trajectory of a ball approaching a horizontal wall (along with a zoom in of the interaction):
Ok, now that I’ve got one wall figured out, I just needed a function that could take as many walls as I liked. That was just a matter of running several vertices through a wall maker function.
Ok, now for some fun! I can make the walls static or time dependent and the Lagrangian just works!
First a simple static triangle:
And here’s a shot of a triangle who’s center vertex moved in a linear fashion straight down. The pink is the initial triangle and the yellow is the final triangle:
One last pic that I thought was cool. I wondered what would happen if I let Mathematica randomly pick vertices to turn into a random polygon. Here’s what happened:
Weird, huh? What happened was the initial condition was outside of the polygon (the small pink triangle). That meant that the ball rolled down into the triangle but had too much energy to be contained. What happened next was, I guess, a dynamical bound state.
I had a lot of fun with this, and I love how flexible it is. I think the rotating octagon is my favorite, but mostly what I really like is how flexible Mathematica is. I made use of the following functions built into Mathematica to make all of these (all in an hour and a half, by the way):
- Polytopes (package)
- Vertices (gives the coordinates of the vertices)
- RotationMatrix (to rotate the octagon)
- NDSolve (differential equation solver)
- ParametricPlot (makes the pretty pics)
- RandomReal (gave me the random polygon)