Calculus III. SM 221

1. 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

 

Back to top