Plotting a Vector Field in Mathematica
Example 1
All of the commands are to be executed in Mathematica.
Here are some basic things to keep in mind:
1. Files in Mathematica are called notebooks.
2. The blue brackets to the right of the notebook denote objects called cells. Cells are the
objects in which all commands are executed.
3. To execute a command, simply type it in, then press Shift+Enter.
4. Mathematica has an extensive help system that contains not only the syntax of each
command but also examples on how to use it. There is also the Mathematica Book that is
included with the help system.
Since Mathematica does not load all of its vast functionality when it is started (for purposes of efficiency), we must load the package that contains the PlotVectorField function. Below is the basic syntax for creating a vector field. We first load the package, then we use the PlotVectorField command.
Please note the syntax for loading the graphics package, PlotField. The << is two less than signs and the ` ` symbols are backwards quotes usually found directly above the Tab key. There are no spaces in this command.
For more information on the PlotVectorField command, double click on the command to select it, then press the F1 key or go to the Help menu and select Find Selected Function...
![[Graphics:Images/index_gr_1.gif]](Images/index_gr_1.gif)
![[Graphics:Images/index_gr_2.gif]](Images/index_gr_2.gif)
![[Graphics:Images/index_gr_3.gif]](Images/index_gr_3.gif)
Here is the PlotVectorField command with an added option that puts a frame around the vector field. Notice that we have named the plot, p1. We will reference this plot later, so naming saves us from having to recreate it.
![[Graphics:Images/index_gr_4.gif]](Images/index_gr_4.gif)
![[Graphics:Images/index_gr_5.gif]](Images/index_gr_5.gif)
If the vector field does not appear correctly, try using the FrameSize and AspectRatio options. Here is an example:
![[Graphics:Images/index_gr_6.gif]](Images/index_gr_6.gif)
![[Graphics:Images/index_gr_7.gif]](Images/index_gr_7.gif)
Play with different image sizes and aspect ratios until you find some that you like.
Now we will solve the initial value problem and plot its solution curve.
![[Graphics:Images/index_gr_8.gif]](Images/index_gr_8.gif)
![[Graphics:Images/index_gr_9.gif]](Images/index_gr_9.gif)
![[Graphics:Images/index_gr_10.gif]](Images/index_gr_10.gif)
To make the plot stand out, try using the PlotStyle option as follows.
![[Graphics:Images/index_gr_11.gif]](Images/index_gr_11.gif)
![[Graphics:Images/index_gr_12.gif]](Images/index_gr_12.gif)
Hue[1] yields a red curve
Hue[0.7] yields a blue curve
Hue[0.3] yields a green curve
Try other numbers between 0.1 and 1. After 1, the colors repeat; 1.7 is blue, 1.3 is green, ...
Now we will overlay the plots. Here is where we can see the benefits of naming the previous plots.
![[Graphics:Images/index_gr_13.gif]](Images/index_gr_13.gif)
![[Graphics:Images/index_gr_14.gif]](Images/index_gr_14.gif)
If the plots do not fit over each other, try the following:
1. Make sure that the range and the domains are the same in both the Plot command and the
PlotVectorField command. You may have to use the PlotRange option in the Plot com-
mand. The syntax is PlotRange->{ymin, ymax}.
2. You may need to set the ImageSize and AspectRatio for each command. Make them
equal in each case. ImageSize->360 and AspectRatio->0.66 are good settings to try.
However, you may like others so play with the numbers.
For all of the available options, use
![[Graphics:Images/index_gr_15.gif]](Images/index_gr_15.gif)
Example 2
Here is an example where we put all the commands into one cell. Notice that in p4 we combine the plots of the lines:
. We surround the lines with {} to create a list.
![[Graphics:Images/index_gr_17.gif]](Images/index_gr_17.gif)
![[Graphics:Images/index_gr_18.gif]](Images/index_gr_18.gif)
![[Graphics:Images/index_gr_19.gif]](Images/index_gr_19.gif)
![[Graphics:Images/index_gr_20.gif]](Images/index_gr_20.gif)
Now it is apparent where the equilibrium values lie and which is an attractor and which is a repellor.