使用 MATLAB 帮助绘制电荷环的电势图

计算科学 matlab 电磁学 绘图
2021-12-13 23:57:10

这是我正在尝试做的事情的摘要:

由于均匀的电荷环,使用 MATLAB 计算空间中任意点使用黎曼和来计算增量的积分,作为您可以更改的变量。在垂直于环区域并通过中心的平面上绘制你的势和场V(x,y,z)N

我有两个版本的代码给我相同的结果:正确的表达式()和正确的向量场。我的问题是让我的等高线图填充 2D 空间。两个代码之间的唯一区别是一个使用 for 循环来执行求和,另一个使用 sum 命令:VVtot

版本 1(使用 for 循环):

%% Computing a symbolic expression for V for anywhere in space

syms x y z % phiprime is angle that an elemental dq of the circular charge is located at, x,y and z are arbitrary points in space outside the charge distribution

N = 200; % number of increments to sum
R = 2; % radius of circle is 2 meters
dphi = 2*pi/N; % discretizing the circular line of charge which spans 2pi

integrand = 0;
for phiprime = 0:dphi:2*pi

  % phiprime ranges from 0 to 2pi in increments of dphi

  integrand = integrand + dphi./(sqrt(((x - R.*cos(phiprime) )).^2 + ((y - R.*sin(phiprime) ).^2) + z.^2));

end

intgrl = sum(integrand);
% unnecessary but harmless step that I leave to show that I am using the
sum of the above expression for each dphi

eps0 = 8.854e-12;
kC = 1/(4*pi*eps0);
rhol = 1e-9; % linear charge density

Vtot = kC*rhol*R.*intgrl; % symbolic expression for Vtot

%% Graphing V & E in plane perpedicular to ring & passing through center

[Y1, Z1] = meshgrid(-4:.5:4, -4:.5:4);
Vcont1 = subs(Vtot, [x,y,z], {0,Y1,Z1}); % Vcont1 stands for V contour; 1 is because I do the plane of the ring next

contour(Y1,Z1,Vcont1)
xlabel('y - axis [m]')
ylabel('z - axis [m]')
title('V in a plane perpendicular to a ring of charge (N = 200)')
str = {'Red line is side view', 'of ring of charge'};
text(-1,2,str)

hold on
% visually displaying line of charge on plot
circle = rectangle('Position',[-2 0 4 .1],'Curvature',[1,1]);
set(circle,'FaceColor',[1, 0, 0],'EdgeColor',[1, 0, 0]);

% taking negative gradient of V and finding symbolic equations for Ex, Ey and Ez
g = gradient(-1.*(kC*rhol*R.*intgrl),[x,y,z]);

% now substituting all the values of the 2D coordinate system for the symbolic x and y variables to get numeric values for Ex and Ey
Ey1 = subs(g(2), [x y z], {0,Y1,Z1});
Ez1 = subs(g(3), [x y z], {0,Y1,Z1});

E1 = sqrt(Ey1.^2 + Ez1.^2); % full numeric magnitude of E in y-z plane

Eynorm1 = Ey1./E1; % This normalizes the electric field lines
Eznorm1 = Ez1./E1;

quiver(Y1,Z1,Eynorm1,Eznorm1);
hold off

版本 2(使用总和):

syms x y z
R = 2; % radius of circle is 2 meters
N=100;
dphi = 2*pi/N; % discretizing the circular line of charge which spans 2pi

phiprime = 0:dphi:2*pi; %phiprime ranges from 0 to 2pi in increments of dphi

integrand = dphi./(sqrt(((x - R.*cos(phiprime) )).^2 + ((y - R.*sin(phiprime) ).^2) + z.^2));

phiprime = 0:dphi:2*pi;
intgrl = sum(integrand); % Riemann sum performed here

eps0 = 8.854e-12;
kC = 1/(4*pi*eps0);
rhol = 1e-9; % linear charge density

Vtot = kC*rhol*R.*intgrl; % symbolic expression for Vtot

%%
[Y1, Z1] = meshgrid(-4:.5:4,-4:.5:4);
Vcont1 = subs(Vtot, [x,y,z], {0,Y1,Z1});

contour(Y1,Z1,Vcont1)
xlabel('y - axis [m]')
ylabel('z - axis [m]')
title('V in a plane perpedicular to a ring of charge (N = 100)')
str = {'Red line is side view', 'of ring of charge'};
text(-1,2,str)

hold on
circle = rectangle('Position',[-2 0 4 .1],'Curvature',[1,1]); % visually displaying ring of charge on plot
set(circle,'FaceColor',[1, 0, 0],'EdgeColor',[1, 0, 0]);

