- •Preface
- •Contents
- •1 Introduction
- •1.1 Physics
- •1.2 Mechanics
- •1.3 Integrating Numerical Methods
- •1.4 Problems and Exercises
- •1.5 How to Learn Physics
- •1.5.1 Advice for How to Succeed
- •1.6 How to Use This Book
- •2 Getting Started with Programming
- •2.1 A Python Calculator
- •2.2 Scripts and Functions
- •2.3 Plotting Data-Sets
- •2.4 Plotting a Function
- •2.5 Random Numbers
- •2.6 Conditions
- •2.7 Reading Real Data
- •2.7.1 Example: Plot of Function and Derivative
- •3 Units and Measurement
- •3.1 Standardized Units
- •3.2 Changing Units
- •3.4 Numerical Representation
- •4 Motion in One Dimension
- •4.1 Description of Motion
- •4.1.1 Example: Motion of a Falling Tennis Ball
- •4.2 Calculation of Motion
- •4.2.1 Example: Modeling the Motion of a Falling Tennis Ball
- •5 Forces in One Dimension
- •5.1 What Is a Force?
- •5.2 Identifying Forces
- •5.3.1 Example: Acceleration and Forces on a Lunar Lander
- •5.4 Force Models
- •5.5 Force Model: Gravitational Force
- •5.6 Force Model: Viscous Force
- •5.6.1 Example: Falling Raindrops
- •5.7 Force Model: Spring Force
- •5.7.1 Example: Motion of a Hanging Block
- •5.9.1 Example: Weight in an Elevator
- •6 Motion in Two and Three Dimensions
- •6.1 Vectors
- •6.2 Description of Motion
- •6.2.1 Example: Mars Express
- •6.3 Calculation of Motion
- •6.3.1 Example: Feather in the Wind
- •6.4 Frames of Reference
- •6.4.1 Example: Motion of a Boat on a Flowing River
- •7 Forces in Two and Three Dimensions
- •7.1 Identifying Forces
- •7.3.1 Example: Motion of a Ball with Gravity
- •7.4.1 Example: Path Through a Tornado
- •7.5.1 Example: Motion of a Bouncing Ball with Air Resistance
- •7.6.1 Example: Comet Trajectory
- •8 Constrained Motion
- •8.1 Linear Motion
- •8.2 Curved Motion
- •8.2.1 Example: Acceleration of a Matchbox Car
- •8.2.2 Example: Acceleration of a Rotating Rod
- •8.2.3 Example: Normal Acceleration in Circular Motion
- •9 Forces and Constrained Motion
- •9.1 Linear Constraints
- •9.1.1 Example: A Bead in the Wind
- •9.2.1 Example: Static Friction Forces
- •9.2.2 Example: Dynamic Friction of a Block Sliding up a Hill
- •9.2.3 Example: Oscillations During an Earthquake
- •9.3 Circular Motion
- •9.3.1 Example: A Car Driving Through a Curve
- •9.3.2 Example: Pendulum with Air Resistance
- •10 Work
- •10.1 Integration Methods
- •10.2 Work-Energy Theorem
- •10.3 Work Done by One-Dimensional Force Models
- •10.3.1 Example: Jumping from the Roof
- •10.3.2 Example: Stopping in a Cushion
- •10.4.1 Example: Work of Gravity
- •10.4.2 Example: Roller-Coaster Motion
- •10.4.3 Example: Work on a Block Sliding Down a Plane
- •10.5 Power
- •10.5.1 Example: Power Exerted When Climbing the Stairs
- •10.5.2 Example: Power of Small Bacterium
- •11 Energy
- •11.1 Motivating Examples
- •11.2 Potential Energy in One Dimension
- •11.2.1 Example: Falling Faster
- •11.2.2 Example: Roller-Coaster Motion
- •11.2.3 Example: Pendulum
- •11.2.4 Example: Spring Cannon
- •11.3 Energy Diagrams
- •11.3.1 Example: Energy Diagram for the Vertical Bow-Shot
- •11.3.2 Example: Atomic Motion Along a Surface
- •11.4 The Energy Principle
- •11.4.1 Example: Lift and Release
- •11.4.2 Example: Sliding Block
- •11.5 Potential Energy in Three Dimensions
- •11.5.1 Example: Constant Gravity in Three Dimensions
- •11.5.2 Example: Gravity in Three Dimensions
- •11.5.3 Example: Non-conservative Force Field
- •11.6 Energy Conservation as a Test of Numerical Solutions
- •12 Momentum, Impulse, and Collisions
- •12.2 Translational Momentum
- •12.3 Impulse and Change in Momentum
- •12.3.1 Example: Ball Colliding with Wall
- •12.3.2 Example: Hitting a Tennis Ball
- •12.4 Isolated Systems and Conservation of Momentum
- •12.5 Collisions
- •12.5.1 Example: Ballistic Pendulum
- •12.5.2 Example: Super-Ball
- •12.6 Modeling and Visualization of Collisions
- •12.7 Rocket Equation
- •12.7.1 Example: Adding Mass to a Railway Car
- •12.7.2 Example: Rocket with Diminishing Mass
- •13 Multiparticle Systems
- •13.1 Motion of a Multiparticle System
- •13.2 The Center of Mass
- •13.2.1 Example: Points on a Line
- •13.2.2 Example: Center of Mass of Object with Hole
- •13.2.3 Example: Center of Mass by Integration
- •13.2.4 Example: Center of Mass from Image Analysis
- •13.3.1 Example: Ballistic Motion with an Explosion
- •13.4 Motion in the Center of Mass System
- •13.5 Energy Partitioning
- •13.5.1 Example: Bouncing Dumbbell
- •13.6 Energy Principle for Multi-particle Systems
- •14 Rotational Motion
- •14.2 Angular Velocity
- •14.3 Angular Acceleration
- •14.3.1 Example: Oscillating Antenna
- •14.4 Comparing Linear and Rotational Motion
- •14.5 Solving for the Rotational Motion
- •14.5.1 Example: Revolutions of an Accelerating Disc
- •14.5.2 Example: Angular Velocities of Two Objects in Contact
- •14.6 Rotational Motion in Three Dimensions
- •14.6.1 Example: Velocity and Acceleration of a Conical Pendulum
- •15 Rotation of Rigid Bodies
- •15.1 Rigid Bodies
- •15.2 Kinetic Energy of a Rotating Rigid Body
- •15.3 Calculating the Moment of Inertia
- •15.3.1 Example: Moment of Inertia of Two-Particle System
- •15.3.2 Example: Moment of Inertia of a Plate
- •15.4 Conservation of Energy for Rigid Bodies
- •15.4.1 Example: Rotating Rod
- •15.5 Relating Rotational and Translational Motion
- •15.5.1 Example: Weight and Spinning Wheel
- •15.5.2 Example: Rolling Down a Hill
- •16 Dynamics of Rigid Bodies
- •16.2.1 Example: Torque and Vector Decomposition
- •16.2.2 Example: Pulling at a Wheel
- •16.2.3 Example: Blowing at a Pendulum
- •16.3 Rotational Motion Around a Moving Center of Mass
- •16.3.1 Example: Kicking a Ball
- •16.3.2 Example: Rolling down an Inclined Plane
- •16.3.3 Example: Bouncing Rod
- •16.4 Collisions and Conservation Laws
- •16.4.1 Example: Block on a Frictionless Table
- •16.4.2 Example: Changing Your Angular Velocity
- •16.4.3 Example: Conservation of Rotational Momentum
- •16.4.4 Example: Ballistic Pendulum
- •16.4.5 Example: Rotating Rod
- •16.5 General Rotational Motion
- •Index
22 |
2 Getting Started with Programming |
2.7 Reading Real Data
When you work with physics you need to handle real data: NASA publishes data for the motion of most stellar objects; your mobile phone has an accelerometer; and a GPS that measures thousands of data-points in a few seconds. You do not want to type in these numbers by hand. Therefore you need to be able to read files containing numbers. For example, the motion of a sprinter running 100 m is given in the file 100m.d.3 The file looks like this if you open it in a text editor (such as emacs):
0.0000000e+000 -2.1155775e-001
1.0000000e-002 -1.7485406e-001
2.0000000e-002 -1.3798607e-001
3.0000000e-002 -1.0095306e-001
4.0000000e-002 -6.3754256e-002
5.0000000e-002 -2.6388915e-002
...
A total of 972 lines of data. The first column gives the time, measured in seconds, and the second column gives the position of the runner, measured in meters. Fortunately, it is very simple to read such as file into Python. It is done by a single command:
>> run100m = loadtxt("run100m.d")
We split the data into two arrays t and x by:
>>t = run100m[:,0]
>>x = run100m[:,1]
and plot the data using
>> plot(t,x), show()
If you experience a problem where Python cannot find the file, getting an error message like:
>> loadtxt("run100m.d")
... IOError: [Errno 2] No such file or directory: "run100m.d"
It means that the file run100m.d is not in your current working directory.
2.7.1 Example: Plot of Function and Derivative
Problem: Plot the function |
|
f (x ) = e−x 2 , |
|
|
|
|
(2.5) |
||
and its derivative by using the formula: |
|
|||
f ′(x ) |
|
f (x + h) − f (x − h) |
, |
(2.6) |
|
2h |
|
3http://folk.uio.no/malthe/mechbook/100m.d.
2.7 Reading Real Data |
23 |
|||
|
|
|
|
|
|
|
|
|
|
Fig. 2.3 Plot of f (x ) as a function of x and its derivative d f /d x as a function of x calculated using a numerical method
as an approximation for the derivative on the interval −5 ≤ x ≤ 5. You may use the value h = 0.001 for h.
Solution: The function can be plotted directly by a vectorized approach:
>>from pylab import *
>>x = linspace(-5,5,1000)
>>f = exp(-x**2)
>>plot(x,f), show()
In order to use the numerical approximation for the derivative, we need to perform the approximation for each of the x -values in the x-array. We access them by a for-loop through the 1000 elements in the x-array:
>>h = 0.001
>>df = zeros(1000,float)
>>for i in range(1000):
... df[i] = (exp(-(x[i]+h)**2)-exp(-(x[i]-h)**2))/(2*h) >> plot(x,df), show()
The resulting plot is shown in Fig. 2.3.
Even simple problems such as these are useful to implement as scripts saved in a file, since this makes debugging—the process of finding and removing errors in the script—simpler. If you make a small mistake, you have to retype all the commands when you operate on the command line, but if you use a script, you simply make a small change in the script, rerun, and that is it.
Summary
Using Python as a calculator:
•Direct calculations are done on the command line
>> 10.0*sin(pi/3)+4.0**3
•Defining and reusing variables
>> a = 2.0, b = 4.5, c = a**2 + b**2
24 |
2 Getting Started with Programming |
•Vectorized plotting of functions
>> x=linspace(0,10,0.01), y=exp(-x)*sin(x),plot(x,y),show()
•Vectorized operations are denoted by a leading . and are done element-wise.
Functions and scripts:
•A script is a sequence of executable commands stored in a separate .py-file
–All variables are available on from the command line afterwards
–The script is run by typing F5 in the editor
–Scripts allow rapid rerunning a program after changes in parameters
•A function can be part of a script of stored in a separate .py file. It has the syntax
def myfunction(a,b,c): v = a*b*c
d = v**2 y = 2.0*d return y
•The name of the function (myfunction) should be the name of the .py-file. Variables defined inside the function, such as v and d are not available outside the function
Plotting:
• You plot two arrays t and x versus each other by
plot(t,x,’-b’), xlabel(’t (s)’), ylabel(’x (m)’)
•Plotting markers and symbols are listed in Table 2.3.
•Plotting several data-sets in the same plot:
plot(t1,x1,’-b’,t2,x2,’-r’)
Table 2.3 Line markers, colors and plotting symbols
Colors |
|
Lines |
|
Symbols |
|
Symbols |
|
|
|
|
|
|
|
|
|
b |
blue |
- |
solid |
. |
point |
v |
triangle |
|
|
|
|
|
|
|
(down) |
|
|
|
|
|
|
|
|
g |
green |
: |
dotted |
o |
circle |
ˆ |
triangle |
|
|
|
|
|
|
|
(up) |
|
|
|
|
|
|
|
|
r |
red |
-. |
dashdot |
x |
x-mark |
< |
triangle |
|
|
|
|
|
|
|
(left) |
|
|
|
|
|
|
|
|
c |
cyan |
– |
dashed |
+ |
plus |
> |
triangle |
|
|
|
|
|
|
|
(right) |
|
|
|
|
|
|
|
|
m |
magenta |
(none) |
no line |
* |
star |
p |
pentagram |
|
|
|
|
|
|
|
|
y |
yellow |
|
|
d |
diamond |
h |
hexagram |
|
|
|
|
|
|
|
|
k |
black |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w |
white |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2.7 Reading Real Data |
25 |
or
plot(t1,x1,’-b’)
hold(’on’), plot(t2,x2,’-r’), hold(’off’)
• Combining several plots into one figure:
subplot(2,1,1), plot(t1,x1,’-b’) subplot(2,1,2), plot(t2,x2,’-r’)
•Saving a figure to a file: either by using the save button from the figure window, or you can save a figure as a pdf from the command line by
savefig(’myfigure.pdf’)
where myfigure.pdf is the name of the generated file.
Loops:
• for-loops run a counter sequentially through a list of values:
for i in range(100): x[i] = sin(i/100.0)
• while-loops run until a given expression is true
i = 0 while (i<100): i = i + 1
x[i] = sin(i/100.0)
Expressions:
•if-statements are used to run a sequence of commands given a particular expression is true:
if (x>10.0): y = 10.0
else:
y = -10.0
•Expressions return true (1) or false (0) and can be joined using logical operators such as or and and as shown in Table 2.4
Table 2.4 Expressions and operators used in Python
Expression |
Name |
Example |
Operator |
Name |
Example |
|
|
|
|
|
|
== |
equal |
(x==0.0) |
and |
logical AND |
(x==0.0)and |
|
|
|
|
|
(y>0.0) |
|
|
|
|
|
|
!= |
not-equal |
(x!=0.0) |
or |
logical OR |
(x!=0.0)or |
|
|
|
|
|
(y>0.0) |
|
|
|
|
|
|
>= |
greater than or equal |
(x>=0.0) |
|
|
|
|
|
|
|
|
|
<= |
less than or equal |
(x<=0.0) |
|
|
|
|
|
|
|
|
|
> |
greater than |
(x>0.0) |
|
|
|
|
|
|
|
|
|
< |
less than |
(x<0.0) |
|
|
|
|
|
|
|
|
|
26 |
2 Getting Started with Programming |
Exercises |
|
2.1 |
Seconds. |
(a) Write a script that calculates the number of seconds, s, given the number of hours, h, according to the formula s = 3600 h.
(b) Use the script to find the number of seconds in 1.5, 12 and 24 h.
2.2 Spherical mass.
(a) Write a script that calculates the mass of a sphere given its radius r and mass density ρ according to the formula m = (4π/3) ρr 3.
(b) Use the script to find the mass of a sphere of steel of radius r = 1 mm, r = 1 m, and r = 10 m.
2.3 Angle.
(a) Write a function that for a point (x , y) returns the angle θ from the x -axis using the formula θ = arctan (y/x ).
(b) Find the angles θ for the points (1, 1), (−1, 1), (−1, −1), (1, −1).
(c) How would you change the function to return values of θ in the range [0, 2π ]?
2.4 Unit vector.
(a) Write a function that returns the two-dimensional unit vector, (u x , u y ), corresponding to an angle θ with the x -axis. You can use the formula (u x , u y ) = (cos θ , sin θ ), where θ is given in radians.
(b) Find the unit vectors for θ = 0, π/6, π/3, π/2, 3π/2.
(c) Rewrite the function to instead take the argument θ in degrees.
2.5 Plotting the normal distribution. The normal distribution, often called the Gaussian distribution, is given as:
P (x |
|
µ, σ ) |
|
1 |
|
e−(x −µ)2/ 2σ 2 |
, |
(2.7) |
|
; |
= √ |
|
|||||||
|
|
2π σ 2 |
|
|
|
where µ is the average and σ is the standard deviation.
(a) Make a function normal(x,mu,sigma) that returns the normal distribution value, P (x , µ, σ ) as given by the formula.
(b) Use this function to plot the normal distribution for −5 < x < 5 for µ = 0 and σ = 1.
(c) Plot the normal distribution for −5 < x < 5 for µ = 0 and σ = 2 and for σ = 0.5 in the same plot.
(d) Plot the normal distribution for −5 < x < 5 for σ = 1 and µ = 0, 1, 2 in three subplots above each other.
2.6 Plotting 1/x n . The function f (x ; n) is given as f (x ; n) = x −n . (a) Make a function fvalue(x,n) which returns the value of f (x ; n).
(b) Use this function to plot 1/x , 1/x 2 and 1/x 3 in the same plot for −1 < x < 1.
2.7 Reading Real Data |
27 |
2.7 Plotting sin(x )/x n . The function g(x ; n) is given as:
|
|
|
|
sin(x ) |
|
|
|
(2.8) |
|||||
|
|
|
g(x ; n) = |
|
|
. |
|
|
|
||||
|
|
|
|
x n |
|
|
|
||||||
( |
a |
) Make a function |
gvalue(x,n) |
which |
returns the value of g(x |
; |
n). |
||||||
|
|
|
2 |
and sin(x )/x |
3 |
|
|
||||||
(b) Use this function to plot sin(x )/x , sin(x )/x |
|
|
in the same plot for |
−5 < x < 5.
(c) Use the help function to find out how to place legends for each of the plots into the figure.
2.8 Logistic map. The iterative mapping x (i + 1) = r x (i ) (1 − x (i )) is called the logistic map.
(a) Make a function logistic(x,r) which returns the value of x (i + 1) given x (i ) and r as inputs.
(b) Write a script with a loop to calculate the first 100 steps of the logistic map starting from x (1) = 0.5. Store all the values in an array x with n = 100 elements and plot x as a function of the number of steps i :
(c) Explore the logistic map for r = 1.0, 2.0, 3.0 and 4.0.
2.9 Euler’s method. In mechanics, we often use Euler’s method to determine the motion of an object given how the acceleration depends on the velocity and position of an object. For example, we may know that the acceleration a(x , v) is given as a(x , v) = −k x − cv. If we know the position x and the velocity v at a time t = 0: x (0) = x0 = 0 and v(0) = v0 = 1, we can use Euler’s method to find the position
and velocity after a small timestep t : |
|
||
v1 |
= v(t0 + |
t ) = v(t0) + a(v(t0), x (t0))Δt |
(2.9) |
x1 |
= x (t0 + |
t ) = x (t0) + v(t0)Δt |
(2.10) |
v2 |
= v(t1 + |
t ) = v(t1) + a(v(t1), x (t1))Δt |
(2.11) |
x2 |
= x (t1 + |
t ) = x (t1) + v(t1)Δt |
(2.12) |
and so on. We can therefore use this scheme to find the position x (t ) and the velocity v(t ) as function of time at the discrete values ti = i t in time.
(a) Write a function acceleration(v,x,k,C) which returns the value of a(x , v) = −k x − C v.
(b) Write a script that calculates the first 100 values of x (ti ) and v(ti ) when k = 10, C = 5, and t = 0.01. Plot x (t ), v(t ), and a(t ) as functions of time.
(c) What would you need to change to instead find x (t ) and v(t ) is the acceleration was given as a(v, x ) = k sin(x ) − C v?
2.10 Throwing two dice. You throw a pair of six-sided dice and sum the number from each of the dice: Z = X 1 + X 2, where Z is the sum of the results from dice 1, X 1, and dice 2, X 2. If we perform this experiment many times (N ), we can find the
28 |
2 Getting Started with Programming |
average and standard deviation from standard estimators from statistics. The average,Z , of Z is estimated from:
|
|
|
Z |
|
|
|
N |
|
|
|
|
(2.13) |
|
|
= |
1 Z j , |
|
|
|
||||||
|
|
N |
j =1 |
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
|
|
|
|
|
and the standard deviation, Z , is estimated from: |
|
|
|
|
||||||||
|
|
|
1 |
|
N |
|
|
2 |
|
|
||
Z |
|
|
|
|
|
(Z j |
− |
Z |
|
. |
(2.14) |
|
|
|
|
|
|
||||||||
|
= N − 1 j =1 |
|
|
|
(a) Write a function that returns an array of N values for Z .
(b) Write a function that returns an estimate of the average of an array z using the formula provided.
(c) Write a function that returns an estimate of the standard deviation of an array z using the formula provided.
(d) Find the average and standard deviation for N = 100 throws of two dice.
2.11 Reading data. The file trajectory.dat4 contains a list of numbers:
t0 x0 y0
t1 x1 y1
.. .. ..
tn xn yn
corresponding to the time t(i) measured in seconds, and the x and y positions x(i) and y(i) measured in meters for the trajectory of a projectile.
(a) Read the data file into the arrays ‘t, x, and y.
(b) Plot the x and y positions as function of time in two plots above each other. (c) Plot the (x , y) position of the object in a plot with x and y on the two axes.
2.12 Numerical integration of a data-set. The file velocityy.dat5 contains a list of numbers:
t0 v0
t1 v1
.. .. ..
tn vn
corresponding to the time t(i) measured in seconds, and the velocity y(i) measured in meters per second for the trajectory of a projectile.
(a) Read the data file into the arrays t, and v. (b) Plot v(t ) as function of time.
4http://folk.uio.no/malthe/mechbook/trajectory.dat.
5http://folk.uio.no/malthe/mechbook/velocityy.dat.
2.7 Reading Real Data |
29 |
For a data-set t(i), v(i), you can estimate the function corresponding to the integral of v(t ) with respect to t at the times ti using the iterative scheme:
y(t1) y(t0) + v(t0) (t1 − t0) |
(2.15) |
y(t2) y(t1) + v(t1) (t2 − t1) |
(2.16) |
. . . . . . |
(2.17) |
y(tn ) y(tn−1) + v(tn−1) (tn − tn−1) |
(2.18) |
where v(ti ) =‘v(i)’ and ti =‘t(i)’ You can assume that the motion starts at y(t0) = 0.0m at t = t0.
(c) Write a script to calculate the time integral y(ti ) of the dataset using this formula. Implement using a for-loop.
(d) Plot the position y(t ) and the derivative v(t ) as functions of time in two plots above each other.