% Constants
epsilon0 = 8.854187817e-12; % Vacuum permittivity (F/m)
q = 1e-9; % Charge magnitude (C)
d = 0.1; % Distance between charges (m)
r_min = 0.01; % Minimum distance for field and potential
calculation (m)
r_max = 0.5; % Maximum distance for field and potential
calculation (m)
num_points = 10; % Number of points for field and potential
calculation
% Create a grid of points for field and potential calculation
x = linspace(-r_max, r_max, num_points);
y = linspace(-r_max, r_max, num_points);
[X, Y] = meshgrid(x, y);
% Calculate electric field components Ex and Ey
r1 = sqrt((X - d/2).^2 + Y.^2);
r2 = sqrt((X + d/2).^2 + Y.^2);
Ex = (q / (4 * pi * epsilon0)) * ((X - d/2) ./ r1.^3 + (X + d/2) ./ r2.^3);
Ey = (q / (4 * pi * epsilon0)) * (Y ./ r1.^3 + Y ./ r2.^3);
% Calculate electric potential V
V1 = (q ./ (4 * pi * epsilon0 * r1));
V2 = (q ./ (4 * pi * epsilon0 * r2));
V = V1 - V2; % Note the subtraction here
% Plot the electric field pattern using quiver arrows
figure;
subplot(2, 1, 1);
quiver(X, Y, Ex, Ey);
xlabel('X');
ylabel('Y');
title('Electric Field Pattern of an Electric Dipole');
axis equal;
grid on;
% Plot the electric potential using contour plots
subplot(2, 1, 2);
contour(X, Y, V, 20); % Adjust the number of contour levels as needed
xlabel('X');
ylabel('Y');
title('Electric Potential of an Electric Dipole');
axis equal;
grid on;
colormap('jet'); % You can change the colormap to your preference
colorbar;
% Adjust the figure layout
set(gcf, 'Position', [100, 100, 1000, 400]);
1
Published with MATLAB® R2023a