我正在尝试计算均匀充电的球形壳的电势和电场,并使用MATLAB在壳外部的空间中绘制结果。我已经尝试了从for循环到命令求和的所有操作,甚至我什至无法获得双积分的正确表达式(因为我可以计算出我的结果给出的结果与解析解不匹配)。
根据我的研究,最好的绘制方式是在同一张图上绘制不同等值的等值面图,以显示电势和电场如何随距球体的距离而变化。但这是我的第二个问题。我什至无法正确计算双积分。所以,我的问题是双重的,
1)如何使用包含作为常数的符号变量的黎曼和来计算双积分?我故意避免使用双积分命令(例如integral2()或Riemann命令,例如trapz()
)2)考虑到我将具有三个变量的函数,如何在3维上绘制结果表达式?
我的代码的当前状态以计算电势(我先定义该状态,然后再使用梯度获取电场),如下所示:
syms r phi theta % symbolic values representing a random point in space
% outside a uniformly charged spherical shell
% constants
N = 3; % total number of increments to divide the riemann sum into
Q = +20 .* 1e-9; % total charge on sphere
R = 1; % radius of sphere is 1 meter
sigma = Q/(4*pi*(R^2)); % uniform surface charge denisty
eps0 = 8.854e-12;
kC = 1/(4*pi*eps0); % coulomb's constant
dphi = 2*pi/N; % discretizing the interval over which I sum
dtheta = pi/N;
% Performing two riemann sums
% dA = R^2.*cos(thetaprime).*dphi.*dtheta; small area on surface of
% sphere in spherical coordinates
phiprime = linspace(0,2*pi,N);
thetaprime = linspace(0,pi,N);
dInt1 = sym(zeros(0,N)); % preallocating space allowing the presence of
symbols
dInt2 = sym(zeros(0,N));
for e = 1: length(phiprime)
for m = 1: length(thetaprime)
dInt1(m) = dInt1 +
((R^2).*cos(thetaprime).*dphi.*dtheta)./sqrt((r.*sin(theta).*cos(phi) -
R.*sin(thetaprime).*cos(phiprime)).^2 + (r.*sin(theta).*sin(phi) -
R.*sin(thetaprime).*sin(phiprime)).^2 + (r.*cos(theta) -
R.*cos(phiprime)).^2)
end
dInt2(e) = dInt2 + sum(dInt1);
end
Int = sum(Int2);
Vtot = kC*sigma.*Int; % total symbolic expression for electric potential
%% Computing Values for Vtot at various points in space and plotting them
rinterval = linspace(1,4,12);
phiinterval = linspace (0,2*pi,12);
thetainterval = linspace(0,pi,12);
[X,Y,Z] = meshgrid(rinterval,phiinterval,thetainterval);
Vcont = subs(Vtot, [r,phi,theta], {X,Y,Z}); % substituting 2D coordinates
now for symbolic values
对于此代码,我得到了错误:
Error using symengine
Array sizes must match.
Error in sym/privBinaryOp (line 1022)
Csym = mupadmex(op,args{1}.s, args{2}.s, varargin{:});
Error in + (line 7)
X = privBinaryOp(A, B, 'symobj::zipWithImplicitExpansion',
'_plus');
Error in Numerical_Integration_of_2D_Surfaces (line 27)
dInt1(m) = dInt1 +
((R^2).*cos(thetaprime).*dphi.*dtheta)./sqrt((r.*sin(theta).*cos(phi) -
R.*sin(thetaprime).*cos(phiprime)).^2 + (r.*sin(theta).*sin(phi) -
R.*sin(thetaprime).*sin(phiprime)).^2 + (r.*cos(theta) -
R.*cos(phiprime)).^2)