Skip to main content Skip to footer site map
Assoc. Prof. Medynets

Computer Algebra Systems in Calculus

Drawing Vector Fields in MathLab:

Use the command

>> [x,y] = meshgrid(-5 :0.5: 5, -3:1:7);

to create a grid of points in the xy-plane.

Here -5 :0.5: 5 tells us that the x-coordinates of points are -5, -4.5, -4,... 5 and y-coordinates are -3, -2,..., 7. 

The operator -5:0.5:5 returns an array of numbers from -5 to 5 with step 0.5.

Suppose that we would like to graph the vector Field F(x,y) = -y i + x j.  We can use the  "quiver" command.

>> quiver(x,y, -y, x);

The last two arguments are the i and j components of the vector field. 

The code should be

>> [x,y] = meshgrid(-5 :0.5: 5, -3:1:7);

>> quiver(x,y, -y, x);

Radial Vector Field

EXAMPLE

>> [x,y] = meshgrid(-5 :0.5: 5, -5:0.5:5);
>> quiver(x,y, sin(x), cos(y));

Vector Field Sin Cos

EXAMPLE

The following code

 figure
[X,Y] = meshgrid(-2:.2:2);
Z = X.*exp(-X.^2 - Y.^2);
[DX,DY] = gradient(Z);
contour(X,Y,Z)
hold on
quiver(X,Y,DX,DY)
colormap hsv
hold off

 

will draw the gradient vector field and the contour diagrams for the function  z = xexp(-x2 -y2)

Gradietn Vector Field

Direction Fields in Mathematica

Example 1.

Input: f[x_, y_] = (y + E^x)/(x + E^y)

Input: StreamPlot[{1, f[x, y]}, {x, 1, 6}, {y, -20, 5}, Frame -> False,  Axes -> True, AspectRatio -> 1/GoldenRatio]

 Direction Field

Example 2.

Input: f[x_, y_] = (6*x^2)/(2*y + Cos[y])

Input: StreamPlot[{1, f[x, y]}, {x, -3, 3}, {y, -5, 5}, Frame -> False,  Axes -> True]

Direction Field

go to Top