ECE LAB #43 Solution



The purpose of LAB 43 is to allow you to use MATLAB to work through a set of problems based on electric fields. These exercises will make use of your work in previous labs, as well as enabling you to consider how to use MATLAB to solve a design problem based around a resistive sensor.

PREPARATION – Individual

  1. Read through these Lab #34 notes carefully, and make sure you understand what each part of the lab is asking you to do. This prior review will save you a lot of time during the lab session, which will enable you to complete the lab in the time available.

  1. The first part of this lab involves using MATLAB to calculate and plot the electric field intensity, E, and the absolute electric scalar potential, V, due to a charged spherical shell, as shown to the right.

Use Coulomb’s law to develop the integrals that you would have to evaluate to calculate the three components of E and the value of V at a general point P(x,y,z) due to this charged shell. You do not have to solve these integrals. Assume that the shell has a radius a, and non-uniform charge density, ρS. Write your handwritten solution in the box below and make sure to show all your work.

  1. With our work from part 2, you can now adapt your ring_of_charge function from Lab #23 to calculate the components of E and the value of V at any point P(x,y,z) for the case of an infinitely-thin spherical shell charged with the non-uniform charge density ρS. Since this is a surface integration it can be approximately calculated by two nested for loops. Using this function as a foundation, create a new function sphere_of_charge that carries out these calculations and write it out in the box on the next page below (this must be written by hand). Your new function must be based on the integrations that you would need to evaluate if you solved this problem using Coulomb’s law. Remember, each integration requires a summation, and so you will have to be careful how you do the two summations required for the charged sphere calculation. Hint: To implement the double integral shown below one option is to use the for loop structure given:

  • !

= ! sin

! !

N = 500;

dtheta = pi/N;

dphi = 2*pi/N;

phi = linspace(dphi,2*pi,N);

theta = linspace(0,pi,N+1);

for e=1:length(phi)

for m=1:length(theta)






Observe that the result of this integration provides the surface area of a sphere of radius . Notice how the phi and theta vectors must be defined carefully. For the 1 × vector phi, it must start at dphi since if it started at zero, it would count that point twice due to the fact that phi = 2 specifies the same position as phi = 0. For the 1 × ( + 1) vector theta, it must have + 1 points so that the spacing between the subsequent entries of theta correspond to dtheta, i.e., theta(2) – theta(1) = dtheta. You can verify that the spacing between the subsequent entries of phi correspond to dphi, as expected.

You must ensure your function works before you come to the lab. To do so, compare the results from your function to those determined from a theoretical analysis (i.e., Gauss’s law), for a sphere of radius = 1 cm and uniformly charged with ρs = −5 nC/m2:

Point P(x, y, z)

E – Theory (in terms of Ex, Ey, and Ez)

E – Matlab Function (in terms of Ex, Ey, and Ez)

(0, 0, 0) cm

(0, 0, 2) cm

(−1, 2, −1) cm

(No need to provide theoretical

values for this point)

University of Toronto


ECE Department


1. Electric-Field of a Spherical Shell

1.1. Uniformly Charged Spherical Shell

For this section of the lab consider a uniformly charged spherical shell in free space ( = !), centered about the origin with radius a = 0.5 m and a charge density of ρs = 2 µC/m2.

Write a new function that will make use of your sphere_of_charge function to calculate and plot the electric field magnitude and the absolute electric scalar potential for this shell along the y – axis from y = −3 m to y = 3 m. You should have around 100 points for your y−vector, and use an N = 200 for each integration.

For these two figures, plot the theoretical values of Etot and V using a series of red o’s (i.e., ‘ro’) and compare this to the calculated values for the field and the potential both inside and outside the shell using a blue solid line (i.e., ‘b-’). Recall that these theoretical values are given by:


| | <


| | <







| | ≥





| | ≥


!| |

Since !!!”#$ and !!!”#$ are given by two different expressions depending on the value of , you must create these vectors carefully. One way to do this is to assign the appropriate values to your !!!”#$ and !!!”#$ vectors using an if statement as you cycle through the -vector in the for loop of your plotting function.

Note: By using Coulomb’s law to calculate the fields inside and outside this spherical shell, we have made no assumptions about the symmetry of the field. Therefore, your results should prove that the field inside a uniformly charged spherical shell, which is fully closed, is zero!

1.2. Non-Uniformly Charged Spherical Shell

Is the electric field always zero within a charged spherical shell? To find this out, consider the example of a spherical shell in free space ( = !), centered about the origin with radius a = 0.5 m and a non-uniform charge density !. Each group will have a different charge density, so ask your TA to tell you what the charge density function is for your group.

For this non-uniformly charged shell, create a 2D plot of the variation of Ex, Ey, Ez, Etot and V along the z-axis, over the range −3 m ≤ ≤ 3 m.

Is the field as you would expect it to be? To answer this you could consider this shell as a collection of stacked rings which have different charge densities (according to how ! changes) and different radii.

Make sure you discuss this section of the lab with your TA before you leave.

University of Toronto 4 ECE Department