Write a program that prints a table with $t$ values in the first column and the corresponding $y(t) = v_0 t − 0.5gt^2$ values in the second column. Use $n$ uniformly spaced $t$ values throughout the interval [0, $2v_0/g$]. Set $v0 = 1$, $g = 9.81$, and $n = 11$.
# Make a table of function values
v0 = 1.0
g = 9.81
n = 11
# The step size between each uniformly spaced value.
# The end-points of the interval are included.
step = (2*v0/g)/(n-1)
for i in range(0, n):
t = step*i
y = v0*t - 0.5*g*t**2
print("%f, %f" % (t, y))
0.000000, 0.000000 0.020387, 0.018349 0.040775, 0.032620 0.061162, 0.042813 0.081549, 0.048930 0.101937, 0.050968 0.122324, 0.048930 0.142712, 0.042813 0.163099, 0.032620 0.183486, 0.018349 0.203874, 0.000000
Modify the program so that the $t$ and $y$ values are stored in two lists t and y. Thereafter, transverse the lists with a for loop and write out a nicely formatted table of t and y values using a zip construction.
# Use list comprehension to build the lists
t = [step*i for i in range(n)]
y = [v0*ti - 0.5*g*ti**2 for ti in t]
for (T,Y) in zip(t,y):
print("%f, %f" % (T, Y))
0.000000, 0.000000 0.020387, 0.018349 0.040775, 0.032620 0.061162, 0.042813 0.081549, 0.048930 0.101937, 0.050968 0.122324, 0.048930 0.142712, 0.042813 0.163099, 0.032620 0.183486, 0.018349 0.203874, 0.000000
Make a Python function gauss( x, m=0, s=1) for computing the Gaussian function $$f(x)=\frac{1}{\sqrt{2\pi}s}\exp\left(-\frac{1}{2} \left(\frac{x-m}{s}\right)^2\right)$$ Call the function and print out the result for x equal to −5, −4.9, −4.8, ..., 4.8, 4.9, 5, using default values for m and s.
# Implement a Gaussian function
from math import exp, pi, sqrt
def gaussian(x, m = 0, s = 1):
# Note: 'm' is the mean, and 's' is the standard deviation.
coefficient = 1/(sqrt(2*pi)*s)
result = coefficient*exp(-0.5*(float(x - m)/s)**2) # Be careful here. x, m, and s are supplied as integers - let's cast the numerator to a float to ensure we don't encounter integer division problems.
return result
x = -5.0
while x <= 5.0:
print("For x = %f, gaussian(x) = %f" % (x, gaussian(x)))
x = x + 0.1 # Increment x by a step size of 0.1.
For x = -5.000000, gaussian(x) = 0.000001 For x = -4.900000, gaussian(x) = 0.000002 For x = -4.800000, gaussian(x) = 0.000004 For x = -4.700000, gaussian(x) = 0.000006 For x = -4.600000, gaussian(x) = 0.000010 For x = -4.500000, gaussian(x) = 0.000016 For x = -4.400000, gaussian(x) = 0.000025 For x = -4.300000, gaussian(x) = 0.000039 For x = -4.200000, gaussian(x) = 0.000059 For x = -4.100000, gaussian(x) = 0.000089 For x = -4.000000, gaussian(x) = 0.000134 For x = -3.900000, gaussian(x) = 0.000199 For x = -3.800000, gaussian(x) = 0.000292 For x = -3.700000, gaussian(x) = 0.000425 For x = -3.600000, gaussian(x) = 0.000612 For x = -3.500000, gaussian(x) = 0.000873 For x = -3.400000, gaussian(x) = 0.001232 For x = -3.300000, gaussian(x) = 0.001723 For x = -3.200000, gaussian(x) = 0.002384 For x = -3.100000, gaussian(x) = 0.003267 For x = -3.000000, gaussian(x) = 0.004432 For x = -2.900000, gaussian(x) = 0.005953 For x = -2.800000, gaussian(x) = 0.007915 For x = -2.700000, gaussian(x) = 0.010421 For x = -2.600000, gaussian(x) = 0.013583 For x = -2.500000, gaussian(x) = 0.017528 For x = -2.400000, gaussian(x) = 0.022395 For x = -2.300000, gaussian(x) = 0.028327 For x = -2.200000, gaussian(x) = 0.035475 For x = -2.100000, gaussian(x) = 0.043984 For x = -2.000000, gaussian(x) = 0.053991 For x = -1.900000, gaussian(x) = 0.065616 For x = -1.800000, gaussian(x) = 0.078950 For x = -1.700000, gaussian(x) = 0.094049 For x = -1.600000, gaussian(x) = 0.110921 For x = -1.500000, gaussian(x) = 0.129518 For x = -1.400000, gaussian(x) = 0.149727 For x = -1.300000, gaussian(x) = 0.171369 For x = -1.200000, gaussian(x) = 0.194186 For x = -1.100000, gaussian(x) = 0.217852 For x = -1.000000, gaussian(x) = 0.241971 For x = -0.900000, gaussian(x) = 0.266085 For x = -0.800000, gaussian(x) = 0.289692 For x = -0.700000, gaussian(x) = 0.312254 For x = -0.600000, gaussian(x) = 0.333225 For x = -0.500000, gaussian(x) = 0.352065 For x = -0.400000, gaussian(x) = 0.368270 For x = -0.300000, gaussian(x) = 0.381388 For x = -0.200000, gaussian(x) = 0.391043 For x = -0.100000, gaussian(x) = 0.396953 For x = -0.000000, gaussian(x) = 0.398942 For x = 0.100000, gaussian(x) = 0.396953 For x = 0.200000, gaussian(x) = 0.391043 For x = 0.300000, gaussian(x) = 0.381388 For x = 0.400000, gaussian(x) = 0.368270 For x = 0.500000, gaussian(x) = 0.352065 For x = 0.600000, gaussian(x) = 0.333225 For x = 0.700000, gaussian(x) = 0.312254 For x = 0.800000, gaussian(x) = 0.289692 For x = 0.900000, gaussian(x) = 0.266085 For x = 1.000000, gaussian(x) = 0.241971 For x = 1.100000, gaussian(x) = 0.217852 For x = 1.200000, gaussian(x) = 0.194186 For x = 1.300000, gaussian(x) = 0.171369 For x = 1.400000, gaussian(x) = 0.149727 For x = 1.500000, gaussian(x) = 0.129518 For x = 1.600000, gaussian(x) = 0.110921 For x = 1.700000, gaussian(x) = 0.094049 For x = 1.800000, gaussian(x) = 0.078950 For x = 1.900000, gaussian(x) = 0.065616 For x = 2.000000, gaussian(x) = 0.053991 For x = 2.100000, gaussian(x) = 0.043984 For x = 2.200000, gaussian(x) = 0.035475 For x = 2.300000, gaussian(x) = 0.028327 For x = 2.400000, gaussian(x) = 0.022395 For x = 2.500000, gaussian(x) = 0.017528 For x = 2.600000, gaussian(x) = 0.013583 For x = 2.700000, gaussian(x) = 0.010421 For x = 2.800000, gaussian(x) = 0.007915 For x = 2.900000, gaussian(x) = 0.005953 For x = 3.000000, gaussian(x) = 0.004432 For x = 3.100000, gaussian(x) = 0.003267 For x = 3.200000, gaussian(x) = 0.002384 For x = 3.300000, gaussian(x) = 0.001723 For x = 3.400000, gaussian(x) = 0.001232 For x = 3.500000, gaussian(x) = 0.000873 For x = 3.600000, gaussian(x) = 0.000612 For x = 3.700000, gaussian(x) = 0.000425 For x = 3.800000, gaussian(x) = 0.000292 For x = 3.900000, gaussian(x) = 0.000199 For x = 4.000000, gaussian(x) = 0.000134 For x = 4.100000, gaussian(x) = 0.000089 For x = 4.200000, gaussian(x) = 0.000059 For x = 4.300000, gaussian(x) = 0.000039 For x = 4.400000, gaussian(x) = 0.000025 For x = 4.500000, gaussian(x) = 0.000016 For x = 4.600000, gaussian(x) = 0.000010 For x = 4.700000, gaussian(x) = 0.000006 For x = 4.800000, gaussian(x) = 0.000004 For x = 4.900000, gaussian(x) = 0.000002 For x = 5.000000, gaussian(x) = 0.000001
The following "step" function is known as the Heaviside function and is widely used in mathematics: $$H(x)=\begin{cases}0, & \text{if $x<0$}.\\\\ 1, & \text{if $x\ge 0$}.\end{cases}$$ Write a Python function H(x) that computes H(x).
# Express a step function as a Python function
def step(x):
if(x < 0):
H = 0
else: # Otherwise x must be greater than or equal to zero
H = 1
return H # Return the result
x = 0.5
print(step(x)) # The value that is returned by the function will be printed out here. For the case of x = 0.5, this should print '1'.
1