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
where F is the height above sea level (F=0), h is the height of the summit and r=x2+y2.
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.
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)?
<matplotlib.contour.QuadContourSet at 0x7f03373c84f0>
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!
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
By looking at equation of 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.
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
Relating to the notation quiver(x,y,u,v), we can see that we need u=exp(−y2) and v=−exp(−y2) 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.
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.
A landscape with two adjacent summits is described by the function
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 [ ]:
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 [ ]:
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 [ ]:
In plane polar coordinates, we can define unit vectors r^ and θ^ to point in directions of increasing r and θ respectively. They can be expressed in Cartesian coordinates as
A vector field representing a particular circular fluid flow is expressed as
where a is a constant.
Verify that the expression for F in Cartesian coordinates is
and hence identify the appropriate quantities to use for u and v in quiver for this problem.
Use Python to plot the vector field on a rectangular grid of points set up using meshgrid. Choose a=1. As 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 [ ]:
Now consider the vector field F=raθ^+br^, 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.