CoCalc Public FilesWorkshop-1-CF / Workshop1-Classical_fields.ipynb
Views : 44
Compute Environment: Ubuntu 20.04 (Default)

# Workshop 1

## PHYS2003 Classical Fields

### Week Two

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.

# Scaler Fields

In [ ]:



In lecture $1$, you saw that a scalar field is any function of position which has a scalar value at each point $\bm{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^{-r^2}$ where $F$ is the height above sea level $(F = 0)$, $h$ is the height of the summit and $r=\sqrt{x^2+y^2}$.

1. Geographical maps plot contours corresponding to regular increments in height. For example, on the OS map of the campus area in the lecture notes, you can see that contours of the hill in Wollaton Park are drawn at $45m$, $50m$ and $55m$ above sea level. Contours are closer together where hills are steepest, because it is not necessary to move as far horizontally for a given change in height than it is where ground is less steep.
Write a Python program to plot $F$ as a function of $x$ for $y = 0$ and $h=1$. Using this plot, try to identify where a map would have contours close together and where they would be further apart. Note that for this module, you will not need to worry about relative separations of contours unless explicitly asked to do so.
In [8]:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-2,2,0.01)
y = 0

h = 1
r = np.sqrt(x**2+y**2)
f = h*np.exp(-r**2)

plt.plot(x,f)


[<matplotlib.lines.Line2D at 0x7f50ab1403a0>]
1. Use Python to plot contours of the hill for $h = 1$ (say). Note that you will only evaluate the function at specific points, which are then joined by straight lines. Therefore the contours may not appear to be smooth. Choose sufficient points so that the contours are fairly smooth, but not so many that the matrix sizes are huge. Note that finding a suitable number of points is best determined by trial and error.
Python chooses contours with equal ‘height’ separations, like on a map. Do the shapes of the contours and their relative separations agree with your predictions from parts (a) and (b)?
In [16]:
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

Vector fields are functions of position that have a vector value at each point $\bm{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!

More specifically, quiver(x,y,u,v) will display arrows representing a vector with components $(u, v)$ at points $(x, y)$. $x$ and $y$ can be set up using meshgrid. Note that the Python help calls $u$ and $v$ ‘velocity vectors’, but $u$ and $v$ can represent any vector field. Note also that Python draws arrows such that the base of the arrow is at the point being represented. Some other computer programs place the middle of the arrow at the point represented.
Consider the vector field $\mathbf{F}=exp(-y^2)(\mathbf{i}-\mathbf{j})$
1. By looking at equation of $\mathbf{F}$, determine the directions and qualitatively estimate relative magnitudes of the vector field on a rectangular grid of points. Enter you answer as text in the box below by pressing on edit at the right corner of the box.
In [ ]:



For small values of y, large arrows with bearing 135 degrees and with arrows decreasing in magnitude but maintaining the same direction as y increases.

1. Relating to the notation quiver(x,y,u,v), we can see that we need $u = exp(−y^2)$ and $v = −exp(−y^2)$ for this problem. (Make sure you understand why.) Use the quiver function to plot the vector field. You will need to choose a suitable grid of $x$ and $y$ values. Do the results agree with your prediction? If not, can you see why what you thought was wrong? Ask if you need an explanation.
In [24]:
q=np.arange(-2,2,0.2)
[x,y]=np.meshgrid(q,q)
u=np.exp(-y**2)
v=-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')

Text(0, 0.5, 'y')
1. Python automatically chooses the lengths of the arrows to fit the chosen grid. This can be altered using an optional scale parameter. Re-plot your vector field with the scale factor such that the arrows are half the length of your previous plot.
In [32]:
q=np.arange(-2,2,0.2)
[x,y]=np.meshgrid(q,q)
u=np.exp(-y**2)
v=-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')

Text(0, 0.5, 'y')
1. Experiment with choosing different separations of the grid points for $x$ and $y$.
In [35]:
q=np.arange(-2,2,0.1)
[x,y]=np.meshgrid(q,q)
u=np.exp(-y**2)
v=-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')

Text(0, 0.5, 'y')

# Exercises

Do as much of the following as you have time for.

### First Exercise

• A landscape with two adjacent summits is described by the function
• $G = h\left(\mathrm{e}^{-(x^2 + y^2)} + \mathrm{e}^{-[(x - 2)^2 + y^2]}\right)$
1. Close to the summit of one hill, the contribution to the height of the other hill is negligible. Use Python to plot $G$ for $y=0$ and $h=1$. Can you predict (without doing any calculations) where there will be a saddle point (i.e. a point where the height increases if we move in some directions but decreases if we move in other directions)?
In [ ]:


1. Use Python to plot contours of the landscape represented by equation of $G$. Do the shapes of the contours and the locations of the summits and saddle point agree with your predictions?
In [ ]:


1. Python can label the contours with their contour values. Look up the clabel function from matlibplot.pyplot.axes() and use the information there to label your contours in (b).
In [ ]:



### Second Exercise

• In plane polar coordinates, we can define unit vectors $\hat{\mathbf{r}}$ and $\hat{\bm{\theta}}$ to point in directions of increasing $r$ and $\theta$ respectively. They can be expressed in Cartesian coordinates as

$\mathbf{r} = \frac{x}{r}\mathbf{i} + \frac{y}{r}\mathbf{j},$
and
$\hat{\bm{\theta}} = -\frac{y}{r} \mathbf{i} + \frac{x}{r}\mathbf{j},$
where $r = \sqrt{x^2+y^2}$.
• A vector field representing a particular circular fluid flow is expressed as

$\displaystyle \mathbf{F} = \frac{a}{r} \hat{\bm{\theta}},$
where $a$ is a constant.
1. Verify that the expression for $\mathbf{F}$ in Cartesian coordinates is

$\mathbf{F} = -\frac{ay}{r^2}\mathbf{i} + \frac{ax}{r^2}\mathbf{j},$

and hence identify the appropriate quantities to use for $u$ and $v$ in quiver for this problem.

1. Use Python to plot the vector field on a rectangular grid of points set up using meshgrid. Choose $a=1$. As $\mathbf{F}$ is infinite at the origin, it is best not to include the origin in your grid points. If you do include the origin, some functions will still work but others will give problems. A suitable grid definition is w = np.arange(-1.1, 1.12, 0.2)
Your result should show the circular nature of the flow. Remember (from last year) that if you use set_aspect('equal') the result should be displayed with equal scaling of the $x$ and $y$ axes (i.e. the axis frame should appear as a square rather than a rectangle and the flow as a circle rather than as an ellipse). However, if you have a wide screen monitor and the resolution is set incorrectly, your circle will still look like an ellipse.
In [ ]:


1. Now consider the vector field $\displaystyle\mathbf{F} = \frac{a}{r} \hat{\bm{\theta}} + b \hat{\mathbf{r}}$, where $a$ and $b$ are both constants. As there is a radial component in addition to the circular motion, this represents a spiral flow.
Use Python to plot the vector field for $a=1$ and $b = 2$. Do the results show a spiral flow as expected? What do you expect the field to look like when $b = -2$? Check in Python.
In [ ]:



In this worksheet, you have explored how Python and a pen-and-paper can be used to help visualise scalar and vector fields. Worksheet 1 on Moodle contains additional written and Python questions. If you finish this Workshop with time to spare, you may wish to look at this now.} Alternatively, you could experiment with plotting vector and scalar fields in Wolfram Alpha. Although you will not be formally taught how to do that in this module, you may find it a useful way of quickly visualising fields.

Model answers to workshops will be added to your CoCalc directory and will be available on Moodle after you have had time to attempt the questions yourselves.