This Exercise Set has been submitted for peer review, but it has not yet been accepted for publication in the PICUP collection.

+
Bellicose Birds with Energy

Developed by Chris Orban

As a continuation of the [Bellicose Birds game](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=247&A=BellicoseBirds), this exercise considers how well the program does in conserving the total energy of the projectile (the bird!). This example will use a programming language called [p5.js](http://p5js.org) that is very similar to C and C++ programming. (Note: If you are familiar with C or C++ the main difference you will see is that there is no main() function and instead the draw() function serves this role.) **Importantly, this exercise can be completed using any computer or chromebook without downloading any software!** This exercise is designed for an algebra-based physics class at the college or high school level. It may also be useful for calculus-based physics for non-majors (e.g. engineering & science majors). This exercise is part of a series of exercises created by Prof. Chris Orban. Students should complete [Bellicose Birds](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=247&A=BellicoseBirds) before working on this exercise. There are pre-and-post assessment questions associated with this exercise (not available here) that are being used in an educational research study. If interested to collaborate on that study please e-mail Prof. Chris Orban (orban@physics.osu.edu). The first paper from this study [is available at this link](https://arxiv.org/abs/1701.01867), the second paper which discusses the electromagnetism exercises [is available at this link](https://arxiv.org/abs/1707.00185)
Subject Area Mechanics High School and First Year Javascript 1. Students will look at the total energy of a launched projectile versus time as it travels and notice that it is not being perfectly conserved. This is an important illustration of the wider lesson that when a computer program is set up to simulate a physics problem, the answer is not always perfectly correct. 2. Students will examine the program and plot various quantities versus time to determine which part of the code is causing the energy conservation problem 3. Students will modify the program to ensure that energy is conserved perfectly. This will involve applying the trapezoidal (a.k.a. "midpoint") method. 60 min
If you have completed the Bellicose Birds activity, you may have wondered how does energy conservation fit into all of this? Of all of the exercises submitted by Prof. Chris Orban ([Planetoids game](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=245&A=Planetoids), [Lunar Descent](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=246&A=LunarDescent), [Planetoids with Momentum](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=248&A=PlanetoidsMomentum), [Planetoids with Torque](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=249&A=PlanetoidsTorque), [Planetoids with a spring](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=251&A=PlanetoidsSpring)...) this is the only one that talks about energy conservation. This is also the only exercise that explains that you've been lied to! It turns out that there is a subtle problem with all these activities that gives the wrong behavior (until we correct it!). The problem is subtle enough that you can't really notice it until you look closely at the motion quantitatively, but it is definitely there as this exercise will show. We will show this by using a graphing system with the bellicose birds code so that you can see how quantities change with time. For the students out there who do this programming activity, the inaccuracy of the other Planetoids-based programming labs (all of them!) will be our little secret (shh!).

### Energy Conservation

It is good to think about energy conservation as a math problem where you are trying to figure out some combination of $v(t)$, $x(t)$ and $y(t)$ that will give you a constant. In many situations this constant (which we will call the total energy) is very useful to know, as it can tell you things like the max height of a projectile or the final speed of some falling object with much less trouble (i.e. algebra) than calculating the detailed trajectory of some object. The bellicose birds lab is a great example of a system where energy is conserved because objects (even birds!) in free fall conserve energy. For an object in free fall, if you combine $v(t)$, $x(t)$ and $y(t)$ in the following way you will always get a constant: $$\frac{1}{2} m v^2(t) + m g y(t) = {\rm constant} \nonumber$$ where $g = +9.8 \, {\rm m/s^2}$, $v^2(t) = v_x^2(t) + v_y^2(t)$ and because objects in free fall are accelerating in the $y$ direction, but not the $x$ direction, we know that $x$ velocity is constant ($v_{x}(t) = v_{xf} = v_{xi}$). Ultimately we get this: $$\frac{1}{2} m (v_{xi}^2 + v_y^2(t)) + m g y(t) = {\rm constant} \nonumber$$ and we refer to this constant as the total energy.

### Step 1. Pen and paper activity!

Prove that $\frac{1}{2} m (v_{xi}^2 + v_y^2(t)) + m g y(t)$ does add up to a constant. Do this by using the kinematic equations: $$y(t) = y_i + v_{yi} t - \frac{1}{2} g t^2 \nonumber$$ $$v_y(t) = v_{yi} - g t \nonumber$$ Remember that $g$, $m$, $v_{xi}$, and $v_{yi}$ are all constants whereas the time (= $t$) is definitely not a constant. So if you can get $t$ to cancel out of the expression completely then you're done! Hint: after you cancel out the $t$ the expression should reduce to $\frac{1}{2} m (v_{xi}^2 + v_{yi}^2) + m g y_i$

### Step 2. Open up the code!

[Click here to open the Bellicose Birds code](http://alpha.editor.p5js.org/ChrisOrban/sketches/BJfZoz9WW) This version has a graph displaying the kenetic, potential, and total energy of the bird. Aim the bird at about a roughly 45 degree angle and launch it to see what happens!

### Step 3. Think about KE and PE

As you have probably heard before, we identify $\frac{1}{2} m v^2(t)$ as the kinetic energy (KE) and $m g y(t)$ as the potential energy (PE). The plot in the upper right shows how these quantities ($KE(t)$ in orange and $PE(t)$ in aqua) change as the bird projectile travels through the air. It also shows the total energy $TE = KE(t) + PE(t)$ in black. Why doesn't the potential energy (PE) equal the total energy (TE) when the bird is at its peak height?

### Step 4. Think about total energy (TE)

If you look closely at the black line (TE) in the plot shown, you'll notice that it is not a perfectly flat line as you would expect from a system that conserves energy. There is a small but noticeable slant to the line so that the total energy becomes a little tiny bit smaller as time goes on! To be absolutely sure that the system is not conserving energy, comment out (or delete) this section of the code:
  if (y < 0) {
drawText("Game Over!",width/2,height/2);
noLoop();
}

when you do this, the game does not end when the bird hits the ground ($y < 0$). Instead the bird keeps on falling like it's been tossed into an infinitely deep hole. Now look at the black line showing the total energy. As the bird falls, the total energy becomes smaller and smaller until it is super obvious that energy is not being conserved!!!

### Step 5. Is there something wrong with the velocity update?

Clearly something has gone wrong! It could be a lot of things but it turns out there is something that is subtly wrong with the code below:
  // Update velocities
vx += deltaVx;
vy += deltaVy;

// Update location
x += vx*dt;
y += vy*dt;

Let's see if the problem is with the code that updates the velocity. If everything is working correctly, we should get this behavior: $$v_y (t) = v_{yi} - gt$$ So if we plot $v_y(t)$ vs $t$ it should look like a diagonal line. Let's see if this happens. [Click here to open another version of the code](http://alpha.editor.p5js.org/ChrisOrban/sketches/BJt8jMqbb) This version has a different graph to display the velocity in the y direction. Modify your code so that graph1 plots vy and comment out graph2 and graph3. Does the plot of vy vs time look like a diagonal line? The question is whether there is any curvature to the plot. It shouldn't! If it does, then perhaps this is why the total energy gets smaller over time. Make a screenshot of your program and turn in this image in order to show what the plot of vy vs time looks like! Say whether the plot has curvature or not!

### Step 6. Is there something wrong with the location update?

Perhaps the total energy conservation problem has to do with the "Update location" part of the code:
  // Update velocities
vx += deltaVx;
vy += deltaVy;

// Update location
x += vx*dt;
y += vy*dt;

In this case, we need to check somehow that the code produces this behavior: $$y(t) = y_i + v_{yi} t - \frac{1}{2} g t^2$$ We learned in the [Bellicose Birds](http://www.compadre.org/PICUP/exercises/exercise.cfm?I=247&A=BellicoseBirds) exercise that the bird will roughly follow this trajectory. But maybe the bird wasn't following this equation exactly? To address this possibility, we need to understand where this equation came from in the first place. Imagine that you are driving in a car and you pass a sign that says the speed limit has increased to 70 mph, so you apply a constant acceleration for a few seconds to go from your initial speed, say $v_{xi} =$50 mph, to your final speed of $v_{xf} =$70 mph. We know that the folowing is true: $v_{xf} = v_{xi} + a_x t$, but how far would you have driven during this interval? A. $\Delta x = v_{xi} t$ B. $\Delta x = v_{xf} t$ C. $\Delta x = \frac{v_{xi} + v_{xf}}{2} t$ Explain why option A is too small of a distance and why option B is too long of a distance! The correct answer is C. The right thing to do is to multiply the average velocity $v_{ave,x}=(v_{xi} + v_{xf})/2$ by the time interval to get the distance traveled. What is true for cars accelerating at a constant rate in the $x$ direction is true for objects falling at a constant rate in the $y$ direction. When an object falls you can find the change in height by averaging the initial velocity velocity and the final velocity and multiplying by the time. Show that $v_{yf} = v_{yi} - gt$ and $y_f = y_i + v_{ave,y} t$ gives us the well-known equation. $$y_f = y_i + v_{yi} t - \frac{1}{2} g t^2$$

### Step 7. Fix the problem!

When you write a computer program, instead of having acceleration over time interval $t$, you are essentially breaking up the time interval (e.g. of accelerating from one speed to another speed on the highway) into many steps of duration dt. The key thing here is that even though dt is much smaller than the total time duration $t$, it is still true that the change in position over the interval dt is the average velocity times the time interval (dt). Look at the code below and explain why the "Update Location" stage does not actually use the average velocity! Does the "Update Location" code resemble option A, B or C from the discussion earlier?
  // Update velocites
vx += deltaVx;
vy += deltaVy;

// Update location
x += vx*dt;
y += vy*dt;

How would you modify the code above to make sure that x and y change according to the average velocity over the interval dt? If you do this correctly, the total energy (TE) curve should be a horizontal line without any slant like we originally expected. [Here is how the code should behave if you fix the problem!](http://www.physics.ohio-state.edu/~orban/physics_coding/energy_files/v2/index.html) Turn in a screenshot that shows that TE is a horizontal line in your program. Hint: Go line by line through the code. Each step involves an initial and final velocity. Is vy at its "final" value or its "initial" value when it is multiplied by dt to give the change in y? How can we make sure that y changes according to the average of the initial and final values of vy?

### How to get full credit for this programming lab

1. Show that the time (t) cancels out of the expression in Step 1 This will take some algebra. You probably want to write this on a sheet of paper and scan it in what you turn in. If you can just cancel out the time variable, then it is clear that the expression adds up to a constant. 2. Explain why the PE doesn't equal TE when the bird is at its peak height (Step 3) Explain this with words in what you turn in. No math necessary. 3. Include a plot of vy vs time and comment on it (Step 5) Include this screenshot in what you turn in. You can do a print screen or if you want you can add saveFrame(); to the end of your draw function and it will write an image file to disk for every time step that your program goes through. Either will do. Make sure to comment whether vy versus time looks like a straight line to you or whether it looks curvy at all. 4. Explain why options A and B are wrong (Step 6) Explain with words why option A is too short a distance and why option B is too far of a distance for the car to travel during the time interval in question. Looking for one or two sentences here. 5. Figure out the modification needed to the "Update location" section (Step 7) Figure out how to make sure that y changes each timestep according to the average velocity during the timestep dt. [Here is how the code should behave if you fix the problem!](http://www.physics.ohio-state.edu/~orban/physics_coding/energy_files/v2/index.html) Talk to an instructor about this if after 20 minutes you don't feel like you're any closer to the solution. Once you fix the code, make sure to do a screen capture to show that the TE plot is a straight line as expected.