Numerical Integration
5.7 MATLAB’s dblquad Function
---Figure 5.5 shows the resulting quiver plot.
5.7 MATLAB’s dblquad Function
The MATLAB function for numerically evaluating a double integral is dblquad.
A description of the function can be obtained by typing help dblquad in the Command window:
Q = DBLQUAD(FUN,XMIN,XMAX,YMIN,YMAX) evaluates the double integral of FUN(X,Y) over the rectangle XMIN <= X <= XMAX, YMIN <= Y <=
YMAX. FUN is a function handle. The function Z = FUN(X,Y) should accept a vector X and a scalar Y and return a vector Z of values of the integrand.
-0.015 -0.01 -0.005 0 0.005 0.01 0.015
-0.015 -0.01 -0.005 0 0.005 0.01 0.015
Electric field due to a line charge along the Z axis
x axis
z axis
Figure 5.5 Vector plot of the electric field in the x-z plane due to a line charge along the z axis.
Non-square regions can be handled by setting the integrand to zero outside of the region of interest. For example, the volume of a hemisphere of radius R can be determined by the dblquad function by setting -R <= x <= R and 0 <= y <= R and setting z = 0 for points (x, y) that lie outside the circle of radius R around the origin.
The usage of dblquad is similar to quad, except
◾ FUN must be a function of two variables (instead of one): a vector of values in the x direction and a single value in the y direction.
◾ There must be two sets of integration limits (one for the x direction, and one for the y direction).
Example 5.9
Calculate the surface area and volume of a hemisphere with radius of 1 m.
To find the surface area, we define a differential area element
= φ θ φ
dA R2sin d d (as shown in Figure 5.6) and then double integrate over the intervals φ =[0, 2]π and θ = [0,2π] using dblquad. To find the volume,
θ
φ
R
R sinφ dθ
dφ
x
y z
dA = (R sinφ dθ)(Rdφ)
R
Figure 5.6 To find the surface area of a hemisphere, we define a differential surface element dA and then do a double integration over dϕ and dθ.
we define a differential volume element dV= R2−x2−y dxdy2 (as shown in Figure 5.7) and double integrate over the intervals x = [−R,R] and y = [−R,R].
% Example_5_9.m
% This program calculates the surface area and volume of
% a hemisphere (with radius=1) using dblquad. The exact
% values from formulae are also calculated.
clear; clc;
global R;
R = 1;
% calculate surface area
SA = dblquad('hemisphere_dA',0,pi/2,0,2*pi);
SA_exact = 2*pi*R^2;
% calculate volume
V = dblquad('hemisphere_dV',-R,R,-R,R);
V_exact = 2/3*pi*R^3;
% print results
fprintf('Surface area SA of a hemisphere of ');
fprintf('radius %.4f m\n',R);
fprintf('SA by DBLQUAD = %.4f m^2\n',SA);
fprintf('SA exact = %.4f m^2\n',SA_exact);
fprintf('Volume V of a hemisphere of radius %.4f m\n',R);
R
differential volume element
dy dx
x
y z
R2 – x2 – y2
R2 – x2 – y2
dV = dxdy
Figure 5.7 To find the volume of a hemisphere, we define a differential volume element dV and then do a double integration over dx and dy.
fprintf('V by DBLQUAD = %.4f m^3\n',V);
SA exact = 6.2832 m^2
Volume V of a hemisphere of radius 1.0000 m V by DBLQUAD = 2.0944 m^3
V exact = 2.0944 m^3
Exercises
Exercise.5.2 field in the x-y plane (instead of the x-z plane) for the interval −50 ≤ x ≤ 50 mm and
−50 ≤ y ≤50 mm with a step size of 10 mm. Remember not to calculate the field at the location of the line charge.
Project.5.2
We now modify the line charge of Example 5.8 to be of a finite length ℓ = 0.02 m along the z axis, starting at z = −0.01 m and ending at z = +0.01 m. Assume
λ = ×2 10−9 coul/m.
1. Calculate and plot (using quiver) the magnitude of the electric field for the interval −0.05 ≤ x ≤ 0.05 m and −0.05 ≤ z ≤ 0.05 m with a step size of 0.01 m.
2. Print to the screen every fourth value of x0, z0 , and the corresponding values of Ex, Ez , and the magnitude of
E.
3. Now, assume that the total charge of the line segment Q= λ = 0.04 10× −9 coul is concentrated as a point charge at the origin. Calculate the magnitude of the electric field for this point charge over the same interval as previously using Equation (5.8). Note that because Q is a point charge, you can use Equation (5.8) directly, and no integration is required.
4. Calculate the percentage error in using the point charge method in the values of Ex and Ez at each point as calculated in parts 1 and 3. Use the following formula for calculating the percentage error in Ex and a similar formula for calculating the percentage error in Ez.
=
Ex,1 = Ex calculated in part 1.
Ex,2 = Ex calculated in part 3.
Print to the screen every fourth value of xo, zo, and the corresponding values of the percentage error in the values of Ex and Ez.
5. At what distance from the origin is the percentage error in using the point charge method less than 5%? In many cases, if we are far enough away from the charge, we can avoid integrating over its geometry and instead just treat it as a point charge, thereby making our calculations much easier. This is called the far-field approximation.
Project.5.3
A positive surface charge density of magnitude σ = ×4 10−12coul/m2 extends infi-nitely in the x-y plane as shown in Figure P5.3a. Using a method similar to what was described in Section 5.4 (show your work), define a differential charge element dQ = σ dy dx and then find the resulting electric field by integrating over the sheet of charge for xp= −∞ ∞[ , ], yp= −∞ ∞[ , ], and zp=0:
1. Write a MATLAB program using the dblquad function to calculate Ex, Ey, and Ez at the following points:
xo, yo, zo = [0, 0, 2 × 10–3] (above the plate) x y zo, ,o o=[ 30 10 , 5 10 , 12 10 ]× −3 − × −3 × −3 (below the plate)
Assume that 10 is a good enough approximation for infinity. Note that by symmetry, the x and y components of the field should integrate to zero (or close to it). Confirm this result.
2. Now, assume that there are two sheet charges in the planes parallel to the x-y plane: the first sheet located at zp= +1 mm with surface charge density σ = ×4 10 coul/m−12 2 and the second sheet located at zp= −1 mm and oppo-sitely charged with surface charge density σ = − ×4 10 coul/m−12 2 (see Figure P5.3b). Find the electric field via superposition by calculating the electric field
y
x z Measurement point
Sheet charge with surface charge density σ coul/m2
Differential charge
element σdxdy r ro
rp dE
(a)
y
x
z
Sheet charge with surface charge density σ coul/m2 located at zp = 1 mm
Sheet charge with surface charge density –σ coul/m2 located at zp = –1 mm
(b)
Figure P5.3 (a) The differential electric field E
d at observation point ro due to a differential sheet charge element σdxdy located on the x-y plane. (b) Two parallel and oppositely charged sheet charges located on the planes zp = +1mm and zp = –1mm.
separately for each sheet and then adding them together. Find Ex, Ey, and Ez for the these three points:
xo, yo, zo = [0, 0, 2 × 10–3] (above the plates) xo, yo, zo = [10 × 10–3, 0, –0.5 × 10–3] (between the plates)
x y zo, ,o o=[ 30 10 , 5 10 , 12 10 ]× −3 − × −3 × −3 (below the plates) Print out the results. Do these results make sense?
Project.5.4
The Biot-Savart law relates electrical current to magnetic field (see Figure P5.4a) and is defined for current flow through wires as
dB 4o Id 2er
r
= π
× (P5.4)
where
dB is the differential magnetic field (a vector), o is the permeability of free space (4π ×10−7 henry/m), I is the current (in amperes),
d is a differen-tial wire length with direction corresponding to the current flow, r is the vector from the current element to the measurement point, r direction, and × represents the vector cross product. In Figure P5.4a, we also ˆer is the unit vector in the define rp as the location of the current element and ro as the point of measure-ment such that r r= −o rp.
We wish to calculate the magnetic field for an infinite wire extending along the z axis and carrying a current of I =
1 mA in the +z direction. Note that, in reality, an infinite-I
y
x
z
Wire
Measurement location r
ro rp
dB Differential current element Id
Figure P5.4a The differential magnetic field element
dB at observation point ro due to a differential wire element carrying current
Id .
length current-carrying wire is impossible because it violates conservation of charge. For now, it is sufficient to presume that the wire is connected to a current source at ±∞.
1. As shown in Figure P5.4b, for a wire in the z direction, d dzp zeˆ
= . Due to the cross product in Equation P5.4, if
d is z directed, then the resulting dB will only have components in the x and y directions. Write two separate equations for dBx and dBy by writing ˆer in terms of its ˆex, ˆey, and ˆez components, computing the cross products between
d and ˆer and then separating the x and y components of
dB into separate equations. Hint: Derive ˆex and ˆey from ˆer using the method employed in Equation (5.10).
2. Write two MATLAB functions for dBx and dBy that compute the x- and y-directed magnetic fields in terms of a single argument zp. Use global variables for the measurement position parameters xo, yo , and zo . Use the quad function to integrate over the interval zp= −∞ ∞[ , ] and calculate the magnetic field at these points:
x y z, , = ×[1 10 , 1 10 , 0]−4 × −4 (first quadrant) x y z, , = − ×[ 1 10 , 1 10 , 0]−4 × −4 (second quadrant) x, y, z = [–1 × 10–4, –1 × 10–4, 0] (third quadrant) x, y, z = [1 × 10–4, –1 × 10–4, 0] (fourth quadrant)
Assume that 10 is a sufficient approximation for infinity. Print out the results.
Comment on any symmetry you find in these solutions.
3. Use the meshgrid function to create a set of points for ro where
−200≤xo≤200 mm, −200≤yo≤200 mm, and zo=0. Use a step size I
x z
Wire along z axis with current I Differential current element Idzpˆe z
r ro
Magnetic field dB (directed into page) at measurement location ro rp
Figure P5.4b The magnetic field due to a wire along the z axis will have only x and y components.
of 50 mm for both xo and yo. Compute the magnetic field Bx and By at each value of ro and plot with quiver. When iterating over your values for ro, be sure to skip the point at the origin.
Project.5.5
Figure P5.5a shows a circular wire with radius R centered at the origin and carry-ing a current I. Using the Biot-Savart law from Equation (P5.4), we can write the differential magnetic field in terms of a differential current element Id IRd peˆ
= θ θ, where Rdθp is the differential length (in cylindrical coordinates) of the current ele-ment at location rp, and ˆeθ is the unit vector in the θp direction. In this type of problem with cylindrical symmetry, the simplest approach is to convert the cylin- drical unit vectors into (constant) cartesian unit vectors but still perform the inte-gration using the cylindrical variables. Figure P5.5b shows how rp and ˆeθ can be expressed in terms of their x and y components using the unit vectors ˆex and ˆey:
rp=ˆ cosexR θ +p eˆ sinyR θp (P5.5a)
ˆeθ= −eˆ sinx θ +p eˆ cosy θp (P5.5b) We can now rewrite Equation (P5.4) as
4 2 2 2 32
( ˆ sin ˆ cos ) (ˆ ( cos ) ˆ ( sin ) ˆ ( ))
(( cos ) ( sin ) ( ) )
B e e e e e
d IRd x R y R z z
x R y R z z
o p x p y p x o p y o p z o p
o p o p o p
= π θ − θ + θ × − θ + − θ + −
− θ + − θ + −
(P5.5c)
where o is the permeability of free space (4π ×10−7 H/m).
y
x
z
Wire loop with radius R
I θp
Measurement location
Differential current element IRdθpˆe θ ro r
rp
dB
Figure P5.5a The differential magnetic field element
dB at observation point ro due to a wire loop located in the x-y plane.
1. Calculate the vector cross product in Equation (P5.5c) and split the result into three separate equations for the x, y, and z components of the differential magnetic field. Use the resulting expressions for dBx, dBy, and dBz to write three MATLAB functions that take one argument (θp) and can be used with MATLAB’s quad function to determine Bx, By, and Bz .
2. Use MATLAB’s meshgrid function to create a set of points in the x-z plane for –8 ≤ x0 ≤ 8 mm (with a step size of 1 mm) and –2 ≤z0 ≤ 2 mm (with a step size of 0.5 mm). Determine Bx and Bz at each point in the set (be sure to use the correct integration limits) and plot with quiver. Assume R = 2.5 mm, I = 1 mA.
Project.5.6
A solenoid is a coil of wire used to create a magnetic field to activate an actuator.
Typical uses include electrical relays, electrically controlled water valves, and auto-motive starter gears. Figure P5.6a shows a solenoid with radius R, length D, turn count N, and current I.
We model the solenoid in MATLAB as a cylindrical sheet current with density
K NIeˆ
= D θ A/m with the current density directed in the θ direction. The Biot-Savart law for a surface current is
= π
×
4 2
B K e
d o dAr r (P5.6a)
where dA is a two-dimensional differential surface element, r is the vector from the differential surface to the measurement point, ˆer is the unit vector in the r direction, × represents the vector cross product, and o is the permeability of free space (4π ×10−7 H/m). From Figure P5.6b, we see that in cylindrical coordinates,
y
x eˆy cosθp
eˆxR cosθp
ˆ
−ex sinθp
ˆeθ
ˆeyR sinθp Wire loop
with radius R
θp I
rp
Figure P5.5b rp and eˆθp may be decomposed into their x and y components to solve for the magnetic field with MATLAB’s quad function.
I
N turns D
2R
Figure P5.6a A solenoid consisting of N turns, radius R, length D, and current I.
y z
x D
2R
θp Measurement location
Differential surface element dA= Rdθpdzp
Cylindrical sheet current with current density K amp/m in the θ direction r
ro rp
K dB
Figure P5.6b The solenoid can be modeled as a sheet current of magnitude K A/m.
= θ
dA Rd dzp p, where rp is the location of the differential surface (with cylindri-cal components rp, θp, and zp), and ˆeθ is the unit vector in the θp direction.
We also define the observation point ro=xo xˆe +yo yeˆ +zo zeˆ . From the geometry of the problem, we can write
KdA and r in terms of Cartesian unit vectors:
ˆ ( ˆ sin ˆ cos )
KdA NI e e e
D Rd dz NI D Rd dz
p p p p x p y p
= θ θ= θ − θ + θ (P5.6b)
r r= −o rp=ˆ (ex xo−Rcos ) ˆ (θ +p ey yo−Rsin ) ˆ (θ +p ez zo−zp) (P5.6c) Substituting back into Equation (P5.6a) gives
4
( ˆ sin ˆ cos ) ˆ ( cos ) ˆ ( sin ) ˆ ( ) ( cos )2 ( sin )2 ( )2 32
B e e e e e
d NIR
D d dz x R y R z z
x R y R z z
o p p x p y p x o p y o p z o p
o p o p o p
( )
( )
= π θ − θ + θ × − θ + − θ + −
− θ + − θ + −
(P5.6d)
1. Calculate the vector cross product in Equation (P5.6d) and split the result into three separate equations for the x, y, and z components of the differential magnetic field. Use the resulting expressions for dBx, dBy, and dBz to write three MATLAB functions that take two arguments (θp and zp) and compute the differential B field in the x, y, and z directions, respectively.
2. Assume that the solenoid is centered at the origin with radius R = 2.5 mm and D
= 15 mm. Use meshgrid to create a three-dimensional point set [X,Y,Z] of measurement points for ro . Note that meshgrid takes an optional third argu-ment to create three-dimensional point sets (see help meshgrid for usage details). Use these limits for ( , , )x y zo o o : − ≤3 xo≤3 mm (with a step size of 1.5 mm), 0ʺ yoʺ 3 (with a step size of 1.5 mm), and −10≤zo≤10 mm (with a step size of 5 mm).
3. Use dblquad to calculate Bx, By, and Bz at each point in the set (be sure to use the correct integration limits). Assume: N = 500, I = 1 mA.
4. Create a three-dimensional quiver plot of your results using MATLAB’s quiver3 function (run help quiver3 for usage details).
157