使用MATLAB

时间:2019-06-08 04:17:30

标签: matlab debugging

我的代码没有为垂直于电荷环的平面正确地绘制正确的轮廓图。 我的问题是等高线图无法填充2D空间

我制作了两个版本的代码,一个使用for循环来计算 Riemann sum和另一个仅使用sum命令。两者都依靠 'subs'命令,用于将网格中的值替换为我的表达式中的V(电势)。

版本1(用于循环):

%% 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); 
% uncessary 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 = 1*10^-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(使用sum命令):

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); % Reimann sum performed here

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

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

此后,版本2的所有其他内容都与版本1相同(用符号x,y,z等代替)

我会发布代码产生的图像,但是显然您需要10声望。谢谢stackoverflow。如果没有这些图片,这将更加令人困惑。

我的代码产生的矢量场是正确的,而轮廓图似乎只在圆环的两端使用了几个点,并将它们与奇怪的菱形形状的直线相连。我无法填补它。

我没有收到任何错误消息。轮廓线以奇怪的菱形形状围绕环的末端(势能接近无穷大)积累,但未在其他任何地方绘制。我需要等高线图来填充2D网格

1 个答案:

答案 0 :(得分:0)

我从MATLAB社区收到了有关此问题的解决方案,并在此处发布了有关该问题的信息:

https://scicomp.stackexchange.com/questions/32834/graphing-electric-potential-of-a-ring-of-charge-using-matlab-help/32842#32842

我会在这里发布,但是这种“您无法发布图像,因为您的信誉不高”会使我的解释过于抽象和难以理解,因此请查看一下您是否遇到了MATLAB等高线图问题,并且想看看我的问题和解决方法