$\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 }$

### Magnetic Field due to a Charge Distribution

We will first find the magnetic field due to a loop carrying current $I$, positioned in the X-Y plane, as shown in Figure fig:magfield. To solve this problem, we will first divide the loop into small pieces and label one of these pieces as $dl$. The magnetic field due to an infinitesimal current, can be found using Biot-Savart’s law. Magnetic field is labeled in Figure fig:magfield as $dB$. The infinitesimal current position is defined by a position vector $\vec {r_2}$. The position of point P, where the field will be calculated, is defined with the position vector $\vec {r_1}$. The distance between the current and the observation point is labeled as $\vec {r}$. The vector $\vec {dB}$ is defined in Equation bscur. Figure 1: Loop of wire carrying current $I$. Magnetic field is shown due to a very small section (arc length) of the loop $dl$.

The total magnetic field at a point P is then equal to the sum of all the fields due to the elemental currents, as shown in Figure bscur1. The equation for the total field is given in eqtotfieldringm. Figure 2: Loop of wire uniformly charged with line charge density $\rho _l$. Electric field is shown due to several very small sections (arc length) 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 bscur ( $I$, $dl$, $\hat {r}$ and $r$) using appropriate coordinate system and given current distribution. As seen in Figure fig:magfield, $\vec {dl}$ is an arc length in the direction of theta (blue arrow next to $dl$) $dl=a\, d\theta a_{\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 P where we want the find the magnetic field. Point P is an arbitrary point in the Cartesian coordinate system, P(x,y,z), therefore its vector is shown in Equationeq1loopm. The vector $\vec {r}$ is the distance vector between the elemental current (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 pcvecm,where $a$ is the radius of the loop. The equation pcvecm can be rewritten in Cartesian coordinate system as in Equation csloopvecm.

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 and the unit vector are shown in Equations vecrloop1m-vecrloop2m.

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

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

Cross product between the distance vector $\vec {r}$ and the vector of the direction of current $\hat {I}$ is found in Equations eq:cprloop-eq:product.

Replacing other variables in the Equations eq1loopm-vecrloop2m, we get the Equation eq:totalfieldloop1m for the magnetic field $\vec {dB}$ at a point P.

Components of the magnetic field are given in Equations eq:totalfieldloop1m-eq:totalfieldloop3m.

Each field component can be integrated separately, as shown in Equations totalfieldloop4m-totalfieldloop6m.

The magnetic field above is shown in Figure mflmat Figure 3: Magnetic Field of a Loop of Current

### 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 can be found in Matlab 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 scalar 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 a slice command is given below. There is an additional command colormap that colors the volume with a specific palette in the example below. To see more about different color maps, see Matlab help. xslice has three points at which the x-axis will be slice. They are -1.2, .8, 2. The volume will be sliced with a plane 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 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 creates 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.