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.
U=zeros(Ny,Nx); V=zeros(Ny,Nx);
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;
Now we are ready to make a plot.
xlabel('X'); ylabel('Y');
That looks reasonable.