In this module, you will work a lot with scalar and vector fields. We will expect you to be able to use pen and paper to do tasks such as sketching vector fields and contours of scalar fields. However, it is also useful to be able to use a computer to do these things. In this workshop, you will explore how fields can be plotted in Python.
If you don't have your 1st year Python book with you, then you may need to refer to the online notes from the first year computing web pages to remind yourself of the basic syntax.
In lecture 1, you saw that a scalar field is any function of position which has a scalar value at each point r=(x,y,z). We usually visualize scalar fields by plotting contours over which the scalar function is a constant. You should be familiar with contour lines on maps, so we will consider geographical contours as our examples.
A particular hill is described mathematically in plane polar co-ordinates (r,θ) by the expression
F=he−r2
where F is the height above sea level (F=0), h is the height of the summit and r=x2+y2.
import numpy as np import matplotlib.pyplot as plt x = np.arange(-2, 2, 0.1) y = 0 h = 1 r = (x**2 + y**2)**0.5 F = h*(np.exp(-(r**2))) plt.plot(x, F)
import numpy as np import matplotlib.pyplot as plt x = np.arange(-2, 2, 0.1) y = np.arange(-2, 2, 0.1) [x1, y1] = np.meshgrid(x,y) h = 1 r = np.sqrt(x1**2 + y1**2) F = h*np.exp(-r**2) plt.contour(x1, y1, F)
Vector fields are functions of position that have a vector value at each point r. We saw in the guided-learning booklet that vector fields are represented by arrows, as with wind velocities on weather maps. The field at a point is represented by an arrow, such that the magnitude is indicated by the length of the arrow and the direction is indicated by the direction of the arrow. Python can easily plot vector fields using the functions quiver in 2D and quiver3 in 3D. Why is it called quiver? Easy - in archery, a quiver is a container for arrows!
Answer: The arrows will be pointing at an angle of 135 degrees and the magnitude of the arrows will be largest when the value of y is small and tend to 0 for larger values
q = np.arange(-2, 2, 0.2) [x, y] = np.meshgrid(q,q) u = np.exp(-(y**2)) v = -1*np.exp(-(y**2)) plt.figure(3, figsize=(4,3)) ax3 = plt.axes([-2, -2, 2, 2]) ax3.quiver(x, y, u, v) plt.xlabel('x') plt.ylabel('y')
q = np.arange(-2, 2, 0.2) [x, y] = np.meshgrid(q,q) u = np.exp(-(y**2)) v = -1*np.exp(-(y**2)) plt.figure(3, figsize=(4,3)) ax3 = plt.axes([-2, -2, 2, 2]) ax3.quiver(x, y, u, v, scale=40) plt.xlabel('x') plt.ylabel('y')
q = np.arange(-2, 2, 0.2) [x, y] = np.meshgrid(q,q) u = np.exp(-(y**2)) v = -1*np.exp(-(y**2)) plt.figure(3, figsize=(4,3)) ax3 = plt.axes([-2, -2, 2, 2]) ax3.quiver(x, y, u, v, angles='xy', scale_units='xy', scale=10) plt.xlabel('x') plt.ylabel('y')
Do as much of the following as you have time for.
Answer: