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.
# Solution
p = 5 # Interest rate in percent per year
A = 1000.0 # Initial amount of money in euros
n = 3 # Number of years since initial deposit
amount = A*(1 + p/100.0)**n
print("The amount of money in the account after %d years is: %.2f euros" % (n, amount))
The amount of money in the account after 3 years is: 1157.63 euros
Suppose somebody has written the simple one-line program below for computing $\sin(1)$. Try to run this program. What is the problem? Try to fix it. (Reminder - the math function needs to be imported).
# Broken program
x=1; print 'sin(%g)=%g' % (x, sin(x))
File "<ipython-input-2-cb035346cc59>", line 2 x=1; print 'sin(%g)=%g' % (x, sin(x)) ^ SyntaxError: invalid syntax
# Fixed program (Solution). You must import the 'sin' function from the 'math' module.
from math import sin
x=1; print('sin(%g)=%g' % (x, sin(x)))
sin(1)=0.841471
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.
# Solution
from math import exp, pi, sqrt
# Parameters.
m = 0.0 # The mean
s = 2.0 # The standard deviation
x = 1.0
coefficient = 1.0/(sqrt(2*pi)*s)
result = coefficient*exp(-0.5*((x - m)/s)**2)
print("The value of the Gaussian function with x = %.2f is %.4f" % (x, result))
The value of the Gaussian function with x = 1.00 is 0.1760
# Alternative Solution. This uses two concepts that will be covered
# in later lectures: 1) Python functions, and 2) casting.
from math import exp, pi, sqrt
def f(x):
# Parameters. These could be given to the function as arguments.
m = 0 # The mean
s = 2 # The standard deviation
coefficient = 1/(sqrt(2*pi)*s)
result = coefficient*exp(-0.5*(float(x - m)/s)**2)
return result
x = 1
print("The value of the Gaussian function with x = %.2f is %.4f" % (x, f(x)))
The value of the Gaussian function with x = 1.00 is 0.1760
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).
# Solution
from math import pi
# The density of air, in kg/(m**3)
density = 1.2
# Mass of the ball, in kg
mass = 0.43
# Radius of the ball, in m
a = 0.11
# Cross-sectional area (m**2)
A = pi*(a**2)
# Non-dimensional
drag_coefficient = 0.2
# Acceleration due to gravity, in m/(s**2)
g = 9.81
# Force due to gravity - independent of the velocity
F_g = mass*g
print("The force due to gravity is %.1f N" % F_g)
# Velocity of the ball after a hard kick, converted from km/h to m/s
V_hard = 33.333333333
# Drag force acting on the ball
F_d = 0.5*drag_coefficient*density*A*(V_hard**2)
print("The drag force acting on the ball after a hard kick is %.1f N" % F_d)
print("The ratio of the drag force and the gravitational force is %.1f" % (F_d/F_g))
# Velocity of the ball after a hard kick, converted from km/h to m/s
V_soft = 2.777777778
# Drag force acting on the ball
F_d = 0.5*drag_coefficient*density*A*(V_soft**2)
# The drag force will be so small that this prints out as "0.0 N"
print("The drag force acting on the ball after a soft kick is %.1f N" % F_d)
print("The ratio of the drag force and the gravitational force is %.1f" % (F_d/F_g))
The force due to gravity is 4.2 N The drag force acting on the ball after a hard kick is 5.1 N The ratio of the drag force and the gravitational force is 1.2 The drag force acting on the ball after a soft kick is 0.0 N The ratio of the drag force and the gravitational force is 0.0
Given a quadratic equation, $$ax^2 + bx + c = 0,$$ $$x1 = −b+\frac{\sqrt{b^2 −4ac}}{2a},$$ and $$x2 = −b−\frac{\sqrt{b^2 −4ac}}{2a}.$$ Why does the following program not work correctly?
# Original program
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-7-3751cf745c60> in <module>() 2 a = 2; b = 1; c = 2 3 from math import sqrt ----> 4 q = sqrt(b*b - 4*a*c) 5 x1 = (-b + q)/2*a 6 x2 = (-b - q)/2*a ValueError: math domain error
# Solution
a = 2; b = 5; c = 2
from math import sqrt
# The error above was caused by taking the square root of a negative number. Try changing b to 5.
q = sqrt(b*b - 4*a*c)
# You need to make sure that -b is divided by 2*a, and also include brackets in the denominator...
x1 = (-b + q)/(2*a)
# ...and again here. This is because of operator precedence.
x2 = (-b - q)/(2*a)
print(x1, x2)
-0.5 -2.0
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.
# Solution
meters = 640
# 1 inch = 2.54 cm. Remember to convert from 2.54 cm to 0.0254 m here.
inches = meters/0.0254
print("%.4f meters = %.4f inches" % (meters, inches))
feet = inches/12.0 # 1 foot = 12 inches
print("%.4f meters = %.4f feet" % (meters, feet))
yards = feet/3.0 # 1 yard = 3 feet
print("%.4f meters = %.4f yards" % (meters, yards))
miles = yards/1760.0 # 1 yard = 1760 miles
print("%.4f meters = %.4f miles" % (meters, miles))
640.0000 meters = 25196.8504 inches 640.0000 meters = 2099.7375 feet 640.0000 meters = 699.9125 yards 640.0000 meters = 0.3977 miles