Calculation of Maxwell’s Equations Using MATLAB
name Arwa Mohammad Hammad
index 184004
THE EQUATION : ∇ × E = − ∂B/∂t
THE CODE :
x = linspace(-5,5,100);
y = linspace(-5,5,100);
z = linspace(-5,5,100);
[X, Y, Z] = meshgrid(x, y, z);
Ex = sin(2*pi/5*Z);
Ey = 0*X;
Ez = 0*X;
[Bx, By, Bz, V] = curl(X, Y, Z, Ex, Ey, Ez);
Eplot = 0*x;
Bplot = 0*x;
for i=1:100 %% Integration-like procedure
Eplot(i) = mean(mean(Ex(:,:,i),1),2);
Bplot(i) = mean(mean(By(:,:,i),1),2);
end
plot3(0*x, y, Eplot, 'b', 'LineWidth', 2); hold on
h = quiver3(0*x(1:3:100), y(1:3:100), 0*z(1:3:100), 0*x(1:3:100), 0*y(1:3:100), Eplot(1:3:100), 0, 'b',
'LineWidth', 1);
set (h, "maxheadsize", 0.0);
plot3(Bplot, y, 0*z, 'g', 'LineWidth', 2);
h = quiver3(0*x(1:3:100), y(1:3:100), 0*z(1:3:100), Bplot(1:3:100), 0*y(1:3:100), 0*z(1:3:100), 0, 'g',
'LineWidth', 1);
set (h, "maxheadsize", 0.0);
grid on, axis square
THE EQUATION : ∮B⋅dl=μ0Ien
THE CODE :
% Constants
mu0 = pi * 4E-7; % Permeability of free space
I = 1; % Current in the wire (A)
% Define radius range
radius = linspace(1E-3, 1); % From 1 mm to 1 m
% Magnetic field calculation using Ampère's Law
AmpereB = @(mu0, I, r) (mu0 .* I) ./ (2 * pi * r);
% Plotting
figure(1);
semilogy(radius, AmpereB(mu0, I, radius), 'LineWidth', 2); % Plot magnetic field
grid on; % Enable grid
% Add title and labels
title('Magnetic Field Around a Current-Carrying Wire ');
xlabel('Distance from Wire, r (m)');
ylabel('Magnetic Field, B (T)');
% Customize grid colors
set(gca, 'XColor', [0.8 0.2 0.6], 'YColor', [0.2 0.4 0.8], 'GridColor', [0.5 0.5 0.5]); %
Pink and blue grid
set(gca, 'FontSize', 12, 'LineWidth', 1.5); % Improve font size and line width
Faraday Law Example Code .m:
The code :
% Parameters
J = 500; % Number of spatial grid points
N = 20; % Number of time steps
pi = 4 * atan(1); % Define pi
f = 1; % Frequency of the wave
omega = 2 * pi * f; % Angular frequency
k = 2 * pi; % Wave number
c = omega / k; % Wave speed
lambda = c / f; % Wavelength
a = 3 * lambda; % Length of the spatial domain
dx = a / J; % Spatial step size
dt = dx / c; % Time step size
x = [0:dx:a]; % Spatial grid
% Set axis limits
axis([0, a, -2, 2]);
% Initial conditions
t = 0; % Initial time
u0 = sin(omega * t - k * x); % Initial displacement
v0 = omega * cos(omega * t - k * x); % Initial velocity
% Plot initial displacement
figure;
plot(x, u0, 'LineWidth', 2); % Plot initial wave
hold on;
title('Wave Propagation Simulation');
xlabel('Position (x)');
ylabel('Displacement (u)');
grid on;
% First time step
t = dt; % Update time
u1 = u0 + dt * v0; % First time step displacement
plot(x, u1, 'LineWidth', 2); % Plot displacement at t=dt
% Iterative update
for n = 1:N
t = t + dt; % Update time
u2 = zeros(1, J + 1); % Initialize new displacement
for j = 2:J
u2(j) = u1(j + 1) + u1(j - 1) - u0(j); % Finite difference update
end
% Periodic boundary conditions
u2(1) = u1(2) + u1(J) - u0(1); % Left boundary
u2(J + 1) = u2(1); % Right boundary
plot(x, u2, 'LineWidth', 2); % Plot displacement at current time
u0 = u1; % Update previous displacement
u1 = u2; % Update current displacement
end
% Add legend
legend('Initial Wave', 'First Time Step', 'Wave Propagation');
Curl of the magnetic field :
% Define grid
[x, y] = meshgrid(-2:0.2:2, -2:0.2:2); % 2D grid
% Define magnetic field components (Bx, By)
Bx = -y; % Bx = -y
By = x; % By = x
% Compute curl of the magnetic field
curlB = curl(x, y, Bx, By);
% Plot the magnetic field
figure;
quiver(x, y, Bx, By, 'b'); % Magnetic field vectors
hold on;
contour(x, y, curlB, 20, 'LineWidth', 2); % Contour plot of curl(B)
title('Curl of the Magnetic Field');
xlabel('x');
ylabel('y');
colorbar;
grid on;
axis equal;