1 of 40

Physics and Numerical Calculations

Rhett Allain

2 of 40

Introduction

3 of 40

Let’s get started

  • Maybe you’ve never made a program before - that’s ok.
  • Maybe you already programmed Hal 9000 - that’s ok.
  • I will assume no background experience

4 of 40

A physics problem

  • A cart is on a track at x = 0 m, at t = 0 s.
  • The cart is moving with an x-velocity of 0.45 m/s.
  • Where is the cart at t = 1.5 seconds?

5 of 40

Use the definition of average velocity

  • at t = 1.5, the cart is at 0.675 m

6 of 40

Breaking into pieces

  • What if I do the same problem with a time interval of 0.25 seconds?
  • Where would the car be after that time?
  • What about another 0.25 seconds? Where would it be?

7 of 40

Fill in the table for v = 0.45 m/s

Time (s)

dt (s)

x (m)

0

0.25

0

8 of 40

Fill in the table for v = 0.45 m/s

Time (s)

dt (s)

x (m)

0

0.25

0

0.25

0.25

0.1125

9 of 40

Fill in the table for v = 0.45 m/s

Time (s)

dt (s)

x (m)

0

0.25

0

0.25

0.25

0.1125

0.5

0.25

0.225

0.75

0.25

0.3375

1

0.25

0.45

1.25

0.25

0.5625

1.5

0.25

0.675

Same final position

10 of 40

You are a computer

  • The original computer was a person that did all these calculations.
  • Now we use an electronic computer - but we just call it a computer.
  • This is just like in the movie Hidden Figures - which you should absolutely see.

11 of 40

Making a numerical model

  • How many steps of 0.25 seconds would you need to move 1.5 seconds?

Position Update Formula

  • If you know the current position, you can find the next position

12 of 40

First Program

  • Go to http://bit.ly/trinket-physics
  • Click on the left “Using Small Pieces”

13 of 40

*No Units!

In one-window, there will be a pencil for edit

This is the same as line 7

14 of 40

Notes

  • The “=“ is not an algebraic equal sign
  • x = x +v*dt means take “x” and add it to v*dt and then make that the new x
  • You have to explicitly include * for multiplication.

15 of 40

Your Turn

  • Run the program
  • Where will the cart be at 2.2 seconds?
  • What if the cart has a velocity of 0.62 m/s, where will it be at 2.2 seconds?
  • What if the cart starts at -0.5 m, has a velocity of 0.62 m/s - where will it be at 2.2 seconds?

http://bit.ly/trinket-physics

(Click “using small pieces”)

16 of 40

  • If you modified the code in any way, you are now a programmer.
  • That wasn’t so difficult.

17 of 40

Accelerating cart

  • A cart starts at x = 0 m, and t = 0 s, with a velocity of 0.45 m/s.
  • The cart has an acceleration of 0.2 m/s2.
  • Where is the cart after 1.5 seconds?

18 of 40

Solution

  • at t = 1.5 s, the cart will be at 0.9 meters

Some derivations skipped

19 of 40

Break it into small steps

  • What if I again break it into steps of 0.25 seconds?
  • What if I still use the position update formula?
  • Use the velocity update formula.
  • What if I use final velocity instead of avg?

20 of 40

Fill in the table for v = 0.45 m/s and a = 0.2 m/s^2

Time (s)

dt (s)

v (m/s)

x (m)

0

0.25

0.45

0

21 of 40

Fill in the table for v = 0.45 m/s and a = 0.2 m/s^2

Time (s)

dt (s)

v (m/s)

x (m)

0

0.25

0.45

0

0.25

0.25

0.5

0.125

Technically

Wrong

Not Average

Velocity - but close

22 of 40

Fill in the table for v = 0.45 m/s and a = 0.2 m/s^2

Time (s)

dt (s)

v (m/s)

x (m)

0

0.25

0.45

0

0.25

0.25

0.5

0.125

0.5

0.25

0.55

0.2625

0.75

0.25

0.6

0.4125

1

0.25

0.65

0.575

1.25

0.25

0.7

0.75

1.5

0.25

0.75

0.9375

Close to 0.9

23 of 40

Second program

24 of 40

Your turn

  • What happens to the final answer when you change the step size? (try dt = 0.01 and dt = 0.1)
  • What if the acceleration is -0.01 m/s^2? Where will the object end up?
  • Challenge: If the acceleration is -0.02 m/s^2, how long would it take to stop and where would it be? (hint: try printing the velocity at the end of the loop)

25 of 40

How to get more data

Indented print

Prints stuff in each loop run

Comments

26 of 40

Graphs

Creates axes

Creates curve

Adds data to

graph

27 of 40

28 of 40

Your Turn

  • Make a plot of velocity vs. time (hint: change line 19) (double hint: you might want to also change the ytitle in line 3)
  • Change the time step to 0.01 seconds and plot position vs. time.

29 of 40

A real physics problem

  • Here is a problem. Car A starts at x = 0.5 m with a velocity of 0.45 m/s. Car B starts at x = 0 with a velocity of 0 m/s but has an acceleration of 0.2 m/s2. When does Car B catch up to Car A? Where do they meet?

30 of 40

Starter Program

These are incomplete

31 of 40

Notes

  • This program has two plots on the same graph (that is defined by fA and fB in lines 4 and 5). Also note that python IS case sensitive. This means that fA is not the same as fa.
  • Lines 7-13 are just creating the variables with their initial values. Since there are two objects, you need two different variable names. That’s why there is an xA and an xB.
  • Lines 17, 19, 20 are incomplete. I gave you a hint as to what should go there – but you need to put something there so this code will run.
  • Lines 24,25 are the parts that add a data point (for each variable) to the graph.
  • Line 15 is important. You will need to change this in some way to find your answer. There are multiple things you could change this to that will provide an answer.

32 of 40

Throw a Ball

  • A ball with a mass of 0.2 kg is thrown straight up with an initial speed of 12 m/s. How high does it go? How long does it take to get back down to the ground?

h

33 of 40

Physics Stuff

  • Once the ball is thrown, there is only the gravitational force on it.
  • The gravitational force pulls down and depends on mass and “g” - the gravitational field.
  • The acceleration can be found from:

*note: in 1-dimension

34 of 40

Starter Program

Calculate the force

in the loop

Calculate the

acceleration

Velocity update

Position update

You fill these in

35 of 40

Results

  • This is what you should get:
  • Is this the highest point? No - because the final velocity would be zero.
  • See if you can change the time of the loop to get the ball to the highest point. How high did it go?
  • See if you can change the time of the loop to get the ball back to the ground. How long did it take?

36 of 40

Highest Point

  • Changing the time to get the right answer might not seem super fun. Here is another option.
  • Try changing the while loop to this:
  • Now the code will run as long as v is positive. It goes from positive to negative velocity at the highest point.

37 of 40

Getting Back to the ground

  • There is also a better way to find the time it takes for the ball to get back to the ground.
  • Try changing the while loop to this:
  • Notice that you have to put “greater than or equal to” since it starts at y = 0 m.

38 of 40

Adding a Graph

39 of 40

Output

40 of 40

Your Turn

  • What if the ball starts at y = 2.5 m? How long would it take to get back to y = 0 m?
  • You might have heard that a ball thrown up comes back down with the same speed when it gets to the ground. Is this true?
  • How high would you have to jump to have a hang time of 2 seconds? (change the starting velocity)
  • Drop a ball from a height of 5 meters. How high would you have to drop a ball to have twice the fall time?