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 lecture , you saw that a scalar field is any function of position which has a scalar value at each point . 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 by the expression where is the height above sea level , is the height of the summit and .
- 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 , and 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 as a function of for and . 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 (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)?
Vector Fields
Vector fields are functions of position that have a vector value at each point . 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 at points . and can be set up using meshgrid. Note that the Python help calls and ‘velocity vectors’, but and 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 , 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 and 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 and 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.
- Experiment with choosing different separations of the grid points for and .
Exercises
Do as much of the following as you have time for.
First Exercise
- Close to the summit of one hill, the contribution to the height of the other hill is negligible. Use Python to plot for and . 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)?
- Use Python to plot contours of the landscape represented by equation of . Do the shapes of the contours and the locations of the summits and saddle point agree with your predictions?
- 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).
Second Exercise
- Verify that the expression for in Cartesian coordinates is
and hence identify the appropriate quantities to use for and in quiver for this problem.
Answer:
- Use Python to plot the vector field on a rectangular grid of points set up using meshgrid. Choose . As 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 and 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.
- Now consider the vector field , where and 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 and . Do the results show a spiral flow as expected? What do you expect the field to look like when ? Check in Python.
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.