how to fix code plot field lines between two electric charges

25 views (last 30 days)
How plot all lines start the origins of the charges?
clc; close all; clear all;
xmax = 4;
xmin = -4;
[x, y] = meshgrid(xmin : .08 : xmax);
q1 = 1;
q2 = -1;
z = q1./sqrt((x-1).^2 + y.^2) + q2./sqrt((x + 1).^2 + y.^2);
[u, v] = gradient(-z, .08);
h=streamslice(x, y, u, v, 2);
hold on
set(h,'LineWidth',2);
set(h,'Color','g');
if (q1 > 0) & (q2 < 0)
r = 0.5;
c = [1 0];
pos = [c-r 2*r 2*r];
h = rectangle('Position',pos,'Curvature',[1 1])
set(h,'Facecolor','r','EdgeColor','r');
hold on;
r = 0.5;
c1 = [-1 0];
pos1 = [c1-r 2*r 2*r];
h1 = rectangle('Position',pos1,'Curvature',[1 1])
set(h1,'Facecolor','b','EdgeColor','b');
text(1-0.35,0,'+','Color','k','FontSize',50);
text(-1-0.2,0.2,'-','Color','k','FontSize',50);
end
xlim([xmin xmax]);
ylim([xmin xmax]);

Answers (1)

akshatsood
akshatsood on 7 Sep 2023
Edited: akshatsood on 7 Sep 2023
Hi Nguyen,
I understand that you want to illustrate the phenomenon of electric field line interaction among two electric charges. I investigated your code and observed that some of the field lines were not originating or ending at a charge. To address this issue, I would recommend leveraging quiver function to plot arrows with directional components specified by cartesian coordinates. Have a look at the documentation page here: https://in.mathworks.com/help/matlab/ref/quiver.html?s_tid=doc_ta
I have worked on a script which demonstrates the interaction of electric field lines between two electric charges. Here is the code script for your reference
clc; close all;
q1 = 1;
x1 = -1; % x coordinate of q1
y1 = 0; % y coordinate of q1
q2 = 1;
x2 = 1; % x coordinate of q2
y2 = 0; % y coordinate of q2
[x, y] = meshgrid(-4:0.1:4, -4:0.1:4);
% initialize electric field components
Ex = zeros(size(x));
Ey = zeros(size(y));
% determine electric field at each point on the grid due to q1
r1 = sqrt((x - x1).^2 + (y - y1).^2);
Ex = Ex + q1 * (x - x1) ./ (4 * pi * r1.^3);
Ey = Ey + q1 * (y - y1) ./ (4 * pi * r1.^3);
% determine electric field at each point on the grid due to q2
r2 = sqrt((x - x2).^2 + (y - y2).^2);
Ex = Ex + q2 * (x - x2) ./ (4 * pi * r2.^3);
Ey = Ey + q2 * (y - y2) ./ (4 * pi * r2.^3);
% normalize the electric field vectors for plotting
E_mag = sqrt(Ex.^2 + Ey.^2);
Ex = Ex ./ E_mag;
Ey = Ey ./ E_mag;
% define starting points for field lines
start_x = linspace(-2, 2, 10);
start_y = linspace(-2, 2, 10);
figure;
quiver(x, y, Ex, Ey, 'color', 'g'); % plot electric field vectors
hold on;
radius = 0.1; %radius of both charges
c1 = [-1 0];
pos1 = [c1-radius 2*radius 2*radius];
h1 = rectangle('Position', pos1, 'Curvature', [1 1]);
set(h1,'Facecolor','b','EdgeColor','b');
c2 = [1 0];
pos2 = [c-radius 2*radius 2*radius];
h2 = rectangle('Position',pos2,'Curvature',[1 1]);
set(h2,'Facecolor','r','EdgeColor','r');
% annotating the elctric charges
text(1-0.05,0.05,'-','Color','w','FontSize',20);
text(-1-0.08,0.05,'+','Color','w','FontSize',15);
hold off;
xlim([-2, 2]);
ylim([-2, 2]);
xlabel('X-axis');
ylabel('Y-axis');
title('Electric Field Lines (Unlike Charges)');
grid on;
axis equal;
I have attached the zoomed out view to depict the pattern of electric field lines for the following two cases
The above script outlines a workflow to visualize the distribution of electric field in space in presence of electric charges. You can tweak it using your very own data for the desired outcomes.
I hope this helps.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!