- •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
2.4 Plotting a Function |
19 |
Notice how simple the vectorized code is—it is almost identical to the mathematical formula. We only have to define the range of x-values before we call the sin(x) function. Beautiful and powerful.
The program above generates the solid line in Fig. 2.1. However, this plot has too sharp corners, because we have too few data-points. Let us generate 1000 x -values and plot sin(x ) with this resolution in the same plot:
>>x = linspace(0,10,1000)
>>y = sin(x)
>>hold(’on’)
>>plot(x,y,’:’);
>>hold(’off’)
>>show()
The result is shown in Fig. 2.1 with a dotted line. Here, we have used a few more tricks. We use the command hold(’on’) to ensure that Python does not generate a new plot, which would remove the previous one, but instead plots the data in the same plot as we have already used. Typing hold(’off’) stops this behavior— otherwise all subsequent plots will be part of the same plot. We have also used the string ’:’ to tell Python that we want a dotted line. You can find more plotting methods in the summary at the end of the chapter.
The vectorization technique is very general, and usually allows you to translate a mathematical formula to Python almost formula by formula. For example, we plot the function
f (x ) |
= |
x 2e−ax sin(π x ) , |
(2.3) |
|
|
|
from x = 0 to x = 10 by typing (when a = 1):
>>x = linspace(0,10,1000)
>>a = 1.0
>>f = x**2*exp(-a*x)*sin(pi*x)
>>plot(x,f), show()
As soon as you have learned to transcribe mathematical expressions from the mathematical notation to Python you are ready to calculate and plot any function.
The technique of vectorization is a powerful and efficient technique. Python is usually very fast at calculating vectorized commands. And we can write very elegant programs using such techniques, ensuring that the Python code follows the mathematical formulation closely, which makes the code easy to understand.
2.5 Random Numbers
Sometimes you need randomness to enter your physical simulation. For example, you may want to model the motion of a tiny dust of grain in the air bouncing about due to random hits by the air molecules, so called Brownian motion. We model this by having the grain move a random distance during a given time interval. How do we create random numbers on the computer? Unfortunately, we cannot generate really
20 |
2 Getting Started with Programming |
random numbers, but most programs have decent pseudo-random number generators, which generate a sequence of numbers that appear to be random. In Python we can simulate the throw of a dice using
>> randint(6) + 1 3
where randint(n) generates a random integer between 0 and n − 1, where each outcome has the same probability. If you type the command several times, you will get a new answer each time. Python includes several functions that return random numbers: It can generate random real numbers between 0 and 1 using the rand function and normal-distributed numbers (with average 0 and standard derivation 1) using the function randn.
2.6 Conditions
Now, if we return to discuss the motion of the grain of dust, we want to model its motion according to a simple rule: I throw a dice. If I get between 1 and 3, the grain moves a step forward, otherwise it moves a step backward. How can we handle such conditions? We need a set of conditional statements, so that we can perform a given set of commands when a particular condition is fulfilled. We need an if-statement:
if (expr): <statement a1> <statement a2>
..
else:
<statement b1> <statement b2>
..
Here the expression (expr) is an expression such as randint(6)+1>3 which may be true or false. If the expression is true, statements a1, a2, ... are executed, otherwise the statements b1, b2, ... are executed.
Let us use this to find the motion of the grain. Every time we throw the dice, the grain moves a distance d x = ±1. If the grain is at position xi at step i , the grain will be at a position
xi +1 = xi + d x . |
(2.4) |
at step i + 1. We can use this rule and an if-statement to write the script to find the position at subsequent steps i = 0, 1, 2, . . .:
from pylab import * n = 1000
x = zeros(n,float) for i in range(n):
if (randint(6)+1<=3): dx = -1
else:
dx = 1 x[i+1] = x[i] + dx
2.6 Conditions |
21 |
|||
|
|
|
|
|
|
|
|
|
|
Fig. 2.2 Plot of the position x (i ) of a random walker (a bouncing grain of dust) as a function of the number of steps i done (left), and when the walker is constrained to the zone −5 ≤ x ≤ +5 (right)
The resulting motion is shown in Fig. 2.2.
We will use if-statements throughout the text, often to enforce particular conditions on the motion. For example, we could add a level of complexity to the motion of the grain by requiring that the grain moved inside a narrow channel of width 10: The grain cannot move outside a region spanning from −5 to +5:
#start1
from pylab import * n = 1000
x = zeros(n,float) for i in range(1,n):
if (randint(6)+1<=3): dx = -1
else:
dx = +1
x[i] = x[i-1] + dx if (x[i]> 5):
x[i] = 5 if (x[i]<-5): x[i] = -5
#end1
plot(x),
xlabel(’i’), ylabel(’x(i)’) show()
The resulting motion xi as a function of i is shown in Fig. 2.2.
For the interested reader, we include a particularly compact formulation of the random walk, which you may have fun trying to understand.
>>x = cumsum(2*(randint(1,7,1000)<=3)-1)
>>plot(x), show()
You can research this expression by using the help-function in Python
>> help(cumsum)