- •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
Chapter 2
Getting Started with Programming
In this text we integrate the use programming techniques and tools in our study of physics. You will therefore need to know a few programming basics in order to profit from this approach. However, if you do not have a relevant background in introductory scientific programming, do not despair. Experience shows that you can learn to program through your first physics course—many students have done this successfully and with good results. In order to prepare you for the main text, this chapter provides an introduction to programming.
2.1 A Python Calculator
We are in this text using the editor Spyder to work with Python. (Alternatively you can use iPython—type ipython in a terminal window to start). When you start Spyder, you get a window where you can type commands to be executed. Click on the Console window in the lower right corner and type:
>> 9*4 36
Notice the difference between the text you type, which is preceded by », and the results generated by the program, which are typeset without indentation.
Python can be used as an advanced calculator by typing expressions on the command line:
>> 3*2**3+4 28
Standard operators are plus (+), minus (-), multiplication (*), division (/), and power (**). Powers of ten are input using e:
>>4.5e4
45000
>>2.5e-10
2.5e-10
which also shows how Python displays numbers.
© Springer International Publishing Switzerland 2015 |
9 |
A. Malthe-Sørenssen, Elementary Mechanics Using Python,
Undergraduate Lecture Notes in Physics, DOI 10.1007/978-3-319-19596-4_2
10 |
2 Getting Started with Programming |
Python has most mathematical functions and constants built in, such as pi, cos, sin, and exp: To use them we need to load the pylab module first:
>> from pylab import *
After this we are ready to use the mathematical functions:
>> 4*pi 12.566370614359172
Python uses radians for the trigonometric functions:
>> sin(pi/6) 0.49999999999999994
As you can see, Python does not always round off as you may expect, even though the answer is close to the exact answer (si n(π/6) = 0.5). You can find a list of useful syntax, functions and expressions in the summary.
Python becomes more useful when you have a formula you want to use. For example, you may want to use the formula:
|
9 |
|
TF = |
5 TC + 32 , |
(2.1) |
to find the temperature, TF , in Fahrenheit, given the temperature TC in centigrade. We may type this formula directly into Python
>> TF = 9/5*TC + 32
Traceback (most recent call last): File "<stdin >"...
NameError: name "TC" is not defined
Ooops. That did not work, because Python does not yet know the value of TC . We give TC a value and retype the formula:
>>TC = 40.0
>>TF = 9.0/5.0*TC + 32.0
To see the answer, type
print TF 104.0
Note that we are typing .0 after each number. This is because Python differs between integers (1, 2, 3, 4, . . .) and real numbers, (1.0, 2.3, 4.9, . . .). Typing .0 or only . (a dot) after each number ensures that the numbers are real. Also notice that integer division is not the same as real division. With integer division 9/5 = 1, while with real numbers 9.0/5.0 = 1.8.
Instead of retyping the formula, you can use the up arrow to find your previously typed commands and execute them again. We have now defined a variable, TC. You can see the value given to TC by typing:
>> print TC 40
Notice that we assigned a value to the variable TF through a calculation. We have not introduced a function for TF. What does this mean? It means that if you change the value of TC, the value of TF will not change automatically unless you retype
2.1 A Python Calculator |
11 |
the formula for TF. You can check this by assigning TC a new value, and then ask Python for the value of TF:
>>TC = 50
>>print TF 104.0
This is an important aspect of a programming language such as Python a variable does not change value unless you assign a new value to it!
2.2 Scripts and Functions
However, we do not want to type in the whole formula each time we want to calculate a new value for TF . Instead we can make a script, a group of several statements, or a function, similar to an internal function such as sin.
Scripts
We can group several statements into a script, which we can reuse. You do this by opening a new file in the File menu in Spyder: File → New file.... This opens a new window with an editor. Here you can now type (or copy) the commands we already used:
TC = 40.0
TF = 9.0/5.0*TC + 32.0 print TF
Now, we need to save the script. In the editor window you do: ‘File‘→‘Save‘. You must give the script a name and choose where to place it. This will generate a file with an extension .py - we call such a file a py-file, because it shows that the file contains a Python script/program. You run the program from the editor window by typing the F5 key. A dialog box will show up. Select Execute in current
..., leave all other options as they are and click the Run button. As a result the commands in the script are executed as if they were typed into the Python window, and the resulting output is shown in the Python window:
104.0
You can now change the value of TC in the script and rerun the script to redo the calculation for another temperature. Notice that we wrote the script so that the temperature TC is assigned inside the script. This means that if you change the value of TC on the command line, for example by typing:
>> TC = 45.0
and then run the script—the script will not use this new value of TC, but instead use the value from inside the script.
12 |
2 Getting Started with Programming |
Writing scripts to solve simple problems will be our standard operating procedure throughout this text. This is an efficient way to develop a simple program, change the parameters (such as changing TC), and rerun the program with new parameters. While this is practical for developing short programs and solving simple problems, it is not good programming practice. In general, we encourage the development of good programming practices, but in this text we will prioritize making the code as simple as possible.
Functions
From a programming perspective, it is better to introduce a function to calculate the temperature. A user defined function acts just like a predefined mathematical function such as sin or exp. We define a function by opening a new py-file: Push File → New File .... We define a function by typing the following into the editor:
def convertF(TC):
# Converts from centigrade to Fahrenheit TF = 9.0/5.0*TC + 32.0
return TF
and save it with the name convertF.py.
What do these statements do? We define a function by the command def followed by the name of the function, the input arguments. The line must end with a colon, :. Here the only argument is the temperature in centigrades, TC. Inside the function we must calculate the value of TF, because this is the value the function is supposed to calculate. Finally, we specify that the function will return the value of the variable TF.
We call our new function by typing:
>> convertF(45.0) 113.0
Notice that Python requires each such used-defined function to either be in the same file as the script using the function, of the function must be in a separate file, and that the file must be in the search path for Python. This means that the file convertF.py must be in the current directory or in the standard Python directory for this to work. I suggest that you always save the functions you need in the same directory as you save the scripts you are currently working on, and that you make new directories for each problem.
A particular feature of functions is that the internal calculations and variables used inside the function are lost as soon as the function is finished. For example, Python may use several calculation steps if we call the sin function, but this is hidden from us. Outside the function, we only see the result of the function. To illustrate this, we could break our short function into several steps: