lab3-extra¶
Additional tasks that exercise our current skill set. swing_time()
relates to the harmonic oscillator, and geometric_mean()
introduces a different way of computing the mean of two numbers.
swing_time(L)
¶
Write a function swing_time(L)
that computes and returns the time
T
[in seconds] needed for an idealized pendulum of length L
[in
meters] to complete a single oscillation, using the equation
Example:
In [ ]: swing_time(1)
Out[ ]: 2.0060666807106475
geometric_mean(xs)
¶
Implement a function geometric_mean(xs)
that computes the geometric
mean of the numbers given in the list xs
. The geometric mean of
\(n\) numbers \(x_1, x_2, \ldots, x_n\) is given by
\(\left(\prod_{i=1}^n x_i\right)^{\frac{1}{n}} = \sqrt[n]{x_1 \cdot x_2 \cdot \ldots \cdot x_n}\).
Example:
In [ ]: geometric_mean([1, 2])
Out[ ]: 1.4142135623730951
Hint: Remember that a**b
computes \(a^b\) (i.e. takes
a-to-the-bth-power). To compute the n-th root of a number x
, you can
write x**(1/n)
in Python:
In [ ]: 16**(1/2) # square root
Out[ ]: 4.0
In [ ]: 16**(1/4) # quartic root
Out[ ]: 2.0
In [ ]: 2**4
Out[ ]: 16
Please include the extra tasks in your file lab3.py
and submit as Computing lab3 assignment.
Back to lab3.
End of lab3-extra.