g = gradient(-1.*(kC*rhol*R.*intgrl),[x,y,z]); % taking negative gradient of V and finding symbolic equations for Ex, Ey and Ez


% substituting all the values of the 2D coordinate system for the symbolic x and y variables to get numeric values for Ex and Ey because the gradient command doesn't accept symbolic arguments
Ey1 = subs(g(2), [x y z], {0,Y1,Z1});
Ez1 = subs(g(3), [x y z], {0,Y1,Z1});


E1 = sqrt(Ey1.^2 + Ez1.^2); % full numeric magnitude of E in y-z plane


Eynorm1 = Ey1./E1; % This normalizes the electric field lines
Eznorm1 = Ez1./E1;

quiver(Y1,Z1,Eynorm1,Eznorm1);
hold off

两种版本的代码都会生成以下图表:

等高线图

注意:本文上方的图片应该像下图一样有轴,而不是另外,本文下方图片的标题应为“ in the plane...”而不是yzxyEV

电场图

正如你所看到的,向量场是正确的,而等高线图似乎只使用了环末端周围的几个点,并用直线将它们连接成一个奇怪的菱形。我不能让它填满空间。

至于我对公式的推导,它在这里: VV的推导

  • s是径向距离
  • ϕ是方位角
  • 任意点没有上标,而素数符号用于标识电荷环上的点(等)P(x,y,z)ϕx
  • 草书表示从环上的一点到的相对距离rP(x,y,z)
  • R是圆环的半径,我选择了2米
  • ds=Rdϕ是弧长的一小部分
2个回答

您不需要符号变量来计算黎曼和的近似势。您可以只使用meshgrid来评估每个兴趣点的潜力。对于电场,您可以只分析计算导数,然后对每个分量重复该过程,或者使用 计算数值梯度gradient

有更好的方法来近似积分,而不仅仅是假设您有具有均匀电荷分布的直线段,但它们在概念上与此过程相似。

以下代码段在不使用符号变量的情况下计算您想要的内容。

%% Parameters
R = 2.0;
k = 1.0;
charge_density = 1.0;
ndiv_ring = 100;
ndiv_x = 50;
ndiv_y = 100;
ndiv_z = 100;

%% Computation
phi = linspace(0, 2*pi, ndiv_ring);
dphi = 2*pi/(ndiv_ring - 1);
x_ring = R*cos(phi);
y_ring = R*sin(phi);
x = linspace(0, 4, ndiv_x);
y = linspace(-4, 4, ndiv_y);
z = linspace(-4, 4, ndiv_z);
[X, Y, Z] = meshgrid(x, y, z);
V = zeros(size(X));
for cont = 1:ndiv_ring
    Rx = X - R*cos(phi(cont));
    Ry = Y - R*sin(phi(cont));
    Rmag = sqrt(Rx.^2 + Ry.^2 + Z.^2);
    V = V + dphi/Rmag;
end
V = k*R*charge_density*V;

%% Compute the gradient
hx = x(2) - x(1);
hy = y(2) - y(1);
hz = z(2) - z(1);
[Ex, Ey, Ez] = gradient(-V, hx, hy, hz);

%% Visualization
levels = 5;
isovalues = linspace(2, 9, levels);
for cont = 1:levels
    [faces, verts, colors] = isosurface(X, Y, Z, V, isovalues(cont), V);
    p = patch('Vertices', verts, 'Faces', faces, 'FaceVertexCData',...
              colors, 'FaceColor','interp','EdgeColor','interp');
    isonormals(X, Y, Z, V, p)
    p.EdgeColor = 'none';
    p.FaceAlpha = 0.4;
end
daspect([1 1 1])
view(3); 
axis vis3d
camlight 
lighting gouraud

可视化部分生成等效于 3D 数据的等值线的等值面。生成的图像应如下所示。

在此处输入图像描述

我从 MATLAB 社区收到了这个问题的解决方案。

本质上,我需要使用“contour()”命令上的“levels”点来指定要绘制的等高线。

级别不仅允许您选择要绘制的线条数量,还允许您选择要绘制的线条如果您定义一个向量,例如

vlevel = linspace(20, 65, 10);

然后将其放置在轮廓的“水平”位置:

contour(Y1,Z1,Vcont1,vlevel)

现在,contour 命令将仅绘制从第 20 行开始到第 65 行结束的 10 条等距线(您可以根据需要选择任意参数)。

我的等高线图现在看起来像: 在此处输入图像描述

这真是太漂亮了。我应该补充一点,我增加了情节以显示第 20 和第 60 之间的 15 行,因此我的图像并不完全代表我的示例。

此处链接到我们的完整讨论: https ://www.mathworks.com/matlabcentral/answers/465471-help-with-creating-contour-plot?s_tid=prof_contriblnk