**Graphics****`****PlotField****`**

Anything that assigns a magnitude and direction at each point gives a vector field. Examples include the electromagnetic field and the velocity field of a fluid. Any ordinary differential equation can be used to define a vector field. These vector fields can be visualized by drawing arrows representing the vectors. The direction of the arrow is equal to the direction of the vector field at its base point. The magnitude of the arrow is proportional to the magnitude of the vector field.

This package plots two-dimensional vector fields. For doing vector field plots in three dimensions use the package Graphics`PlotField3D`.

Plotting vector fields in two dimensions.

This loads the package.
In[1]:= **<<Graphics`PlotField`**

The two components of this vector field are given by and

.
In[2]:= **PlotVectorField[{Sin[x], Cos[y]},**

{x, 0, Pi}, {y, 0, Pi}]

Given a scalar-valued function on the plane, there are two common ways of constructing a vector field. The function's gradient vector field has first and second components given by and , respectively. The two components of the function's Hamiltonian vector field are and

. The gradient field gives the direction in which the function is increasing most rapidly. If one treats the function as the Hamiltonian of a mechanical system, the Hamiltonian vector field gives the equations of motion in phase space.

PlotGradientField and PlotHamiltonianField compute formulas for the partial derivatives of the scalar-valued function given as an argument. This means that the functions you give must be such that Mathematica

can compute their derivatives.

Here is the gradient field of the "potential"

.
In[3]:= **PlotGradientField[x^2 + y^2,**

{x, -3, 3}, {y, -3, 3}]

In the plane, the gradient and Hamiltonian vector fields of the same function are always orthogonal.
In[4]:= **PlotHamiltonianField[x^2 + y^2,**

{x, -3, 3}, {y, -3, 3}]

Options for vector field plotting.

This increases the number of sample points in each direction to 20 and draws each vector at half its true magnitude.
In[5]:= **PlotVectorField[{Sin[x y], Cos[x y]},**

{x, 0, Pi}, {y, 0, Pi},

PlotPoints -> 20,

ScaleFunction -> (.5#&),

ScaleFactor -> None]

There are a variety of options to control the display of the vectors. Three of them control aspects of the scaling of the vector magnitudes. They are applied in the order ScaleFunction, MaxArrowLength, and ScaleFactor. The ScaleFunction can be a pure function that takes the magnitude of the vector, and returns the new magnitude for that vector. It can also be None, specifying no rescaling by this option. If its application results in a nonnumeric value, the vector is set to zero magnitude. Next, MaxArrowLength eliminates any vectors that are longer than the specified value after the application of the ScaleFunction. It can also be set to None, so no vectors are removed. Finally, the ScaleFactor is applied. It linearly scales the remaining vectors so that the length of the longest one is equal to the specified value. If ScaleFactor is set to Automatic, then in the field plotting functions the longest vector will be sized to the grid increment, so the head of any given vector will not cover the base of any adjacent vector. If it is set to None, no rescaling due to this option will occur.

Thus, the ScaleFunction option is best used to change the relative magnitudes of the vectors, while the ScaleFactor is best used for linear scaling of the vectors when you know the desired length of the longest vector. If you want to perform linear rescaling of the vector lengths without specifying the longest vector, set ScaleFactor to None, as in the preceding example. MaxArrowLength is best for removing unusual outliers, or filtering out all longer vectors so fine changes in the field can be better observed.

All of the two-dimensional vector plots also accept the same options as the Arrow primitive; all the arrows in the plot will use these options.

A complex-valued function of a complex argument can be viewed as a vector field on the plane by using the real and complex parts of a complex number to give two coordinates. This is essentially the Polya representation of such a function. However, in the Polya representation the magnitude is scaled using ScaleFunction->(Log[#+1]&) and the negative of the imaginary part is used.

Plotting the Polya representation of a complex-valued function.

The complex function

has zeros at the fourth roots of unity, so the magnitude of the vector field is small near these points.
In[6]:= **PlotPolyaField[(x + I y)^4 - 1,**

{x, 0, 3}, {y, 0, 3}]

Vector field plots of lists.

This gives an array of random vectors.
In[7]:= **varray = Table[**

{Random[Real, {-0.7, 0.7}],

Random[Real, {-0.7, 0.7}]},

{i, 10}, {j, 10}];

This displays the array of vectors in a plot.
In[8]:= **ListPlotVectorField[varray]**

This generates a list of vectors as

base,vector

pairs.
In[9]:= **vectors = Table[**

{{Sin[u], Cos[u]}, {u^2 Sin[2u], u^2 Cos[2u]}},

{u, 0, 2 Pi, Pi/16}];

Here are the vectors without any scaling applied.
In[10]:= **ListPlotVectorField[**

vectors,

ScaleFactor -> None,

Frame -> True

]

Here are the vectors with the longest vector scaled to a length of 1, and all other vectors scaled proportionately.
In[11]:= **ListPlotVectorField[**

vectors,

ScaleFactor -> 1,

Frame -> True

]

In this plot, the vectors are all scaled by a function that converts their lengths to a constant.
In[12]:= **ListPlotVectorField[**

vectors,

ScaleFunction->(.4&),

Frame -> True]

Note that ListPlotVectorField treats the option ScaleFactor->Automatic the same as ScaleFactor->None.