Lab - Numerical Analysis of Motion

Read all the stuff below (especially the linked material). Then come back and watch this screen cast. PLEASE do not watch this during lab because I hate to hear myself talk.



Extra Reading Material:


Goal In this lab, you will use either Excel or VPython to calculate the position and time of a ball that is thrown into the air. You will then use analytical methods to verify your answer. For this simple situation, numerical methods are not needed, but this lab will introduce you to the methods so that they can be used in later labs.

Numerical vs. Analytical Solutions
In this lab, we are trying to determine the position of the thrown ball at certain times. One way to do this would be to start with fundamental ideas (or the kinematics equations) and algebraically solve for y(t). If we did this (and we will), this is called an analytical solution. What if instead we used simpler ideas and calculated (by hand) the position of the ball every 0.01 seconds. Using this method, the math is easier but to find the position of the ball after 1 seconds you would have to do 100 steps of calculations. This method is called numerical solutions and it does not look inviting - UNLESS you get a computer to do all the tedious calculations. Analytical solutions are only possible for a small set of simplistic situations, but numerical solutions can be used in many more complex situations.

In this lab, I will show you how to complete the numerical analysis in both Microsoft Excel (would also work in other spreadsheets) and also in VPython (http://www.vpython.org)

Initial Conditions
For all calculations, we will use a ball that is thrown straight up with an initial speed of 6 m/s and that the ball has an acceleration of 9.8 m/s2 in the downward direction.

Numerical Recipe
For both Excel and VPython, we will use the following steps:

1) Break the motion into small pieces of time. How big each time step is variable. The best way to determine the appropriate step size is to start with something like 0.1 seconds and calculate the time it takes to reach the highest point. Then decrease the time interval and see if your answer changes. If your answer does not change significantly with step size, then you have a good value.

2) For each step, you are going to do the following:
Calculate the y position. The average velocity (in the y direction) is defined as:
screenshot_02
If we replace delta y with y2 - y1 and solve for y2, we get:
screenshot_03
To use this, we can calculate the current position (y2) using the position it was before (y1) and the velocity. BUT WAIT! How can that be correct, the velocity is changing! Here is the magic of numerical solutions. If your time step is small enough, the change in velocity is small enough to not matter.

Now we need to update the velocity. The acceleration (in the y direction) is defined as:
screenshot_04
similar to above, we can get:
screenshot_05
and then we can do the same thing with velocity as we did with position.

The last thing we need to do is update the time. t2 = t1 + delta t

Now on to explicit instructions for Excel and VPython.

Excel

1) Declare your constants: There will be several numbers that you will use. It is much better to have a cell that contains those constants that you reference. In this way, if you want to change the initial velocity, you only have to change one number. I set up the following values:
screenshot_01

Another trick: instead of calling the velocity "B1" (the value in cell B1) you can name that cell. Click on cell B1 and you should see something like this:
screenshot_10
You can just type over B1 and write something like v0. Now you can reference that cell by the name v0 instead of B1 (or $B$1). Do the same for y0, dt, and a (you can call them whatever you want)

2) Now set up your variables, these are the things that will change each time step. You should have a variable for time, y-position, and y-velocity
screenshot_11

3) Calculate the first time step. For the very first time, you can just type 0. For the first y, click on the cell and type "=y0" (without the quotes). For the first velocity type "=v0"
*note: when you type "=" in an excel cell, you are telling the program you are entering in an equation for it to calculate.

4) Calculate the next time step.
For time:
The next time will be the previous time plus the change in time, so enter:
"=A7+dt" This assumes that the cell directly above is A7 and that your time step is called "dt"

For y-position:
The next y-position will be the previous one plus v*dt, so enter
"=B7+C7*dt"
*note: I have used the velocity from the LAST time step. This will be ok if I use small time steps. (trust me)

For the y-velocity:
The next y-velocity will be the previous one plus a*dt, so enter:
"=C7+a*dt"

5) Calculate all the other times, positions and velocities.
You could repeat the above steps, but that would be tedious. Excel is good at tedious things. Highlight cells A8 through C8 and you should see a little square handle on the highlight box
screenshot_03
Click and drag that handle down as far as you like and it will repeat the calculations. Drag far enough so that the position returns to 0.

Now try to reduce the time step to 0.01 seconds and see if the time to get back to zero y is the same (or similar).

Now you can make graphs (if you like) of position-time, velocity vs. time.

Answer the following questions:

1) How high did the ball go?

2) How long was the ball in the air?

