$\newenvironment {prompt}{}{} \newcommand {\ungraded }{} \newcommand {\todo }{} \newcommand {\oiint }{{\large \bigcirc }\kern -1.56em\iint } \newcommand {\mooculus }{\textsf {\textbf {MOOC}\textnormal {\textsf {ULUS}}}} \newcommand {\npnoround }{\nprounddigits {-1}} \newcommand {\npnoroundexp }{\nproundexpdigits {-1}} \newcommand {\npunitcommand }{\ensuremath {\mathrm {#1}}} \newcommand {\RR }{\mathbb R} \newcommand {\R }{\mathbb R} \newcommand {\N }{\mathbb N} \newcommand {\Z }{\mathbb Z} \newcommand {\sagemath }{\textsf {SageMath}} \newcommand {\d }{\mathop {}\!d} \newcommand {\l }{\ell } \newcommand {\ddx }{\frac {d}{\d x}} \newcommand {\zeroOverZero }{\ensuremath {\boldsymbol {\tfrac {0}{0}}}} \newcommand {\inftyOverInfty }{\ensuremath {\boldsymbol {\tfrac {\infty }{\infty }}}} \newcommand {\zeroOverInfty }{\ensuremath {\boldsymbol {\tfrac {0}{\infty }}}} \newcommand {\zeroTimesInfty }{\ensuremath {\small \boldsymbol {0\cdot \infty }}} \newcommand {\inftyMinusInfty }{\ensuremath {\small \boldsymbol {\infty -\infty }}} \newcommand {\oneToInfty }{\ensuremath {\boldsymbol {1^\infty }}} \newcommand {\zeroToZero }{\ensuremath {\boldsymbol {0^0}}} \newcommand {\inftyToZero }{\ensuremath {\boldsymbol {\infty ^0}}} \newcommand {\numOverZero }{\ensuremath {\boldsymbol {\tfrac {\#}{0}}}} \newcommand {\dfn }{\textbf } \newcommand {\unit }{\mathop {}\!\mathrm } \newcommand {\eval }{\bigg [ #1 \bigg ]} \newcommand {\seq }{\left ( #1 \right )} \newcommand {\epsilon }{\varepsilon } \newcommand {\phi }{\varphi } \newcommand {\iff }{\Leftrightarrow } \DeclareMathOperator {\arccot }{arccot} \DeclareMathOperator {\arcsec }{arcsec} \DeclareMathOperator {\arccsc }{arccsc} \DeclareMathOperator {\si }{Si} \DeclareMathOperator {\scal }{scal} \DeclareMathOperator {\sign }{sign} \newcommand {\arrowvec }{{\overset {\rightharpoonup }{#1}}} \newcommand {\vec }{{\overset {\boldsymbol {\rightharpoonup }}{\mathbf {#1}}}\hspace {0in}} \newcommand {\point }{\left (#1\right )} \newcommand {\pt }{\mathbf {#1}} \newcommand {\Lim }{\lim _{\point {#1} \to \point {#2}}} \DeclareMathOperator {\proj }{\mathbf {proj}} \newcommand {\veci }{{\boldsymbol {\hat {\imath }}}} \newcommand {\vecj }{{\boldsymbol {\hat {\jmath }}}} \newcommand {\veck }{{\boldsymbol {\hat {k}}}} \newcommand {\vecl }{\vec {\boldsymbol {\l }}} \newcommand {\uvec }{\mathbf {\hat {#1}}} \newcommand {\utan }{\mathbf {\hat {t}}} \newcommand {\unormal }{\mathbf {\hat {n}}} \newcommand {\ubinormal }{\mathbf {\hat {b}}} \newcommand {\dotp }{\bullet } \newcommand {\cross }{\boldsymbol \times } \newcommand {\grad }{\boldsymbol \nabla } \newcommand {\divergence }{\grad \dotp } \newcommand {\curl }{\grad \cross } \newcommand {\lto }{\mathop {\longrightarrow \,}\limits } \newcommand {\bar }{\overline } \newcommand {\surfaceColor }{violet} \newcommand {\surfaceColorTwo }{redyellow} \newcommand {\sliceColor }{greenyellow} \newcommand {\vector }{\left \langle #1\right \rangle } \newcommand {\sectionOutcomes }{} \newcommand {\HyperFirstAtBeginDocument }{\AtBeginDocument }$

### Electric Field due to a Charge Distribution

This is an optional section.

#### Derive Analytical Solution for the Electric field Due to a Loop of Charge

We will first find the electric field due to a loop of charge. The loop of charge is charged with the line charge density $\rho _l$ and is in the X-Y plane, as shown in Figure fig:loopSinglePt. To solve this problem, we first divide the loop into small pieces. The small (blue) arc obtained in this way can be considered a point charge. The electric field due to a point charge is shown in Figure fig:loopSinglePt(the blue arrow). The position of the point charge is defined by a position vector $\vec {r_2}$. The position of point P is defined with the position vector $\vec {r_1}$. The vector $\vec {dE}$ is defined in Equation genfieldloop.

The total electric field at a point P is then equal to the sum of all the fields due to the point charges, as shown in Figure fig:loopAllPts. The equation for the total field is given in Equation eqtotfieldring. Figure 1: Loop of wire uniformly charged with line charge density $\rho _l$. Electric field is shown due to a very small section (arc length) of the loop $dl$. Figure 2: Loop of wire uniformly charged with line charge density $\rho _l$. Electric field is shown due to several very small sections (arc lengths) of the loop $dl$.

The problem now is to represent all the variables in the Equation genfieldloop ( $dQ$, $dl$, $\hat {r}$ and $r$) using appropriate coordinate system and given charge distribution. The total charge on the segment $dl$ is equal to $dQ=\rho _l dl$. As seen in Figure fig:loopAllPts, $dl$ is an arc length in the direction of theta, $dl=a\, d\theta$, where $a$ is the radius of the loop. The vector $\vec {r_2}$ is the position vector of the arc length $dl$, and the vector $\vec {r_1}$ is the position vector of the point be of the electric field calculation. Point P is an arbitrary point in the Cartesian coordinate system, P(x,y,z), therefore its vector is shown in Equation eq1loop. The vector $\vec {r}$ is the distance vector between the point charge (the source) and the point at which we are calculating the electric field.

The vector $\vec {r_2}$ can be written in Polar Coordinates as in Equation pcvec,where $a$ is the radius of the loop. The equation pcvec can be rewritten in Cartesian coordinate system as shown in Equation csloopvec.

The two vectors mark the beginning and the end of the distance vector $\vec {r}$. The vector $\vec {r}$ is the sum of vectors $-\vec {r_2}$ and $\vec {r_1}$.

Or:

Vector $\vec {r}$ has the magnitude of:

Unit vector in the direction of vector $\vec {r}$ is:

Replacing other variables in the Equations eq1loop-vecrloop2 to the Equation genfieldloop, we get the Equation totalfieldloop for the electric field $\vec {dE}$ at a point P.

Components of the electric field are given in Equations totalfieldloop1-totalfieldloop3.

Each component of the field can be integrated separately, as shown in Equations totalfieldloop4-totalfieldloop6.

The integrals in Equations totalfieldloop4-totalfieldloop6 can be integrated analytically in some special cases only. In general, this is in an elliptical integral and cannot be solved analytically. However, this integral can be sold numerically.

#### Derive Numerical solution to the Electric Field due to a Loop of Charge

The integrals in equations totalfieldloop4-totalfieldloop6 can be represented as infinite sums using simple numerical integration as shown in Equations totalfieldloop4n-totalfieldloop6n. Here we see that the continuous function of $\theta$ was replaced with the discrete values of $\theta$.

$\Delta \theta$ represents the length of the interval that the line is divided into, $\theta _i$ represents the value of angle at a certain point, and $i$ designates different points on the loop. It should be noted here that the error due to the trapezoidal rule can be improved by using more advanced numerical integration techniques.

#### Matlab Code to Find the Electric Field due to a Loop of Charge

Equations totalfieldloop4n-totalfieldloop6n can be implemented in Matlab as shown below. Cut-and-paste the program below in Matlab editor. Play with values for the size of the ring and meshgrid values. For some values of the step, the vectors on the graph will completely disappearcomment on why that may be.

clear all
%Specify the extents of x,y,z axes
%Make X,Y,Z matrices
%Define constants epsilon, Q. They are obviously not physical.
%They are set
%to small constants so that we get smaller numbers.
eps=1;
Q=1;
a=1;
const=Q/(2*pi*eps);
%Set the initial electric field components to zero.
Ex=0;
Ey=0;
Ez=0;
%Here we find the fieldl at all X,Y,Z points defined previously
% from each of the "unit" charges on the ring.
%th variable starts from 0.1
%to avoid the infinite value of V at the point r=1,theta=0.
for th=0.1:pi/20:2*pi

t=const./ (sqrt((X-cos(th)).^2 + (Y-sin(th)).^2 + (Z).^2)).^(3);
Ex = Ex+t.*(X-cos(th));
Ey = Ey+t.*(Y-sin(th));
Ez = Ez+t.*Z;
end
%Plot the field components Ex,Ey, Ez at points X,Y,Z.
%Scale the vectors by factor 3.
quiver3(X,Y,Z,Ex,Ey,Ez,3)
hold on;
%Plot the ring of charge where it is positioned in x-y plane.
t = linspace(0,2*pi,1000);
r=1;
x = r*cos(t);
y = r*sin(t);
plot(x,y,’b’);
hold off


### Short line of charge

Derive electric field integral, numerical solution for integral, then implement the code in MATLAB, to find the electric field from a 1 m stick of charge, charged with a uniform line charge density of $\rho _l=1nC/m$. The stick is positioned as in Figure stickf. Figure 3: Stick of wire uniformly charged with line charge density $\rho _l$. Electric field is shown due to a very small section of the line $dl$.

In the simulation below, observe how changing the section of the selected piece of charge (a red square) changes the field at a point P.

Then click on the play button in the lower left corner of the simulation below to watch how each piece of charge contributes to the total electric field.

### Potential

#### Derive Analytical Solution for the Potential Due to a Loop of Charge

Derive the potential due to a ring of charge, charged with a line charge density $\rho _l$.

Potential due to a point charge is given in Equation genpotloop. We will first find the potential due to a loop of charge. Assume that the loop of charge is charged with the line charge density $\rho _l$. The loop of charge is in the X-Y plane, as shown in Figure loopanyptf. First, we will divide the loop into small pieces. We will assume that the small arc obtained in this way can be considered a point charge. The potential due to a point charge at a point P is labeled in Figure loopanyptf as $dV$. The position of the point charge is defined by a position vector $\vec {r_2}$. The position of point P is defined with the position vector $\vec {r_1}$. The electric scalar potential $dV$ is defined in Equation genpotloop. Figure 4: Loop of wire uniformly charged with line charge density $\rho _l$. Electric field is shown due to a very small section (arc length) of the loop $dl$.

The total potential at a point P is then equal to the sum of all the potentials due to the point charges, as shown in Figure loopanypt1. The equation for the total potential is given in eqtotfieldringpot. Figure 5: Loop of wire uniformly charged with line charge density $\rho _l$. Electric field is shown due to several very small sections (arc lengths) of the loop $dl$. Each section is modeled by a point charge $dQ$.

The problem now is to represent all the variables in the Equation genpotloop ( $dQ$, $dl$, $\hat {r}$ and $r$) using appropriate coordinate system and given charge distribution. The total charge on the segment $dl$ is equal to $dQ=\rho _l dl$. As seen in Figure loopanyptf, $dl$ is an arc length in the direction of theta (blue arrow next to $dl$) $dl=a\, d\theta$, where $a$ is the radius of the loop. The vector $\vec {r_2}$ is the position vector of the arc length $dl$, and the vector $\vec {r_1}$ is the position vector of the point be of the electric field calculation. Point P is an arbitrary point in the Cartesian coordinate system, P(x,y,z), therefore its vector is shown in Equation eq1loopp. The vector $\vec {r}$ is the distance vector between the point charge (the source) and the point at which we are calculating the electric field.

The vector $\vec {r_2}$ can be written in Polar Coordinates as in equation pcvecp,where $a$ is the radius of the loop. The Equation pcvecp can be rewritten in Cartesian coordinate system as in Equation csloopvecp.

The two vectors mark the beginning and the end of the distance vector $\vec {r}$. The vector $\vec {r}$ is the sum of vectors $-\vec {r_2}$ and $\vec {r_1}$.

Therefore the vector’s $\vec {r}$ magnitude is shown in Equations vecrloop1p.

Vector $\vec {r}$ has the magnitude of:

Replacing other variables in the Equations eq1loopp-vecrloop1p to the Equation genpotloop, we get the Equation totpot for the potential $dV$ at a point P.

The integral in Equation totalfieldloopp2 can be integrated analytically in some special cases; however, this equation can be solved numerically, as shown in the next section.

#### Derive Numerical solution to the Electric Field due to a Loop of Charge

The integrals in equations totalfieldloopp2 can be represented with infinite sum using simple numerical integration (trapezoidal rule), as shown in Equations totalfieldloop6np. Here we see that the continuous function of $\theta$ was replaced with the discrete values of $\theta$.

Where $\Delta \theta$ represents the length of the interval that the line is divided into, $\theta _i$ represents the value of angle at a certain point, and $i$ designates different points on the loop. It should be noted here that the error due to the trapezoidal rule can be improved by using more advanced numerical integration techniques.

#### Matlab Code to Find the Electric Potential due to a Loop of Charge

Plot the cross-section of the potential and the equipotential lines on the planes x=0, y=0, z=0.

clear all
%Specify the extents of x,y,z axes

%Make X,Y,Z matrices
%Define constants epsilon, Q. They are obviously not physical.
%They are set
%to small constants so that we get smaller numbers.
eps=1
Q=1
const=Q/(2*pi*eps)
%Set the initial potential value to zero. Initialize V.
V=0
%Here we find the potential at all X, Y, Z points defined
% previously from
%each of the "unit" charges on the ring.
%The th variable starts from 0.1
%to avoid the infinite value of V at the point r=1,theta=0.

for th=0.1:pi/20:2*pi
t=const./ sqrt((X-cos(th)).^2 + (Y-sin(th)).^2 + (Z).^2)
V= V+t;
end
%Plot the volume distribution of potential at planes
% x=0, y=0, z=0
slice(X,Y,Z,V,,,)
%Keep the same figure
hold on;
%Plot contours of potential at planes x=0, y=0, z=0. See
%more on contours in appendix.
h=contourslice(X,Y,Z,V,,,)
set(h,’EdgeColor’,’k’,’LineWidth’,1.5)

##### Plot several equipotential surfaces.
 clear all
%Specify the extents of x,y,z axes

%Make X,Y,Z matrices

%Define constants epsilon, Q. They are obviously not physical.
%They are set
%to small constants so that we get smaller numbers.
eps=1
Q=1
const=Q/(2*pi*eps)
%Set the initial potential value to zero. Initialize V.
V=0
%Here we find the potential at all X, Y, Z points defined
%previously from
%each of the "unit" charges on the ring. The th variable
% starts from 0.1
%to avoid the infinite value of V at the point r=1,theta=0.

for th=0.1:pi/20:2*pi
t=const./ sqrt((X-cos(th)).^2 + (Y-sin(th)).^2 + (Z).^2)
V= V+t;
end
%Plot the volume distribution of potential at planes
% x=0, y=0, z=0
p = patch(isosurface(X,Y,Z,V,7));
isonormals(X,Y,Z,V,p)
set(p,’FaceColor’,’red’,’EdgeColor’,’none’);
daspect([1 1 1])
view(3); axis tight
camlight
lighting gouraud


Observe the equipotential surfaces. Explain why the surfaces doughnut are shaped? What is the electric field direction on the surface? Change the isosurface from 7 to 10 or some larger number. How does the isosurface look now? How does the potential of a ring of charge looks at the distances far away from the ring? What about the field?

### Visualizing Scalar Fields in Matlab

To visualize scalar fields in Matlab, we can use the following functions: slice, contourslice, patch, isonormals, camlight, and lightning. Please note that a more detailed explanation about these functions shown here can be found in Matlab’s help.

#### slice

Slice is a command that shows the magnitude of a scalar field on a plane that slices the volume where the potential field is visualized. The format of this command is as shown below.

slice(x,y,z,v,xslice,yslice,zslice)


Where X, Y, and Z are coordinates of points where the scaler function is calculated, V is v the scalar function at those points, and the last three vectors xslice, yslice, and zslice are showing where will the volume will be sliced.

An example of slice command is given below. In the example below, there is an additional command colormap that colors the volume with a specific palette. To see more about different color maps, see Matlab’s help. xslice has three points at which the x-axis will be slice. They are -1.2, .8, 2. This means that the volume will be slice with a plane that is perpendicular to the x-axis, and it crosses the x-axis at points -1.2, .8, and 2.

clc
clear all
[x,y,z] = meshgrid(-2:.2:2,-2:.25:2,-2:.16:2);
v = x.*exp(-x.^2-y.^2-z.^2);
xslice = [-1.2,.8,2]; yslice = 1; zslice = [-2,0];
slice(x,y,z,v,xslice,yslice,zslice)
colormap hsv


#### contourslice

Contourslice command will display equipotential lines on a plane being the volume where the potential field is visualized. An example of a contourslice function is shown below.



[x,y,z] = meshgrid(-2:.2:2,-2:.25:2,-2:.16:2);
v = x.*exp(-x.^2-y.^2-z.^2); % Create volume data
[xi,yi,zi] = sphere; % Plane to contour
contourslice(x,y,z,v,xi,yi,zi)
view(3)


#### patch

Patch command creates a patch of color.

#### isonormals

Command isonormals create equipotential surfaces.

#### camlight

camlight(’headlight’) creates a light at the camera position.

camlight(’right’) creates a light
right and up from camera.

camlight(’left’) creates a light
left and up from camera.camlight with no arguments is the
same as camlight(’right’).

camlight(az,el) creates a light
at the specified azimuth (az) and elevation (el)
with respect to the camera position.

The camera target is the center of rotation,
and az and el are in degrees.



#### lighting

Lighting flat selects flat lighting.

Lighting gouraud selects gouraud lighting.

Lighting phong selects phong lighting.

lighting none turns off lighting.