import antigravity
Here is a formula for the position of a ball in vertical motion, starting at ground level (i.e. $y=0$) at time $t=0$: $$ y(t) = v_0t- \frac{1}{2}gt^2 $$
where:
The computational task is: given $v_0$, $g$ and $t$, compute the value $y$.
How do we program this task? A program is a sequence of instructions given to the computer. However, while a programming language is much simpler than a natural language, it is more pedantic. Programs must have correct syntax, i.e., correct use of the computer language grammar rules, and no misprints.
So let's execute a Python statement based on this example. Evaluate $y(t) = v_0t- \frac{1}{2}gt^2$ for $v_0=5$, $g=9.81$ and $t=0.6$. If you were doing this on paper you would probably write something like this: $$ y = 5\cdot 0.6 - {1\over2}\cdot 9.81 \cdot 0.6^2.$$ Happily, writing this in Python is very similar:
print(5*0.6 - 0.5*9.81*0.6**2)
1.2342
Go ahead and mess with the code above to see what happens when you change values and rerun. To see what I mean about programming being pedantic, see what happens if you replace **
with ^
:
print(5*0.6 - 0.5*9.81*0.6^2)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-22bae6d4b595> in <module>() ----> 1 print(5*0.6 - 0.5*9.81*0.6^2) TypeError: unsupported operand type(s) for ^: 'float' and 'int'
or write
rather than print
:
write(5*0.6 - 0.5*9.81*0.6**2)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-4-b1d15385295b> in <module>() ----> 1 write(5*0.6 - 0.5*9.81*0.6**2) NameError: name 'write' is not defined
While a human might still understand these statements, they do not mean anything to the Python interpreter. Rather than throwing your hands up in the air whenever you get an error message like the above (you are going to see many during the course of these lectures!!!) train yourself to read the message patiently to get an idea what it is complaining about and re-read your code from the perspective of the pedantic Python interpreter.
Error messages can look bewildering (frustrating etc.) at first, but it gets much easier with practise.
From mathematics you are already familiar with variables (e.g. $v_0=5,\quad g=9.81,\quad t=0.6,\quad y = v_0t -{1\over2}gt^2$) and you already know how important they are for working out complicated problems. Similarly, you can use variables in a program to make it easier to read and understand.
v0 = 5
g = 9.81
t = 0.6
y = v0*t - 0.5*g*t**2
print(y)
1.2342
This program spans several lines of text and uses variables, otherwise the program performs the same calculations and gives the same output as the previous program.
In mathematics we usually use one letter for a variable, resorting to using the Greek alphabet and other characters for more clarity. The main reason for this is to avoid becoming exhausted from writing when working out long expressions or derivations. However, when programming you should use more descriptive names for variable names. This might not seem like an important consideration for the trivial example here but it becomes increasingly important as the program gets more complicated and if someone else has to read your code.
Permitted variable names include:
Variable names are case-sensitive (i.e. "'a'" is different from "'A'"). Let's rewrite the previous example using more descriptive variable names:
initial_velocity = 5
acceleration_of_gravity = 9.81
TIME = 0.6
VerticalPositionOfBall = initial_velocity*TIME - 0.5*acceleration_of_gravity*TIME**2
print(VerticalPositionOfBall)
1.2342
Certain words have a special meaning in Python and cannot be used as variable names. These are: and, as, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, with, while, and yield.
Not everything written in a computer program is intended for execution. In Python anything on a line after the '#' character is ignored and is known as a comment. You can write whatever you want in a comment. Comments are intended to be used to explain what a snippet of code is intended for. It might for example explain the objective or provide a reference to the data or algorithm used. This is both useful for you when you have to understand your code at some later stage, and indeed for whoever has to read and understand your code later.
# Program for computing the height of a ball in vertical motion.
v0 = 5 # Set initial velocity in m/s.
g = 9.81 # Set acceleration due to gravity in m/s^2.
t = 0.6 # Time at which we want to know the height of the ball in seconds.
y = v0*t - 0.5*g*t**2 # Calculate the vertical position
print(y)
1.2342
Make a program where you set a length given in meters and then compute and write out the corresponding length measured in inches, in feet, in yards, and in miles. Use the fact that one inch is 2.54 cm, one foot is 12 inches, one yard is 3 feet, and one British mile is 1760 yards. As a verification, a length of 640 meters corresponds to 25196.85 inches, 2099.74 feet, 699.91 yards, or 0.3977 miles.
Often we want to print out results using a combination of text and numbers, e.g. "'At t=0.6 s, y is 1.23 m'". Particularly when printing out floating point numbers we should never quote numbers to a higher accuracy than they were measured. Python provides a printf formatting syntax exactly for this purpose. We can see in the following example that the slot %g
was used to express the floating point number with the minimum number of significant figures, and the slot %.2f
specified that only two digits are printed out after the decimal point.
print("At t=%g s, y is %.2f m." % (t, y))
At t=0.6 s, y is 1.23 m.
Notice in this example how the values in the tuple (t, y)
are inserted into the slots.
Sometimes we want a multi-line output. This is achieved using a triple quotation (i.e. """
):
print("""At t=%f s, a ball with
initial velocity v0=%.3E m/s
is located at the height %.2f m.
""" % (t, v0, y))
At t=0.600000 s, a ball with initial velocity v0=5.000E+00 m/s is located at the height 1.23 m.
The drag force, due to air resistance, on an object can be expressed as $$F_d = \frac{1}{2}C_D\rho AV^2$$ where $\rho$ is the density of the air, $V$ is the velocity of the object, $A$ is the cross-sectional area (normal to the velocity direction), and $C_D$ is the drag coefficient, which depends heavily on the shape of the object and the roughness of the surface. The gravity force on an object with mass $m$ is $F_g = mg$, where $g = 9.81ms^{−2}$. Write a program that computes the drag force and the gravity force on an object. Write out the forces with one decimal in units of Newton ($N = kgm/s^2$). Also print the ratio of the drag force and the gravity force. Define $C_D$, $\rho$, $A$, $V$, $m$, $g$, $F_d$, and $F_g$ as variables, and put a comment with the corresponding unit.
As a computational example, you can initialize all variables with values relevant for a football kick. The density of air is $\rho = 1.2 kg m^{−3}$. For any ball, we have obviously that $A = \pi a^2$, where $a$ is the radius of the ball, which can be taken as $11cm$ for a football. The mass of the ball is $0.43kg$. $C_D$ can be taken as $0.2$. Use the program to calculate the forces on the ball for a hard kick, $V = 120km/h$ and for a soft kick, $V = 10km/h$ (it is easy to make the mistake of mixing inconsistent units, so make sure you compute with V expressed in m/s). Make sure you use the printf formatting style introduced above.
Consider the random mathematical expression, ${5\over9} + 2a^4/2$, implemented in Python as 5.0/9 + 2*a**4/2
.
The rules for evaluating the expression are the same as in mathematics: proceed term by term (additions/subtractions) from the left, compute powers first, then multiplication and division. Therefore in this example the order of evaluation will be:
r1 = 5.0/9
r2 = a**4
r3 = 2*r2
r4 = r3/2
r5 = r1 + r4
Use parenthesis to override these default rules. Indeed, many programmers use parenthesis for greater clarity.
Let p be a bank's interest rate in percent per year. An initial amount A has then grown to $$A\left(1+\frac{p}{100}\right)^n$$ after n years. Write a program for computing how much money 1000 euros have grown to after three years with a 5% interest rate.
What if we need to compute $\sin x$, $\cos x$, $\ln x$, etc. in a program? Such functions are available in Python's math module. In fact there is a vast universe of functionality for Python available in modules. We just import in whatever we need for the task at hand.
In this example we compute $\sqrt{2}$ using the sqrt function in the math module:
import math
r = math.sqrt(2)
print(r)
1.4142135623730951
or:
from math import sqrt
r = sqrt(2)
print(r)
1.4142135623730951
or:
from math import * # import everything in math
r = sqrt(2)
print(r)
1.4142135623730951
Another example:
from math import sin, cos, log
x = 1.2
print(sin(x)*cos(x) + 4*log(x)) # log is ln (base e)
1.0670178174513938
The bell-shaped Gaussian function, $$f(x)=\frac{1}{\sqrt{2\pi}s}\exp\left(-\frac{1}{2} \left(\frac{x-m}{s}\right)^2\right)$$ is one of the most widely used functions in science and technology. The parameters $m$ and $s$ are real numbers, where $s$ must be greater than zero. Write a program for evaluating this function when $m = 0$, $s = 2$, and $x = 1$. Verify the program's result by comparing with hand calculations on a calculator.
Given a quadratic equation, $$ax^2 + bx + c = 0,$$ $$x1 = \frac{−b + \sqrt{b^2 −4ac}}{2a},$$ and $$x2 = \frac{−b − \sqrt{b^2 −4ac}}{2a}.$$ Why does the following program not work correctly?
a = 2; b = 1; c = 2
from math import sqrt
q = sqrt(b*b - 4*a*c)
x1 = (-b + q)/2*a
x2 = (-b - q)/2*a
print(x1, x2)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-1-3c400308bf7d> in <module>() 1 a = 2; b = 1; c = 2 2 from math import sqrt ----> 3 q = sqrt(b*b - 4*a*c) 4 x1 = (-b + q)/2*a 5 x2 = (-b - q)/2*a ValueError: math domain error