3) What was the acceleration of the ball as it was going up? What was the acceleration at the highest point? What was the acceleration on the way down?

4) What was the velocity when the ball got back to y = 0?

*note: Units have not been included because Excel is not smart enough for that. If we changed ALL the units from meters to feet, this would still be the answer.


Numerical Analysis with VPython

Python is a simple to use programming language (very common). VPython is Python with some visual modules loaded to easily create 3-D simulations. VPython has been compiled for Mac OS X, Windows, and Linux and it is free. More info and downloads for VPython can be found at http://www.vpython.org

The following instructions will be for VPython on the imac computers in PH 104. Other installations may be slightly different.

1) To Launch the VPython editor, you must first start the X11 environment. In the terminal window, type "/sw/bin/vpython" to start.

2) Click "file" - "new" to make sure you have a new editing window.

3) Load visual modules. Type the following first line in your program:
screenshot_15
This tells your python program to use the visual modules.

4) Type the next line:
screenshot_16
This calls the visual module "sphere" which makes a 3D sphere. You can now run this to see what happens. Before you run you will need to save. Click File-Save and you will get the following window:
screenshot_17
Where it says "file name" type "/users/physics/ball.py". You can change then name of ball.py to whatever you want.

5) Run the program by clicking Run - Run Module. Two things should happen, first is the python shell window should show up and second a 3D window should show up that looks like this:
screenshot_18
Congratulations, you just created your first program (that wasn't too bad, was it?)

6) Now on to the real program. The first thing we need to do is set up our variables (just like in Excel). Enter something like this:
screenshot_21
Instead of just using "sphere" we gave it a label called "ball". We also gave the sphere some properties. pos=(0,0,0) says that the position of the center of the ball will be the origin (in 3 dimensions). Radius is obviously the radius of the ball and color the color.

In the next line, "ball.v=vector(0,6,0)" we declare a property of the ball call ball.v (we will use this for the velocity of the ball). We set its initial value to the vector (0,6,0)

The same thing for the variable "a"

7) Calculate all the steps. In excel, each row was a time step. In python, each time step will be one iteration of a loop. We will use a while loop that basically says while a certain condition is true, do the following. Type this:
screenshot_23
I will explain what each line means.

"while ball.pos.y>=0:" This is the beginning of the while loop. The condition is that we want to run until the ball gets back to the ground (you could change this to other things). "ball.pos.y" is the y-component of the ball's position. I wrote ">=0" to say that it is at or above y=0. The rest of the code in the while loop MUST be indented.

"rate(30)". Computers are really fast. If you don't have this statement in there, it will run this program so fast you won't see anything (maybe that is ok for you). "rate(30)" tells the program to not do the loop more than 30 times a second. If you did rate(10) with dt=.1, it would run at correct world speed.

"ball.pos=ball.pos+ball.v*dt" - this is the updating the position equation.

"ball.v=ball.v+a*dt" - this is the updating the velocity equation

"t=t+dt" - updates the time.

Getting the information - PRINT
Well, now your program works, but how can you determine anything? One simple way is to use the "print" statement. Add the following line INSIDE your while loop:

print ball.pos.y

Now run your program. The 3D window should look the same, but in the output window, you should see a long list of positions (it prints in each iteration).
You could have put:

print "the y-positon of the ball is ", ball.pos.y

Try that.

You could print the time, the position and the velocity. This would be all the information you would need to answer the following questions:

1) How high did the ball go?

2) How long was the ball in the air?

3) What was the acceleration of the ball as it was going up? What was the acceleration at the highest point? What was the acceleration on the way down?

4) What was the velocity when the ball got back to y = 0?

*note: Units have not been included because VPython is not smart enough for that. If we changed ALL the units from meters to feet, this would still be the answer.

Analytical Solution

For the analytical solution, you can simply use the kinematic equation that relates position and velocity to time in the case for constant acceleration:

screenshot_24

Use these two equations and answer the same questions as before.

HINT:

For the case of finding how high it went, start with the second of these equations. You know at the highest point, the velocity (vy) was zero. This can be used (in the second equation) to solve for the time. Now use the time in the first equation to solve for y.

For the case of finding the speed when the ball gets back to y=0, do the reverse. Use the first equation to solve for t, then plug this into the second equation.

In the case of determining the acceleration on your numerical analysis, you can use the definition of acceleration (a = (v2-v1)/(t2-t1)). If you do not like your answers, try drawing a graph of vy vs. time. The slope of this line is the acceleration. If you want, you could use the velocity values you printed out to create a graph.