[The gradient of a scalar field is a vector field.]

# Background: Scalar and Vector Fields

From Notes from Physics 200: Computational Physics, Wolfgang Christian, Davidson College.

We study scalar and vector fields in 2D and learn how to solve Laplace's equation using the relaxation method.

## Scalar Field Gradient

The gradient of a scalar field U is a vector field that points in the direction of the greatest rate of change of the scalar field. In Cartesian coordinates, the gradient is computed using the del or nabla operator.

If the scalar field is defined on a grid, we can compute the derivates using the central difference approximation.

>     // compute the gradient using the central difference approximation
>     double h2=2*(_view.scalarField.indexToX(1)-_view.scalarField.indexToX(0));
>     for(int i=1; i<m-1; i++) {                             // loop over grid x
>       for(int j=1; j<m-1; j++) {                           // loop over grid y
>         xGradData[i][j]=(scalarData[i+1][j]-scalarData[i-1][j])/h2;
>         yGradData[i][j]=(scalarData[i][j+1]-scalarData[i][j-1])/h2;
>       } // end of y loop
>     } // end of x loop

The Vector Field Element displays the x-gradient and y-gradient vector components by using small color-coded arrows. Visit the EJS Wiki page for this element for a description of the various properties.

## Related Models

The following EJS models demonstrate how to display two-dimensional scalar and vector fields using elements on the fields and plots tab on the EJS 2D Drawables palette (and can be found in the collection of models for Physics 200: at the Davidson EJS DL->Christian->Phy 200-> Week 9).

- Dipole Field Model
- Scalar Field Gradient Model
- Laplace Equation Model
- Charged Particles Potential Model
- Charged Particles Electric Field Model
- Analytic Vector Field Model

### Credits:

The Scalar Field Gradient Model was created by Wolfgang Christian using the Easy Java Simulations (EJS) version 4.1 authoring and modeling tool. You can examine and modify a compiled EJS model if you run the model (double click on the model's jar file), right-click within a plot, and select "Open Ejs Model" from the pop-up menu. You must, of course, have EJS installed on your computer.

Information about Ejs is available at: <http://www.um.es/fem/Ejs/> and in the OSP comPADRE collection <http://www.compadre.org/OSP/>.