# Extra credit: Bellicose birds and energy!

If you have completed the Bellicose Birds activity, you may have wondered how does energy conservation fit into all of this? This extra credit exercise 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 the programming labs 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 those of you who do this lab, the inaccuracy of the 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}$$ 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}$$ 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$$ $$v_y(t) = v_{yi} - g t$$

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!

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.

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 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.

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?

Comment: It turns out that, as the code is currently written, the height of the projectile is given by $y(t) = y_i + v_{yi} t - (3/4) g t^2$. This is so similar to the correct equation that the incorrect factor on the $t^2$ was overlooked.

### 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. 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.