Vector Calculus Plots and Identities

Includes divergence and curl examples with vector identities.

You are now following this Submission

Scalar Field and Gradient
In this section we will define a scalar field and then take its gradient symbollicaly. Hence we will see that gradient of a scalar field does not contain any rotational component. Play with phi to see the changes.
Define variables, scalar field and calculate gradient.
syms x y
phi(x,y) = 0.5*x^2 + 2*x*y + 4*x - 1.5*y^2 - y
grad_phi(x,y) = gradient(phi)
Define the interval and the grid points that the function will be evaluated.
interval = -50:1:50;
[X,Y] = meshgrid(interval);
Evaluate function and its gradient at grid points.
phi_XY = double(phi(X,Y));
grad_XY = grad_phi(X,Y);
grad_X = double(grad_XY{1});
grad_Y = double(grad_XY{2});
clear grad_XY;
Plot scalar field with contour.
figure;
contour(X,Y,phi_XY,'ShowText','on');
hold on;
xlabel('x');
ylabel('y');
Plot gradient with quiver.
quiver(X, Y, grad_X, grad_Y);
You may want to zoom into the plot to see field lines.
Curl of Gradient
We first extend 2d representation to 3d to calculate curl operation.
syms z
grad_phiXYZ(x,y,z) = [grad_phi(x,y);sym(0)]
curl_grad_phi = curl(grad_phiXYZ, [x y z])
Vector Field and Divergence
In this section we will define a vector field and then take its divergence symbollicaly. We then plot the contour lines of divergence to see the sources of vector field. In the example you can see that when vector field passes through positive contours the field lines are getting stronger. Play with A to see it for different setups.
Define variables, vector field and calculate its divergence.
clear;
syms x y
A1(x,y) = y+x^2;
A2(x,y) = x^2+y^2 - 1;
A(x,y) = [A1(x,y); A2(x,y)]
clearvars -except A x y
divA(x,y) = divergence(A)
Define the interval and the grid points that the function will be evaluated.
interval = -50:1:50;
[X,Y] = meshgrid(interval);
Evaluate vector field and its divergence at grid points.
div_XY = double(divA(X,Y));
A_XY = A(X,Y);
A_X = double(A_XY{1});
A_Y = double(A_XY{2});
clear A_XY;
Plot vector field with quiver.
figure;
q = quiver(X,Y,A_X,A_Y);
hold on;
q.LineWidth = 2;
xlabel('x');
ylabel('y');
Plot divergence with contour.
contour(X,Y,div_XY, 'ShowText', 'on');
You may want to zoom into the plot to see field lines.
Vector Field and Curl
In this section we will define a vector field and then take its curl symbollicaly. We then plot the quivers of curl and vector fields to see how they behave. In the example you can see that curl is perpendicular to the rotation plane. Play with A to see how curl changes.
Define variables, vector field and calculate its divergence.
clear;
syms x y z
A1(x,y,z) = -y;
A2(x,y,z) = x;
A3(x,y,z) = sym(0);
A(x,y,z) = [A1(x,y,z); A2(x,y,z); A3(x,y,z)]
clearvars -except A x y z
curlA(x,y,z) = curl(A, [x,y,z])
Define the interval and the grid points that the function will be evaluated.
interval = -1:0.1:1;
[X,Y,Z] = meshgrid(interval,interval,interval);
Evaluate vector field and its divergence at grid points.
curl_XYZ = curlA(X,Y,Z);
curl_X = curl_XYZ{1};
curl_Y = curl_XYZ{2};
curl_Z = curl_XYZ{3};
clear curl_XYZ
A_XYZ = A(X,Y,Z);
A_X = A_XYZ{1};
A_Y = A_XYZ{2};
A_Z = A_XYZ{3};
clear A_XYZ
Plot vector field with quiver.
figure;
q = quiver3(X,Y,Z,A_X,A_Y,A_Z);
hold on;
q.LineWidth = 1;
q.ShowArrowHead = "on";
q.AutoScaleFactor = 2;
xlabel('x');
ylabel('y');
zlabel('z');
title('A(x,y,z)')
view([-0.30 90.00]);
Plot curl with quiver.
figure;
q = quiver3(X,Y,Z,curl_X,curl_Y,curl_Z);
hold on;
q.LineWidth = 1;
q.ShowArrowHead = "on";
xlabel('x');
ylabel('y');
zlabel('z');
title('\nabla \times A(x,y,z)');
Divergence of Curl
div_curlXYZ = divergence(curlA)

Tags

Add Tags

Add the first tag.

General Information

MATLAB Release Compatibility

  • Compatible with any release

Platform Compatibility

  • Windows
  • macOS
  • Linux
Version Published Release Notes Action
1.0.0