Let $A$ be the two-dimensional array $$ \mathbf{A} = \left\lbrack\begin{array}{ccc} 0 & 12 & -1\cr -1 & -1 & -1\cr 11 & 5 & 5 \end{array}\right\rbrack $$ Implement and apply the function
$$ f(x) = x^3 + xe^x + 1 $$to each element in $A$. Then calculate the result of the array expression $A^3 + Ae^A + 1$, and demonstrate that the end result of the two methods are the same.
from math import exp
from numpy import *
def f(x):
return x**3 + x*exp(x) + 1
A = array([[0, 12, -1],
[-1, -1, -1],
[11, 5, 5]])
for i in range(0, len(A)):
for j in range(0, len(A)):
print "A[%d][%d] = %d; f(A[%d][%d]) = %.2f" % (i, j, A[i][j], i, j, f(A[i][j]))
print "Result from vectorised function:", f(A)
A[0][0] = 0; f(A[0][0]) = 1.00 A[0][1] = 12; f(A[0][1]) = 1954786.50 A[0][2] = -1; f(A[0][2]) = -0.37 A[1][0] = -1; f(A[1][0]) = -0.37 A[1][1] = -1; f(A[1][1]) = -0.37 A[1][2] = -1; f(A[1][2]) = -0.37 A[2][0] = 11; f(A[2][0]) = 659947.56 A[2][1] = 5; f(A[2][1]) = 868.07 A[2][2] = 5; f(A[2][2]) = 868.07 Result from vectorised function: [[ 1.00000000e+00 1.95478650e+06 -3.67879441e-01] [ -3.67879441e-01 -3.67879441e-01 -3.67879441e-01] [ 6.59947559e+05 8.68065796e+02 8.68065796e+02]]
The viscosity of water, $\mu$, varies with the temperature $T$ (in Kelvin) according to
$$ \mu = A\times 10^{B/(T-C)} $$where $A$ = $2.414 \times 10^{−5}$ Pa s, $B$ = 247.8 K, and $C$ = 140 K. Plot $\mu(T)$ for $T$ between 0 and 100 degrees Celsius. Label the $x$ axis with "temperature (C)" and the $y$ axis with "viscosity (Pa s)". Note that $T$ in the formula for $\mu$ must be in Kelvin.
%pylab inline
from numpy import *
from pylab import *
def viscosity(T):
A = 2.414e-5 # Pa s
B = 247.8 # K
C = 140.0 # K
return A*10**(B/(T - C))
def C2K(C):
return C + 273.15
N = 10 # Number of points
T_celsius = linspace(0, 100, N)
T_kelvin = C2K(T_celsius) # Convert to degrees Kelvin
v = viscosity(T_kelvin)
plot(T_celsius, v)
xlabel("Temperature (C)")
ylabel("Viscosity (Pa s)")
show()
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline]. For more information, type 'help(pylab)'.
The wave speed $c$ of water surface waves depends on the length $\lambda$ of the waves. The following formula relates $c$ to $\lambda$:
$$ c(\lambda) = \sqrt{\frac{g\lambda}{2\pi}\left(1 + s\frac{4\pi^2}{\rho g \lambda^2}\right)\tanh\left(\frac{2\pi h}{\lambda}\right)} $$where $g$ is the acceleration of gravity, $s$ is the air-water surface tension ($7.9 \times 10^{-2}$ N/m), $\rho$ is the density of water (can be taken as 1000 kg/m$^3$ ), and $h$ is the water depth. Let us fix $h$ at 50 m. First make a plot of $c(\lambda)$ for small $\lambda$ (1 mm to 10 cm). Then make a plot $c(\lambda)$ for larger $\lambda$ (1 m to 2 km).
%pylab inline
from pylab import *
g = 9.8 # m/s**2
s = 7.9e-2 # N/m
density = 1e3 # kg/m**3
h = 50.0 # m
def c(l):
a = (g*l)/(2*pi)
b = 1.0 + s*(4*pi**2)/(density*g*l**2)
c = tanh(2*pi*h/l)
return sqrt(a*b*c)
small_l_array = linspace(1e-3, 0.1, 100) # Small l (1 mm to 10 cm)
large_l_array = linspace(1.0, 2e3, 100) # Large l (1 m to 2 km)
plot(small_l_array, c(small_l_array))
xlabel("Wave length $\lambda$")
ylabel("Wave speed c($\lambda$)")
#plot(large_l_array, c(large_l_array))
show()
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline]. For more information, type 'help(pylab)'.
Suppose we have measured the oscillation period $T$ of a simple pendulum with a mass $m$ at the end of a massless rod of length $L$. We have varied $L$ and recorded the corresponding $T$ value. The measurements are found in a file data/pendulum.dat, containing two columns. The first column contains $L$ values and the second column has the corresponding $T$ values. Load the $L$ and $T$ values into two arrays. Plot $L$ versus $T$ using circles for the data points.
%pylab inline
from pylab import *
f = open("data/pendulum.dat", "r")
L = []
T = []
f.readline() # Skip the first line (because it's a header line)
for line in f:
(l, t) = line.split()
L.append(float(l))
T.append(float(t))
f.close()
L = array(L)
T = array(T)
plot(L, T)
xlabel("Length (L)")
ylabel("Oscillation period (T)")
show()
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline]. For more information, type 'help(pylab)'.
To elongate a spring a distance x, one needs to pull the spring with a force $kx$. The parameter $k$ is known as the spring constant. The corresponding potential energy in the spring is $\frac{1}{2}kx$. Make a class for springs. Let the constructor __init__ store $k$ as a class attribute, and implement the methods force(x) and energy(x) for evaluating the force and the potential energy, respectively.
class Spring:
def __init__(self, k):
self.k = k
def force(self, x):
return self.k*x
def energy(self, x):
return 0.5*self.k*x
s = Spring(4900)
print s.force(0.5)
2450.0