how does matlab simulate electric field
조회 수: 134 (최근 30일)
이전 댓글 표시
how does matlab simulate electric field?
i mean the method that the matlab codes tend to take ( what is the process? )
댓글 수: 0
채택된 답변
William Rose
2022년 11월 2일
편집: William Rose
2022년 11월 2일
Matlab does not simulate the electric field. The user does, by writing code that implements the standard equations of electrostatics or electrodynamics. Matlab' quiver() and quiver3() functions frpvide a good way to visualize results in 2D or 3D respectively. To use quiver(), you need 4 matrices: X, Y = cooridnates of the points where the E field is computed; U, V = x and y components of the E-field at those points. Then the user can call quiver(X,Y,U,V), to make a plot of arrows showing the E field at the various points.
The standard equations of electrostatics are often presented in a way that does not specify the x and y components of the E field. The Matlab user does that, by using the appropriate sine and cosine functions when calculating the U and V matrices.
Simple example: Compute and plot the E-field around a unit point charge located at the origin. The plot range should be +-4 in the x direciton, and +-3 in the y direction.
First make the X and Y matrices, for the x,y locations where the field will be computed:
[X,Y]=meshgrid(-4:4, -3:3);
Next, use the equations for the E field:
where is a unit vector pointing from the charge to the point (x,y)
You can show with a bit of algebra and vector analysis that this is equivalent to
for a charge at the origin. And likewise, .
Therefore we can write the following code. There are ways to write the code below so that it does not use nested for loops, but that is a topic for another day.
[Ny,Nx]=size(X); %size of the arrays
U=zeros(Ny,Nx); V=zeros(Ny,Nx); %allocate arrays U,V
k=1; %one could use k=1/(4*pi*e0)
q=1; %charge
for i=1:Ny
for j=1:Nx
if X(i,j)==0 & Y(i,j)==0
U(i,j)=0; V(j,j)=0; %the field=+Inf here
else
U(i,j)=k*q*X(i,j)/(X(i,j)^2+Y(i,j)^2)^1.5;
V(i,j)=k*q*Y(i,j)/(X(i,j)^2+Y(i,j)^2)^1.5;
end
end
end
Now we are ready to make a plot.
quiver(X,Y,U,V)
xlabel('X'); ylabel('Y');
That looks reasonable.
댓글 수: 0
추가 답변 (1개)
Sam Chak
2022년 11월 2일
편집: Sam Chak
2022년 11월 2일
Hi @abdullah
Typically, the quiver() function can be used.
You can also find a very good example on the simulation of Electric Field here. The original code is provided by @Kutlu Yigitturk and modified by @William Rose.
help quiver
참고 항목
카테고리
Help Center 및 File Exchange에서 Thermal Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!