0% found this document useful (0 votes)
2 views4 pages

2D Vector and Gradient Field Visualizations

The document provides MATLAB code for visualizing 2D vector fields, gradients, curls, and line integrals. It includes generating vector fields, calculating gradients, and visualizing curl magnitudes, along with computing line integrals over parameterized paths. Each section contains code for plotting and illustrating the mathematical concepts involved.

Uploaded by

24d137
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

2D Vector and Gradient Field Visualizations

The document provides MATLAB code for visualizing 2D vector fields, gradients, curls, and line integrals. It includes generating vector fields, calculating gradients, and visualizing curl magnitudes, along with computing line integrals over parameterized paths. Each section contains code for plotting and illustrating the mathematical concepts involved.

Uploaded by

24d137
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Vector

% 2D Vector Field Visualization

[x, y] = meshgrid(-2:0.2:2, -2:0.2:2);

u = -y;

v = x;

figure;

quiver(x, y, u, v, 'b');

axis equal;

xlabel('x');

ylabel('y');

title('\bfVector Field: F(x, y) = <-y, x>');

grid on;

grad

% --- Gradient Visualization ---

[x, y] = meshgrid(-2:0.2:2, -2:0.2:2);

f = x.^2 + y.^2; % Scalar field

[fx, fy] = gradient(f, 0.2, 0.2); % Compute numerical gradient

figure;

contour(x, y, f, 20, 'LineWidth', 1.5); hold on;

quiver(x, y, fx, fy, 'r');

title('\bfGradient Field: \nabla f(x,y) = <2x, 2y>');

xlabel('x'); ylabel('y');

axis equal; grid on;


legend('Contours of f', 'Gradient vectors');

curl f

% --- Corrected Curl Visualization ---

clc; clear; close all;

% Define grid

[x, y] = meshgrid(-2:0.2:2, -2:0.2:2);

% Define vector field F(x, y) = <-y, x>

u = -y;

v = x;

% Compute partial derivatives

[uy, ux] = gradient(u, 0.2, 0.2); % ∂u/∂y, ∂u/∂x

[vy, vx] = gradient(v, 0.2, 0.2); % ∂v/∂y, ∂v/∂x

% Compute curl (z-component)

curlz = vx - uy;

% Plot results

figure;

contourf(x, y, curlz, 20, 'LineColor', 'none'); % Color map of curl

hold on;

quiver(x, y, u, v, 'k'); % Vector field arrows


colorbar;

title('\bfCurl Visualization: F(x, y) = <-y, x>');

xlabel('x');

ylabel('y');

axis equal tight;

grid on;

legend('Curl magnitude (z-component)', 'Vector field');

line integral

clc; clear; close all;

% --- Define vector field ---

F = @(x, y) [y; x]; % F(x, y) = <y, x>

% --- Parameterized path (quarter circle) ---

r = @(t) [2*cos(t); 2*sin(t)];

drdt = @(t) [-2*sin(t); 2*cos(t)];

t = linspace(0, pi/2, 200);

% --- Evaluate along the curve ---

x = 2*cos(t);

y = 2*sin(t);

Fx = y; % F_x = y

Fy = x; % F_y = x
% --- Line integral computation ---

dotProd = Fx .* (-2*sin(t)) + Fy .* (2*cos(t));

lineIntegral = trapz(t, dotProd);

fprintf('Value of the line integral ∫C F·dr = %.4f\n', lineIntegral);

% --- Plot vector field ---

[xg, yg] = meshgrid(-2:0.2:2, -2:0.2:2);

ug = yg;

vg = xg;

figure;

quiver(xg, yg, ug, vg, 1.2, 'b'); hold on; % ↑ scaling factor increased to 1.2

% --- Plot path ---

plot(x, y, 'r', 'LineWidth', 2);

% --- Overlay F along the path ---

quiver(x, y, Fx, Fy, 0.4, 'm', 'LineWidth', 1.2); % arrows along curve

axis equal;

xlabel('x'); ylabel('y');

title('\bfVector Line Integral Visualization');

legend('Vector field F(x,y)', 'Path C', 'F along C', 'Location', 'best');

grid on;

You might also like