how does matlab simulate electric field

75 views (last 30 days)
how does matlab simulate electric field?
i mean the method that the matlab codes tend to take ( what is the process? )

Accepted Answer

William Rose
William Rose on 2 Nov 2022
Edited: William Rose on 2 Nov 2022
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.

More Answers (1)

Sam Chak
Sam Chak on 2 Nov 2022
Edited: Sam Chak on 2 Nov 2022
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
QUIVER Quiver plot. QUIVER(X,Y,U,V) plots velocity vectors as arrows with components (u,v) at the points (x,y). The matrices X,Y,U,V must all be the same size and contain corresponding position and velocity components (X and Y can also be vectors to specify a uniform grid). QUIVER automatically scales the arrows to fit within the grid. QUIVER(U,V) plots velocity vectors at equally spaced points in the x-y plane. QUIVER(U,V,S) or QUIVER(X,Y,U,V,S) automatically scales the arrows to fit within the grid and then stretches them by S. Use S=0 or S='off' to plot the arrows without the automatic scaling. QUIVER(...,LINESPEC) uses the plot linestyle specified for the velocity vectors. Any marker in LINESPEC is drawn at the base instead of an arrow on the tip. Use a marker of '.' to specify no marker at all. See PLOT for other possibilities. QUIVER(...,'filled') fills any markers specified. QUIVER(AX,...) plots into AX instead of GCA. H = QUIVER(...) returns a quivergroup handle. Example: [x,y] = meshgrid(-2:.2:2,-1:.15:1); z = x .* exp(-x.^2 - y.^2); [px,py] = gradient(z,.2,.15); contour(x,y,z), hold on quiver(x,y,px,py), hold off, axis image See also FEATHER, QUIVER3, PLOT. Documentation for quiver doc quiver

Tags

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!