+
Visualizing Vector Fields and their Derivatives

Developed by Michael Massa and Owen Butler - Published February 18, 2022

One of the challenges met by students as they study electricity and magnetism comes when working with vector derivatives such as gradient, divergence and curl. When visualizing fields and their derivatives, textbooks often provided limited examples, which may skew their perspective on these quantities. This exercise aims to help build students' intuition for these derivatives and mitigate some common misconceptions they may have about them. Students will visualize vector fields and learn simple computational methods to compute the gradient, divergence and curl of a vector field. By the end, students will have a program that allows them create any 2D vector field that they can imagine, and visualize the field, its divergence and curl.
Subject Area Electricity & Magnetism Beyond the First Year Python Students who complete this exercise set should be able to: - Use computational methods for numerical differentiation (Exercise 2) - Use computational methods for obtaining the divergence and curl of a vector field (Exercise 3) - Understand and relate various vector field representations (symbolic expressions, vector field plots, field line plots) (Exercises 1-3) - Discern common vector field and vector derivative misconceptions (Exercises 1, 3) >180 min

These exercises are not tied to a specific programming language. Example implementations are provided under the Code tab, but the Exercises can be implemented in whatever platform you wish to use (e.g., Excel, Python, MATLAB, etc.).

## Exercise 1: Pre-assignment quiz (20-30 min) A pre-quiz is provided which focuses on properties of the divergence and curl of vector fields. - Part A: Students are first asked to identify general properties of divergence and curl (select from 9 statements on each vector derivative). - Part B: Different representations of 2D vector fields (symbolic, vector field plots and field line plots) are given. Students are asked to identify whether each of the eight fields given has non-zero divergence and/or curl. The quiz can also be used as a post-exercise assessment for learning gains. The pre-quiz can be found in the Additional Resources section. ## Exercise 2: Computing the gradient of a scalar field (~1 hr tutorial) Numerical derivatives and the gradient of a scalar field Here are two simple approaches to computing the derivative of a function that is presented as an array of discrete points. Newton's difference (forward difference): $$f'(r_i)\approx\frac{f(r_{i+1})-f(r_i)}{h}$$ Central difference: $$f'(r_i)\approx\frac{f(r_{i+1})-f(r_{i-1})}{2h}$$ - Create a function, *deriv*, that takes two 1D arrays as input, $f$ and $r$, and computes the derivative $df/dr$ using a simple numerical method (Newton's difference or central difference). This can be tested on simple known functions (e.g. trig, polynomial). - Next, read in the file, "scalarField.csv" as a 2D array, and use the function above to compute the gradient of the scalar field in cartesian coordinates.$$\vec{\nabla} V=\frac{\partial V}{\partial x}\hat{i}+\frac{\partial V}{\partial y}\hat{j}$$ - The function *deriv* can be used to compute x (y) component of the gradient, taking horizontal (vertical) slices across the 2D scalar field. - **Note:** Pay attention to the syntax of 2D array indices in your code, which may order as [row, column], in contrast to how we general express position coordinates as [x, y]. - Plot both the field and its gradient. Where does the numerical derivative do a good job? A poor job? What are the strengths and weaknesses of different visualizations (contour, field line, vector plot)? ## Exercise 3: Computing the divergence and curl of vector fields In Cartesian coordinates, the divergence and curl of a 2D vector field $\vec{V}$ are: $$\vec{\nabla} \cdot \vec{V}=\frac{\partial V_x}{\partial x}+\frac{\partial V_y}{\partial y} \quad \text{and} \quad \vec{\nabla} \times \vec{V}=\left(\frac{\partial V_y}{\partial x}-\frac{\partial V_x}{\partial y}\right)\hat{k}$$ Use the same approach from Exercise 2 to compute the divergence and curl - the application is similar to that of the gradient; however, consideration should be given as to how derivatives of 1D slices of the field $\vec{V}$ are taken, corresponding to the different terms in the vector derivatives. The vector field itself now consists of two 2D arrays, one each for the x- and y-components. - Choose four of the vector fields to that were presented in the Exercise 1 pre-quiz. 2D arrays for the $V_x$ and $V_y$ components of the fields can be obtained in two ways: - arrays can be uploaded from prepared csv-files (provided in accompanying materials), or - arrays can be produced by writing a function that creates these arrays from symbolic expressions (a sample function has been provided in the code template, for the vector field shown in the thumbnail at the beginning of the Exercise). - Visualize the vector fields and their divergence and curl. Sample output is shown below for the vector field depicted at the top of this Exercise. Consider the choice of graphing style that best presents the results, bearing in mind that some fields may have non-zero $\vec{\nabla} \cdot \vec{V}$, $\vec{\nabla} \times \vec{V}$ or both! - **Note:** Although the curl of a field is a vector quantity, in 2D it can be represented as a scalar, since the only non-zero component points in the $\hat{k}$ direction. The use of positive/negative scalar values embodies the direction of this component. - Questions: - How do the computed divergence and curl for each field compare with your pre-quiz results? - Of the field representations in the pre-quiz (symbolic, vector field plots and field line plots), did any of these give you the more insight or mislead you when it came to identifying fields with divergence and curl? - Do you feel that computing fields and their derivatives supports/enhances your understanding of electrostatics? ![Sample vector field (upper) plotted at quiver and streamplots and (lower) divergence and curl of this field](images/vecderivs/plotDivCurl.png "")