Example for curl and div of a 2D vector field

Contents

You need to download new m-files.

Download the files vectorfield.m, ezpcolor.m, colorposneg.m

(1) Plot a 2D vector field

Plot the 2D vector field F = ( cos(x+2*y), sin(x-2*y) ) for x,y in [-2,2].

syms x y z real
F = [ cos(x+2*y), sin(x-2*y) ];
vectorfield(F,-2:.25:2,-2:.25:2)

(2) Find G = curl(F) and plot G_3 together with vector field

G = curl([F,0],[x y z])                % need vector field of 3 components for curl

vectorfield(F,-2:.25:2,-2:.25:2); hold on
ezpcolor(G(3),[-2.5 2.5 -2.5 2.5]); hold off
colorbar; colorposneg
title('colors show curl: blue is clockwise, red is counterclockwise')
G =
                             0
                             0
 cos(x - 2*y) + 2*sin(x + 2*y)

(3) Find div(F) and plot it together with vector field

g = divergence(F,[x y])                % find divergence

vectorfield(F,-2:.25:2,-2:.25:2); hold on
ezpcolor(g,[-2.5 2.5 -2.5 2.5]); hold off
colorbar; colorposneg
title('colors show divergence: blue is sink, red is source')
g =
- 2*cos(x - 2*y) - sin(x + 2*y)