如何使用Contourf在Matlab中模拟和绘制电场? [英] How to simulate and draw electric fields in Matlab with contourf?
问题描述
我想知道如何针对特定点(例如(4,5))以等高线f绘制带有两个电荷Q和-Q以及它们的总E的图形. 我的M文件实际上扫描该区域(从-3x到3x)并计算每个点的E,并将其存储在表格中,但是此后,我不知道如何使用Contourf绘制它..
I'd like to know how to draw a graph with 2 electric charges Q and -Q and their total E, for a specific point (eg (4,5)), with contour f.. My M-file actually scans the area (from -3x to 3x) and calculates E for every spot, stores it in a table, but after this, I don't know how to use contourf to draw it..
syms i
syms j
syms d
d=input('dwse thn timi tou d,ths apostashs')
j=0
i=0
for j=-d:d/1000:d
j=j+1
for i=-d:d/1000:d
i=i+1
z=(i,j)
end, end
推荐答案
基于库仑定律,距离为r的单个离散电荷q产生的电场由下式给出:
Based on Coulomb's law, the electric field created by a single, discrete charge q at a distance r is given by:
E = q/(4 * pi * e0 * r.^ 2);
E=q/(4*pi*e0*r.^2);
如果您有多个费用,则可以使用叠加原理并添加每个费用的贡献.
If you have several charges you can use the superposition principle and add the contribution of each charge.
剩下要做的就是创建一个网格来计算电场.为此,您可以使用Matlab函数meshgrid
.
The only thing left to do is to create a grid to compute the electrical field. For this you can use tha Matlab function meshgrid
.
在Matlab中一个简单的例子是:
A simple example in Matlab would be:
k=1/4/pi/8.854e-12;
d=2;
q=[-1 1];
x=[-d/2 d/2];
y=[0 0];
dx=0.01;
X=(-d:dx:d);
Y=(-d:dx:d);
[X Y]=meshgrid(X,Y);
E=zeros(size(X));
for i=1:numel(q)
r=sqrt((X-x(i)).^2+(Y-y(i)).^2);
E=E+k*q(i)./r.^2;
end
E(isinf(E))=NaN;
figure;
contourf(X,Y,E);
axis image;
希望它对您有帮助.您可以阅读contourf
的文档以根据需要调整图.
Hope it helps you. You can read the documentation of contourf
to tweak the plot to your needs.
这篇关于如何使用Contourf在Matlab中模拟和绘制电场?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!