Você está na página 1de 50

Partial Dierential Equations

Peter Hertel
Physics Department
Osnabr uck University
Germany
Lectures delivered at
TEDA Applied Physics School
Nankai University, Tianjin, PRC
A partial dierential equation describes a eld, a function of more than one
variable. The equation itself is a relation between the arguments, the eld
and its partial derivatives. A partial dierential equation, or PDE, must
be augmented by additional conditions which have to be fullled by the so-
lution. We here concentrate on numerical aspects. We describe the Finite
Dierence Method, the Finite Element Method and propagation algorithms.
Example PDE solvers are formulated in Matlab, they can easily be general-
ized to other programming languages and to more complex problems. Since
this is a tutorial we restrict ourselves to only two variables.
October 5, 2010
Introduction 3
1 Finite Dierences 4
1.1 Approximating derivatives . . . . . . . . . . . . . . . . . . . . 4
1.2 One-dimensional example . . . . . . . . . . . . . . . . . . . . 6
1.3 Laplace equation on a nite domain . . . . . . . . . . . . . . 7
1.4 Von Neumann boundary conditions . . . . . . . . . . . . . . . 12
1.5 Innite domain . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.6 Helmholtz equation . . . . . . . . . . . . . . . . . . . . . . . . 22
2 Finite Elements 27
2.1 Weak form of a partial dierential equation . . . . . . . . . . 27
2.2 Triangulation and tent functions . . . . . . . . . . . . . . . . 28
2.3 A worked out example . . . . . . . . . . . . . . . . . . . . . . 31
3 Propagation 33
3.1 Stability considerations . . . . . . . . . . . . . . . . . . . . . 34
3.2 Crank-Nicholson scheme . . . . . . . . . . . . . . . . . . . . . 36
A Matlab 43
B Program listing 50
2
Introduction
Most laws of physics are relations between derivatives of functions, so called
dierential equations.
Right from the beginning: Newtons rst law states that the product of
the mass of a body and its acceleration (second derivative of the location
with respect to time) are equal to the force exerted on that body. This law,
combined with special initial conditions, allows to calculate the trajectory of
a body. This distinction is important. m x = f describe the law. There are
many trajectories compatible with that law. The initial conditions x(0) = x
0
and x(0) = v
0
single out a unique solution.
The trajectories of one or many bodies are governed by ordinary dierential
equations: there is just one independent variable, time in this example.
The temperature eld of a body depends on time and space, T = T(t, x).
Consequently, the time derivative and spatial derivatives (gradients, diver-
gencies, and curls) enter the game, and we have to struggle with partial
dierential equations.
This series of lectures covers computational methods
1
for solving partial
dierential equations. The subject is vast, and we must restrict ourselves to
simple examples.
We cannot do justice to all methods. Within a time budget of 15 lectures
of 90 minutes we concentrate on the nite dierence method, introduce
the nite element method, and present stability considerations as well as a
worked out example for an initial value problem, the propagation of a beam
of light in a dielectric waveguide.
The nite element method has won the competition in practically all re-
spects. Although it is dicult to realize from scratch, there are commercial
packages which allow for swift solutions.
Examples are presented in the Matlab programming language. First, be-
cause the code is so short that it can be printed and explained within the
text. Second, because the code is nevertheless ecient. Analogous programs
in Fortran or C++ proved to be neither superior nor better readable, but
certainly more dicult to develop.
We apologize for a certain verboseness with respect to program listings. The
program code is extracted from the L
A
T
E
X sources so that discrepancies be-
tween code and documentation will be avoided. The prize to pay is that the
entire program has to be printed, in some places with unnecessary details.
1
as contrasted with analytical methods
3
1 Finite Dierences
The derivative is the limit of a dierence quotient,
f

(x) = lim
x0
f
x
where f = f(x + x/2) f(x /2) . (1)
All nite dierence methods have one feature in common: the derivative
f

= df/dx is approximated by the quotient f/x of nite dierences.


Since a computer has a nite number of dierent states only, and since the
computation time must be nite, it is impossible to work with a continuum
of numbers. We have to represent a function by its values at a nite set of
representative locations.
1.1 Approximating derivatives
Assume that a function f is represented by values f
j
at representative points
x
j
for which we assume x
j
< x
j+1
. What about f

at x
j
?
Well, forming the derivative is a linear operation, and the same shall hold
for the approximation scheme. We also want to mimic dierentiation, so the
scheme must be local. We also assume that a priori both neighbors are of
equal importance. Both requirements are fullled if
f

(x
j
) f

j
=

f
j1
+
0
f
j
+
+
f
j+1
(2)
such the the coecients

,
0
,
+
do not depend on the f
j
.
Let us interpolate by a quadratic function,
f(x) p(x) =
(x x
j
)(x x
j+1
)
(x
j1
x
j
)(x
j1
x
j+1
)
f
j1
+
(x x
j1
)(x x
j+1
)
(x
j
x
j1
)(x
j
x
j+1
)
f
j
(3)
+
(x x
j1
)(x x
j
)
(x
j+1
x
j1
)(x
j+1
x
j
)
f
j+1
By identifying f

j
= p

(x
j
) we arrive at

=
x
j
x
j+1
(x
j1
x
j
)(x
j1
x
j+1
)

0
=
1
x
j
x
j1
+
1
x
j
x
j+1
(4)

+
=
x
j
x
j1
(x
j+1
x
j
)(x
j+1
x
j1
)
4
For equidistant spacing, x
j
= jh, this reduces to
f

j
=
f
j+1
f
j1
2h
, (5)
a highly plausible result.
From (3) we can also read o an approximation f

j
= p

(x
j
) for f

at x
j
.
For the same reasons as explained above we have
f

(x
j
) f

j
=

f
j1
+
0
f
j
+
+
f
j+1
. (6)
These weights are given by

=
2
(x
j1
x
j
)(x
j1
x
j+1
)

0
=
2
(x
j
x
j1
)(x
j
x
j+1
)
(7)

+
=
2
(x
j+1
x
j1
)(x
j+1
x
j
)
For equidistant spacing we obtain
f

j
=
f
j1
2f
j
+f
j+1
h
2
. (8)
This result is highly plausible as well. We should approximate the derivative
at x
j
+h/2 by (f
j+1
f
j
)/h and at x
j
h/2 by (f
j
f
j1
)/h. Their dierence,
if divided by h, coincides with (8).
We test our ndings by the following program. The function to be repre-
sented is f(x) = 1/(1 + x), its second derivative f

(x) = 2/(1 + x)
3
. We
choose an interval s [0, 0.9] and/home/phertel map it by x(s) = s/(1 s).
This maps s [0, 1) to x [0, )]. Equally spaced s-values lead to a
non-equidistant set of representative points x
j
.
1 clear all;
2 N=32;
3 s=linspace(0,0.9,N);
4 x=s./(1-s);
5 f=1./(1+x);
6 xm=x(1:N-2);
7 x0=x(2:N-1);
8 xp=x(3:N);
9 fm=f(1:N-2);
10 f0=f(2:N-1);
5
11 fp=f(3:N);
12 wm=2./(xm-x0)./(xm-xp);
13 w0=2./(x0-xm)./(x0-xp);
14 wp=2./(xp-xm)./(xp-x0);
15 fpp=wm.*fm+w0.*f0+wp.*fp;
16 fdd=2./(1+x0).3;
17 plot(x0,fdd-fpp,.,MarkerSize,20);
We plot the dierence between true and approximated second derivatives in
Figure 1.
0 1 2 3 4 5 6 7
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
x 10
3
Figure 1: The dierence between f

(x
j
) and f

j
is plotted vs. x. The function
f(x) = 1/(1+x) was represented at 32 non-equidistant points. Note the 10
3
scale.
1.2 One-dimensional example
Let us study a boundary problem on a one-dimensional region, = (0, )
say:
u

+u = 0 where u(0) = 0 , u() = 1 (9)


the solution of which is u(x) = sin(x).
We divide the interval [0, ] into n equal subintervals of length h = /N.
x
k
= kh for k = 0, 1, . . . , N are representative points. = {x
0
, x
n
} make
up the boundary while {x
1
, x
2
, . . . , x
n1
} represents the interior of . We
6
approximate the derivative at x
k
h/2 by (x
k
x
k1
)/h and at x
k
+ h/2
by (x
k+1
x
k
)/h. The symmetric second derivative at x
k
should therefore
be approximated by
u

k
=
u
k+1
2u
k
+u
k1
h
2
. (10)
These are the equations to be solved for k = 1, 2, . . . , n 1:
u
k+1
2u
k
+u
k1
+h
2
u
k
= 0 where u
0
= 0 and u
n
= 1. . (11)
Note that this is a system of linear equations Mu = r. The matrix M has
2 +h
2
on its diagonal and 1 in both side diagonals. The right hand side r
vanishes except for r
n1
= u
n
= 1.
Here is the realization in Matlab:
1 N=16;
2 h=0.5*pi/(N-1);
3 d0=ones(N-2,1);
4 d1=ones(N-3,1);
5 M=diag(d1,-1)+(h*h-2)*diag(d0,0)+diag(d1,1);
6 r=zeros(N-2,1);
7 r(N-2)=-1;
8 u=M\r;
9 x=linspace(0,pi/2,N);
10 uu=[0;u;1];
diag(v,k) creates a matrix with a diagonal v which is k places o the main
diagonal. u=M\r solves the system of linear equations Mu = r. Figure 2
depicts the result.
For N as small as 16 (i. e. 14 variables) the maximum deviation is less than
3 10
4
, for N = 1024 the approximation is better than 10
7
. M then
requires more than 8 MB of memory, and the problem is solved in 0.22 s on
my new laptop (as of 2008).
1.3 Laplace equation on a nite domain
Let us study a very simple example. An inner tube of square cross section is
placed concentrically within an outer tube of likewise square cross section.
The inner tube is kept at constant temperature T
i
, the outer tube a temper-
ature T
o
. Between the two tubes there is a homogeneous heat conducting
medium. We are interested in the temperature distribution T between the
two tubes in a stationary state. The temperature eld T then does not
depend on time and has to fulll (
2
x
+
2
y
) T = 0, the Laplace
2
equation.
2
Pierre-Simon Laplace (1749-1827), French mathematician, astronomer, and physicist
7
0 0.5 1 1.5
0.2
0.4
0.6
0.8
Figure 2: Open circles represent the solution of u

+ u = 0 on (0, /2) by the


nite dierence method with suitable boundary conditions. The values at 14 rep-
resentative points are contrasted with the exact solution. Full circles represent the
boundary conditions.
Without loss of generality we dene the heat conducting region by
= (1, 1) (1, 1) [a, a] [a, a] . (12)
The tubes have a width and height of 2 and 2a, respectively (0 < a < 1).
The boundary consists of two disjoint sets. The inner part is

i
= {(x, y) | (|x| = a and |y| a) or (|y| = a and |x| a) } .(13)
The same expression with a replaced by 1 denes the outer part
o
of the
boundary.
We dene u by T = T
o
+u(T
i
T
o
). The function u = u(x, y) has to fulll
3
u
xx
+u
yy
= 0 on , (14)
3
u
x
is short for
x
u which is short for u(x, y)/
x
. u
xx
etc. have to been read likewise.
8
u = 0 on
o
, (15)
u = 1 on
i
. (16)
We here assume equidistant spacing, the same for both coordinates. The
Laplacian at (x
j
, y
k
) = (jh, kh) is then approximated by
(u)
j,k
=
4u
j,k
+u
j+1,k
+u
j,k+1
+u
j1,k
+u
j,k1
h
2
. (17)
The u
j,k
are either variables (if a = (j, k) indexes an interior point), or
boundary values. a = 1, 2, . . . , N
v
is a running index for the N
v
variables.
We rst setup a matrix w which denes the computational window. Its
entries are the prescribed value of u, or Inf which signals not yet computed,
or a variable.
1 N=32;
2 x=linspace(-1,1,N);
3 y=linspace(-1,1,N);
4 a=0.25;
5 [X,Y]=meshgrid(x,y);
6 w([1:N],[1:N])=Inf;
7 w(abs(X)==1|abs(Y)==1)=0;
8 w(abs(X)<=a&abs(Y)<=a)=1;
9 u=laplace(w);
10 mesh(u);
The job is done by the following function. u=laplace(w) solves the Laplace
equation on a domain described by w. We have assumed equidistant spacing,
so that h in (17) drops out.
1 function u=laplace(w)
2 [Nx,Ny]=size(w);
3 jj=zeros(Nx*Ny,1);
4 kk=zeros(Nx*Ny,1);
5 aa=zeros(Nx,Ny);
6 Nv=0;
7 for j=1:Nx
8 for k=1:Ny
9 if w(j,k)==Inf
10 Nv=Nv+1;
11 jj(Nv)=j;
12 kk(Nv)=k;
13 aa(j,k)=Nv;
9
14 end;
15 end;
16 end;
This part of the program nds out the number of variables and establishes
the mapping (j, k) a as well as a (j, k). Next we declare a so far empty
sparse square matrix of dimension N
v
as well as a right hand side column
vector.
We run through the rows of the Laplacian matrix, for a=1:Nv. The diago-
nal, lap(a,a) is set to -4. Then we inspect the four neighbors b to the east,
north, west, and south. If the neighbor is a variable, lap(a,b) is set to 1.
If it is a boundary value, it is added to the right hand side rhs(a)
17 lap=sparse(Nv,Nv);
18 rhs=zeros(Nv,1);
19 for a=1:Nv
20 lap(a,a)=-4;
21 j=jj(a);
22 k=kk(a);
23 if w(j+1,k)==Inf
24 b=aa(j+1,k);
25 lap(a,b)=1;
26 else
27 rhs(a)=rhs(a)-w(j+1,k);
28 end;
29 if w(j,k+1)==Inf
30 b=aa(j,k+1);
31 lap(a,b)=1;
32 else
33 rhs(a)=rhs(a)-w(j,k+1);
34 end;
35 if w(j-1,k)==Inf
36 b=aa(j-1,k);
37 lap(a,b)=1;
38 else
39 rhs(a)=rhs(a)-w(j-1,k);
40 end;
41 if w(j,k-1)==Inf
42 b=aa(j,k-1);
43 lap(a,b)=1;
44 else
45 rhs(a)=rhs(a)-w(j,k-1);
46 end;
47 end;
10
It remains to solve lap*v=rhs for v which is the main eort
4
. The resulting
vector of values for the variables must then be inserted into the matrix
u(j,k) representing the eld.
48 v=lap\rhs;
49 u=w;
50 for a=1:Nv
51 u(jj(a),kk(a))=v(a);
52 end;
We have visualized the result in Figure 3.
0
5
10
15
20
25
30
35
0
10
20
30
40
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Figure 3: The interior rectangular tube is kept at (a relative) temperature u = 1,
the outer at u = 0. We have plotted the temperature in-between. The x, y axes are
labeled by the indices of the computational window.
At this point we should discuss performance issues.
The 32 32 computational window amounts to 836 variables, the sparse
Laplacian requires 52 kB of storage. The computation time is 0.2 s on a
standard PC (as of 2008). A 128 128 window results in 14,852 variables
4
measured in lines of source code, and, for large problems, also in computation time
11
with a Laplacian of 0.94 MB, if stored as a sparse matrix. A full matrix
would need 1.8 GB. The computation time is now 4 s on a standard PC.
1.4 Von Neumann boundary conditions
The example of subsection 1.3 has an obvious symmetry: u(x, y) = u(x, y)
and u(x, y) = u(x, y). Therefore, it suces to study the problem on the
upper right quadrant. Because u is an even function of x and y, the respec-
tive partial derivatives have to vanish at x = 0 or y = 0.
Dirichlet
5
boundary conditions specify the function value at the boundary or
parts of the boundary. Our previous example was formulated with Dirichlet
boundary conditions.
One may also prescribe the normal derivative at the boundary or parts of
the boundary. One speaks of von Neumann
6
boundary conditions in this
case.
In our case we have to solve the Laplace equation u
xx
+u
yy
= 0 on
= [0, 1) [0, 1) [0, a] [0, a] . (18)
The boundary conditions to be observed are
a < x < 1 , y = 0 : u
y
= 0
x = 0 , a < y < 1 : u
x
= 0
x = a , 0 y a : u = 1
0 x a , y = a : u = 1
0 x 1 , y = 1 : u = 0
x = 1 , 0 y 1 : u = 0
(19)
We now have to distinguish between three sorts of quantities: xed values,
variables representing the eld at interior points, and variables at a boundary
being restricted by a vanishing gradient.
Again we describe the problem by a matrix w representing the computational
window
7
. Each entry is either a number (representing a value of the eld
at a boundary) or a symbol. The symbols are IP (interior point), EA (the
5
Peter Gustav Lejeune Dirichlet (1805-1859), German mathematician, professor at
Breslau, Berlin and G ottingen
6
Johann von Neumann (1903-1957), German-American mathematician, lecturer at
Berlin and Hamburg, professor at Princeton university, later at the Institute of Advanced
Studies at Princeton.
7
We treat j in w(j,k) as the x variable running from left to right, or from west to east.
k corresponds to the variable y running from bottom to top, or from south to north.
12
gradient towards east has to vanish) and likewise NO, WE, and SO. There is
one more symbol VA such that w(j,k)>VA is true if the eld value at (j, k)
is a variable. Needless to say that the boundary values must be less than
the symbols.
If (j, k) is characterized by EA, we know that (j +1, k) is outside the domain
of denition. However,
(u
x
)
j,k
=
u
j+1,k
u
j1,k
2h
(20)
must vanish, so that the Laplacian of u at (j, k) has to be calculated with
u
j+1,k
= u
j1,k
. With a = (j, k) and b = (j 1, k) we have to enter
()
a,b
= 2/h
2
instead of 1/h
2
if (j, k) and (j 1, k) were both internal
points.
The following code sets up the computational window w, calls u=lap_vn(w)
and generates a mesh representation of the result.
1 global VA IP EA NO WE SO
2 VA=1000;
3 IP=1001;
4 EA=1002;
5 NO=1003;
6 WE=1004;
7 SO=1005;
8 M=9;
9 N=33;
10 w([1:N],[1:N])=IP;
11 w(1,[M+1:N-1])=WE;
12 w([M+1:N-1],1)=SO;
13 w([1:M],[1:M])=1;
14 w(N,:)=0;
15 w(:,N)=0;
16 u=lap_vn(w);
17 mesh(u);
Here is how the Laplace equation with both Dirichlet and von Neumann
boundary conditions is solved.
For technical reasons (because we want to use it as a global variable) the
computational window has to be duplicated. As before, we establish the
mapping (j, k) a and reserve space for the sparse matrix lap and the
right hand side rhs.
1 function u=lap_vn(w);
2 global VA IP EA NO WE SO
13
3 global ww lap rhs a aa
4 ww=w;
5 [Nx,Ny]=size(ww);
6 jj=zeros(Nx*Ny,1);
7 kk=zeros(Nx*Ny,1);
8 aa=zeros(Nx,Ny);
9 Nv=0;
10 for j=1:Nx
11 for k=1:Ny
12 if ww(j,k)>VA
13 Nv=Nv+1;
14 jj(Nv)=j;
15 kk(Nv)=k;
16 aa(j,k)=Nv;
17 end
18 end
19 end
20 lap=sparse(Nv,Nv);
21 rhs=zeros(Nv,1);
Next we loop over the rows of the Laplacian.
22 for a=1:Nv
23 lap(a,a)=-4;
24 j=jj(a);
25 k=kk(a);
When inspecting neighbors, we have to distinguish between dierent cases:
does the variable represent an interior point, or is it restricted by a von
Neumann condition?
26 switch ww(j,k)
27 case(IP)
28 neighbor(j+1,k);
29 neighbor(j,k+1);
30 neighbor(j-1,k);
31 neighbor(j,k-1);
The function neighbor is explained below. We now handle the case that
the normal derivative towards the east has to vanish:
32 case(EA)
33 neighbor(j,k+1);
34 neighbor(j,k-1);
35 lap(a,aa(j-1,k))=2;
14
and then the same with north, west, and south:
36 case(NO)
37 neighbor(j+1,k);
38 neighbor(j-1,k);
39 lap(a,aa(j,k-1))=2;
40 case(WE)
41 neighbor(j,k+1);
42 neighbor(j,k-1);
43 lap(a,aa(j+1,k))=2;
44 case(SO)
45 neighbor(j+1,k);
46 neighbor(j-1,k);
47 lap(a,aa(j,k+1))=2;
48 end % switch
49 end % for a...
After these preliminaries, the system of linear equations has to be solved,
and the solution must be placed into the u-eld. minput(edit graphics and
continue by striking a key);
50 v=lap\rhs;
51 u=ww;
52 for a=1:Nv
53 u(jj(a),kk(a))=v(a);
54 end
It remains to formulate the neighbor function:
55 function neighbor(j,k)
56 global ww lap rhs a aa VA
57 if ww(j,k)>VA
58 lap(a,aa(j,k))=1;
59 else
60 rhs(a)=rhs(a)-ww(j,k);
61 end
The result is visualized in Figure 4.
In general, a von Neumann boundary condition prescribes the normal deriva-
tive u
n
which must not necessarily vanish. For an EA boundary condition
this would mean
u
j+1,k
= u
j1,k
+ 2h(u
n
)
j,k
(21)
15
0
10
20
30
40
0
5
10
15
20
25
30
35
0
0.2
0.4
0.6
0.8
1
Figure 4: See Figure 3 for details. Exploiting symmetry, only one quadrant of the
previous computational window was investigated. Dirichlet as well as von Neumann
boundary conditions had to be observed.
to be inserted into the approximate Laplacian
(u)
j,k
=
4u
j,k
+u
j+1,k
+u
j,k+1
+u
j1,k
+u
j,k1
h
2
. (22)
It is left as a challenge to the reader to exploit the x y symmetry as well.
1.5 Innite domain
Let us discuss another problem. Two parallel innitely long conducting
strips are held at constant potential u = +1 and u = 1, respectively. There
is another boundary condition: the potential has to vanish at innity.
Now, innity is too large, so we represent it by a nite value, see Figure 5.
For symmetry reasons it suces to calculate the potential in the upper right
quadrant. After all, the computational window must be large, so we should
use variables sparingly.
The following program solves the Laplace equation u
xx
+ u
yy
= 0 on the
rst quadrant. SX and SY dene half a strip, M is a margin which should be
16
-
6
x
y
u = 1
u = +1
u = 0
u = 0
u
x
= 0
Figure 5: The computational window for a strip capacitor.
innite.
1 global VA IP EA NO WE SO
2 VA=1000;
3 IP=1001;
4 EA=1002;
5 NO=1003;
6 WE=1004;
7 SO=1005;
8 SX=14;
9 SY=7;
10 M=20;
11 NX=SX+M;
12 NY=SY+M;
13 w([2:NX-1],[2:NY-1])=IP;
14 w(1,[1:SY-1,SY+1:NY-1])=WE;
15 w(1:SX,SY)=1;
16 w(:,1)=0;
17 w(NX,:)=0;
18 w(:,NY)=0;
19 u=lap_vn(w);
20 contour(u,linspace(0,1,24));
21 axis equal;
17
22 axis([0,4,0,4]);
23 axis tight;
The result is plotted as Figure 6.
5 10 15 20 25 30
5
10
15
20
25
Figure 6: The potential of a strip capacitor in the rst quadrant. There are 811
variables. The axes are labeled by the variable index.
Between the plates well away from the edges the electric eld strength E =
(u
x
, u
y
) is constant. However, the simulation of free space is rather
bad. The eld is obviously pressed upon by the too close-by boundary
representing innity.
Increasing the margin M helps, but not much because the computational
time grows excessively with the number of variables.
A way out is to drop the equidistant spacing scheme. The region close to
innity can be represented by a coarser resolution than the neighborhood of
the strips. Since we do not want to introduce more than one complication
at the same time, both coordinate axes are treated alike.
We map by
z =
s
1 s
or z = s/(1 s) (23)
18
between [0, 1) and [0, ).
Recall (7) how to work out the Laplacian with non-equidistant spacing.
The following code is very special to our example of a strip capacitor, the
result is displayed in Figure 7.
0 0.5 1 1.5 2 2.5 3 3.5 4
0
0.5
1
1.5
2
2.5
3
3.5
4
Figure 7: Lines of constant potential for a strip capacitor. By properly mapping
the coordinates, the boundary condition u = 0 at innity was simulated much
better than in Figure 6.
1 xs=1;
2 ys=0.5;
3 N=50;
4 s=linspace(0,0.9,N);
5 z=s./(1-s);
6 [,XS]=min(abs(z-xs));
19
7 [,YS]=min(abs(z-ys));
8 zz=[-z(2),z];
9 zm=zz(1:N-1);
10 z0=zz(2:N);
11 zp=zz(3:N+1);
12 betm=2./(zm-z0)./(zm-zp);
13 bet0=2./(z0-zm)./(z0-zp);
14 betp=2./(zp-zm)./(zp-z0);
This part denes the strip (0 x x
s
and y = y
s
), denes the mapping,
and calculates the weights

,
0
,
+
.
The next piece determines indexing j, k a:
15 aa=zeros(N,N);
16 jj=zeros(1,N*N);
17 kk=zeros(1,N*N);
18 V=0;
19 for j=1:N-1
20 for k=2:N-1
21 if ((j==0)&&(k=YS))|((j<=XS)&&(k=YS))|(j>XS)
22 V=V+1;
23 aa(j,k)=V;
24 jj(V)=j;
25 kk(V)=k;
26 end
27 end
28 end
Now we set up the sparse Laplacian matrix lap and the right hand side rhs:
29 lap=sparse(V,V);
30 rhs=zeros(V,1);
31 for a=1:V
32 j=jj(a);
33 k=kk(a);
34 lap(a,a)=bet0(j)+bet0(k);
35 if j+1<N
36 lap(a,aa(j+1,k)) =betp(j);
37 end
38 if (k+1==YS)&&(j<=XS)
39 rhs(a)=-betp(k);
40 elseif k+1<N
41 lap(a,aa(j,k+1))=betp(k);
42 end
20
43 if j-1==0
44 lap(a,aa(j+1,k))=lap(a,aa(j+1,k))+betp(j);
45 elseif (k==YS)&&(j-1==XS)
46 rhs(a)=-betm(j);
47 else
48 lap(a,aa(j-1,k))=betm(j);
49 end;
50 if (k-1==YS)&&(j<=XS)
51 rhs(a)=-betm(k);
52 elseif k-1>1
53 lap(a,aa(j,k-1))=betm(k);
54 end
55 end
If j = 1 we have to implement the von Neumann boundary condition u
x
= 0
which is done by identifying the ctitious variable u
0,k
with u
2,k
.
We now solve the sparse system of linear equations and place the solution
at the proper place into the matrix u representing the potential:
56 v=lap\rhs;
57 u=zeros(N,N);
58 u([1:XS],YS)=1;
59 for a=1:V
60 u(jj(a),kk(a))=v(a);
61 end
The remaining part of the program visualizes the result:
62 contour(z,z,u,linspace(0,1,24));
63 axis equal;
64 axis([0,4,0,4]);
65 print -deps2 fdid2.eps
There were 2324 variables in this example. On my old 266 MHz laptop the
program required six seconds for setting up the Laplacian matrix and the
right hand side, and less than one second for solving the system of linear
equations. The sparse Laplacian matrix requires only 15 kB of storage. A
full matrix would have occupied 43.2 MB. Put otherwise, without sparse
matrix algorithms, this or only slightly larger problems would barely be
tractable. You should also have learnt that only a non-equidistant spacing
scheme allowed to keep the number of variables reasonably small when a
boundary at innity is involved.
21
1.6 Helmholtz equation
Maxwells equations read
D = , B = 0 , E =

B , H = j +

D . (24)
D is the dielectric displacement, B the induction, E the electric and H
the magnetic eld strength. The electromagnetic eld is incited by a charge
density and a current density j.
A non-magnetic isotropic dielectric medium is characterized by
D =
0
E and B =
0
H (25)
where
0

0
c
2
= 1. c is the vacuum speed of light, the frequency dependent
permittivity (or relative dielectric constant).
We discuss the propagation of light with angular frequency . Maxwells
equations reduce to
E = k
2
0
E , (26)
where = k
2
0
c. If
0 = E E (27)
is an acceptable approximation, (26) reduces to
E = k
2
0
E , (28)
the Helmholtz
8
equation.
Here we want to study a dielectric waveguide which is described by a per-
mittivity prole = (x, y). Such a waveguide carries modes of the form
E(t, x, y, z) = E(x, y) e
iz it
. (29)
Each component u of E has to obey
u
xx
+u
yy
+k
2
0
u =
2
u . (30)
We look for guided waves which are characterized by

dxdy |u(x, y)|


2
< , (31)
which means nite energy ux. Square integrable solutions of (30) are pos-
sible for certain discrete eigenvalues =
2
only.
22
-
y
6
x
substrate
rib
cover

c
W/2 W/2
H
u = 0
Figure 8: The computational window for a rib waveguide.
We will discuss one of the simplest devices, a rib waveguide. On top of a
substrate (permittivity
s
) there is a rib of rectangular cross section (width
W, height H, permittivity
r
) surrounded by a cover (permittivity
c
). See
Figure 8 for a sketch.
The linear operator dened by the left hand side of (30) is simple to con-
struct. It is the Laplacian plus a contribution on the diagonal. Lets do
it!
We place the computational window [1:NX,1:NY] such that x runs from
xlo to xhi and y from ylo to yhi.
1 EC=1.00;
2 ES=3.80;
3 ER=5.20;
4 lambda=1.50;
5 k0=2*pi/lambda;
6 hx=0.05;
7 hy=0.05;
8 xlo=20;
9 xhi=35;
10 ylo=15;
11 yhi=45;
8
Hermann von Helmholtz (1821-1894), German physicist and physiologist.
23
12 NX=50;
13 NY=59;
The permittivity prole is crudely dened by
14 prm=EC*ones(NX,NY);
15 prm([1:xlo-1],:)=ES*ones(xlo-1,NY);
16 prm([xlo+1:xhi-1],[ylo+1:yhi-1])=ER*ones(xhi-xlo-1,yhi-ylo-1);
We want to avoid continuity considerations here and therefore rene the
permittivity prole such that it is the average at the border of two dierent
media:
17 prm(xlo,:)=0.5*(prm(xlo-1,:)+prm(xlo+1,:));
18 prm(xhi,[ylo+1:yhi-1])=0.5*(ER+EC)*ones(1,yhi-ylo-1);
19 prm([xlo+1:xhi-1],[ylo,yhi])=0.5*(ER+EC)*ones(xhi-xlo-1,2);
At corners we set the average of the four neighboring quadrants,
20 prm(xlo,[ylo,yhi])=0.25*(2*ES+EC+ER);
21 prm(xhi,[ylo,yhi])=0.25*(3*EC+ER);
The following line calls a function which sets up the Helmholtz operator,
the left hand side of (30):
22 H=helmholtz(hx,hy,k0*k0*prm);
The corresponding program will be discussed later.
Now, the Helmholtz operator, which is dened by the left hand side of (30),
is the sum of the Laplacian and a multiplication operator. The Laplacian is
a negative operator, and the eigenvalues of the Helmholtz operator must be
below the maximal permittivity. We look for the highest and the next lower
value dening the ground mode and the rst excited mode. The string LA
tells the diagonalization algorithm to look for eigenvalues with the largest
algebraic part, in this case, two of them.
23 [u,d]=eigs(H,2,LA);
24 mode1=reshape(u(:,1),NX,NY);
25 mode2=reshape(u(:,2),NX,NY);
We display the ground mode in Figure 9 and the rst excited mode in
Figure 10.
After what we have studied before, setting up the Helmholtz matrix is sim-
ple:
24
0
10
20
30
40
50
60
0
10
20
30
40
50
0
0.01
0.02
0.03
0.04
0.05
0.06
0.07
0.08
Figure 9: The ground mode of a dielectric waveguide. The electric eld strength is
plotted on the waveguide cross section. The cover, substrate, and rib permittivities
are
c
= 1.00,
s
= 3.80, and
r
= 5.20. The light wavelength is = 1.50 microns.
The rib is 0.75 microns high and 1.5 microns wide.
1 function H=helmholtz(hx,hy,d)
2 [NX,NY]=size(d);
3 N=NX*NY;
4 ihx2=1/hx/hx;
5 ihy2=1/hy/hy;
6 md=-2*(ihx2+ihy2)*ones(N,1)+reshape(d,NX*NY,1);
7 xd=ihx2*ones(N,1);
8 yd=ihy2*ones(N,1);
9 H=spdiags([yd,xd,md,xd,yd],[-NX,-1,0,1,NX],N,N);
10 for n=NX:NX:N-NX
11 H(n,n+1)=0;
12 H(n+1,n)=0;
13 end
The last four lines of code suppress links between variables at the lower and
upper border of the computational window.
25
0
10
20
30
40
50
60
0
10
20
30
40
50
0.08
0.06
0.04
0.02
0
0.02
0.04
0.06
0.08
Figure 10: The rst excited mode of a dielectric waveguide. See Figure 9 for a
description of the waveguide. The structure cannot guide more than two modes.
We will not discuss further details of waveguiding here. In particular, polar-
ization and continuity requirements at the interface of dierent media pose
problems which are beyond the scope of this text.
26
2 Finite Elements
In the preceding section we have discussed linear partial dierential equa-
tions Lu = 0 or Lu = u. The function u was approximated by representa-
tive values, i. e. by a vector, the dierential operator L by a matrix where
non-vanishing boundary values lead to a right hand side.
The nite element method expands u into a set of functions such that the
operator L can be worked out. The residual r = Lu has to vanish. Instead
of solving for r = 0 directly we demand that the scalar products (f, r) vanish
for a suciently large set of test functions f. The nite element method
proper makes use of tent functions dened on a triangulation of the domain
.
2.1 Weak form of a partial dierential equation
We study the class
cu +a u = f (32)
of elliptic partial dierential equations. u = u(x, y) is the searched for
solution. It is dened in the interior of a nite domain R
2
. = (
x
,
y
)
is the nabla operator in two dimensions. All previously discussed examples
belong to this class. c, a, and f are complex valued functions C, just
as u.
At the boundary we demand
ncu +q u = g , (33)
n, the outward pointing normal vector, q, and g are dened on the boundary
. If the rst term is dropped, we speak of a Dirichlet boundary conditions.
q = 0 stands for a von Neumann boundary conditions. We allow for both.
Let us choose an arbitrary test function v : C. With d = dxdy we
may write (32) as

dvcu +

dvau =

dvf (34)
which, after partial integration, yields

ds nvcu +

d(v) c (u) +

dvau =

dvf . (35)
We insert the boundary condition (33) and nd

d(v) c (u)+

dvau+

ds vqu =

ds vg+

dvf (36)
27
which has to hold for all test functions v. (36) is the weak form of (32) with
(33).
Galerkin
9
suggested to replace the notion of all possible solutions u and
all test functions v by u, v L
n
, an n-dimensional linear space of suitable
functions.
Let us select a base
1
,
2
. . . ,
n
for L
n
. (36) has to valid for v =
1
,
2
, . . .
n
.
We expand
u(x, y) =
n

j=1
U
j

j
(x, y) (37)
and obtain a system of linear equations:
n

j=1
K
ij
U
j
= F
i
. (38)
The matrix K is given by
K
ij
=

d(
i
) c (
j
) +

d
i
a
j
+

ds
i
q
j
, (39)
the right hand side F by
F
i
=

d
i
f +

ds
i
g . (40)
In many applications we have to do with c > 0, a 0, and q 0. In this
case
||w||
2
=

d

c|w|
2
+a|w|
2

ds q|w|
2
(41)
describes a norm. The Galerkin approximation is optimal with respect to
this norm.
2.2 Triangulation and tent functions
The region will be covered by non-overlapping simplexes. These are in-
tervals in one dimension, triangles in two dimensions, tetrahedrons in three
dimensions, and so forth. The regions is made up of such simple nite
elements.
Triangulation or the analogous procedure in three and more dimension is
a subtle problem, we rely on the Matlab PDE Toolbox. As an example
we show in Figure ?? a triangulation for the heat conduction problem as
discussed earlier.
9
Boris Grigorievich Galerkin (1871-1945), Russian mathematician, inventor of the nite
element method.
28
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Figure 11: Triangulation of the region between two parallel and concentric
squares. Symmetry arguments allow to take only one quadrant into account. Von
Neumann as well as Dirichlet boundary conditions have to be specied.
There are triangles, nodes, and edges. The boundary is a set of edges. We
denote nodes by their coordinates, p
i
= (x
i
, y
i
). For each node i we construct
a tent function
i
as follows.
i
is piecewise continuous and linear, and we
demand

i
(x
j
, y
j
) =
ij
. (42)
This choice has an important consequence: the expansion coecients U
j
of
(37) are the eld values at the corresponding nodes,
u(x
j
, y
j
) = U
j
. (43)
Let us have a closer look at the triangulation as sketched in Figure ??.
29
p
1
p
2
p
3
p
4
p
5
p
6 A
B
C
D
E
Figure 12: Neighborhood of node p
1
. The tent function
1
has the value 1 at p
1
and falls o linearly towards the edges p
3
p
6
, p
6
p
5
, p
5
p
2
, and p
2
p
3
. It
vanishes outside. Emphasis is on triangle A.
Tent function
1
does not vanish within triangles A, C, D, and E only,
where it falls o linearly from 1 at p
1
towards the edges p
3
p
6
, p
6
p
5
,
p
5
p
2
, and p
2
p
3
.
We here discuss
1
on triangle A. The area of A is where
2 = x
1
y
2
+x
2
y
3
+x
3
y
1
x
1
y
3
x
2
y
1
x
3
y
2
. (44)
(44) is the result of rewriting (x
2
x
1
)(y
3
y
1
) (x
3
x
1
)(y
2
y1) or a
cyclic permutation thereof. We dene
s(x, y) =
(y
2
y
3
)(x x
3
) + (x
3
x
2
)(y y
3
)
2
. (45)
s is linear in x and y, it vanishes at p
2
and p
3
, and s(x
1
, y
1
) = 1. Therefore

1
= s on triangle A. We simply change indices and arrive at similar ex-
pressions for
1
on triangles C, D and E. We likewise construct expressions
for
2
and
3
on triangle A. All remaining tent functions vanish on A.
The partial derivatives of
1
on A are given by

1
=
y
2
y
3
2
and
y

1
=
x
3
x
2
2
. (46)
30
Hence the nite element A contributes to K
11
by

A
d
1
c
1
= c

y
2
y
3
2

2
+

x
3
x
2
2

. (47)
Here c is the function c = c(x, y) of (32) evaluated at the center of triangle
A. The contribution to K
12
is

A
d
1
c
2
= c

y
2
y
3
2
y
3
y
1
2
+
x
3
x
2
2
x
1
x
3
2

. (48)
These examples should suce to explain how to assemble the various inte-
grals:
functions c, a, and f will be evaluated at the center of the actual
triangle
functions q and g are evaluated at the center of the actual edge
partial derivatives are given by (46).
Triangle A for example with nodes p
1
, p
2
, and p
3
contributes to K
11
, K
12
,
K
13
, K
21
, K
22
,K
23
, K
13
, K
23
,K
33
as well as F
1
, F
2
, and F
3
. On the other
hand, contributions to K
11
come from triangles A, C, D, and E.
2.3 A worked out example
We refer to the heat conduction problem of subsection 1.3. This time we do
not solve it by the nite dierence method but with the Matlab PDE Tool-
box. This toolbox implements the nite element method for solving a large
class of partial dierential equations
10
.
>> pdetool
at the Matlab prompt opens a graphical user interface for specifying and
solving a partial dierential equation. You may compose out of rect-
angles (including squares), ellipses (including circles) and polygons. These
primitive regions can be combined by the union, intersection and dierence
operations. Once has been constructed, you may go to boundary mode.
By clicking on one ore more boundary sections, the boundary conditions may
be specied. Next you specify the partial dierential equation to be solved.
Thereafter you create an initial mesh of triangles which should be rened at
least once. Now one may solve the problem and plot the solution. The mesh
([p,e,t])
11
and the solution u may be exported to the Matlab workspace,
10
Note that Matlab identies PDE (partial dierential equations) with the nite ele-
ment method FEM to solve them.
11
points, edges of the boundary, and triangles
31
and th Symmetry arguments allow to take only one quadrant into account.
e solver function as well. See Figure 11 for the triangularization which we
have employed.
0
0.2
0.4
0.6
0.8
1
0
0.2
0.4
0.6
0.8
1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Figure 13: FEM solution of u
xx
+ u
yy
= 0 with u = 1 on the inner and u = 0
on the outer square. The triangulation shown in Figure 11 has been used. For
symmetry reasons, only one quarter of the region was investigated. Therefor von
Neumann as well as Dirichlet boundary conditions had to be implemented.
The Matlab PDE Toolbox is not restricted to domains which are made up
of rectangles and circles. You may specify a le problemg.m which describes
the geometry of your problem. Inspect lshapeg.m as an example.
The Matlab PDE Toolbox provides not only for elliptic, but also for
parabolic, hyperbolic and eigenvalue problems. Prototypes are
u
xx
+u
yy
= 0
u
t
u
xx
= 0
u
tt
u
xx
= 0
u
xx
+u
yy
u = 0
32
3 Propagation
The heat equation

T = T (49)
for the temperature eld T = T(t, x) is the prototype of an initial value
problem. For time t = 0 the temperature distribution T(0, x) = T
0
(x) is
given, and we want to nd T(t, x) for t > 0. So far we have studied the
nal, or stationary state only which is described by

T = 0, or T = 0. By
the way, it is always advisable to chose proper units of space and time such
that (49) reads
u
t
= u
xx
+u
yy
+u
zz
. (50)
The Fresnel
12
equation is another prototype example. Monochromatic light
in an isotropic medium with smoothly varying permittivity = (x, y, z) is
described by the Helmholtz equation
E = k
2
0
E , (51)
where E is any component of the electric eld and k
0
= /c = 2/ is the
vacuum wave number. We now assume that u is almost a plain wave,
E(x, y, z) = u(x, y, z) e
iz
(52)
such that
|u
zz
| k
0
|u
z
| (53)
holds true. We then obtain
iu
z
=
u
xx
u
yy
+ (
2
k
2
0
) u
2
, (54)
the Fresnel equation. Again, u(x, y, 0) = u
0
(x, y) is prescribed, and we want
to know the eld u(x, y, z) at z. Note that the stationary case iu
z
= 0
describes modes which we have discussed before.
Again, this equation may be written as
iu
z
= u
xx
+u
yy
+u (55)
12
Augustin-Jean Fresnel (1788-1827), French physicist, pioneer of wave optics. He main-
tained that not even acclaim from distinguished colleagues could compare with the plea-
sure of discovering a theoretical truth or conrming a calculation experimentally.
33
by choosing proper units.
The initial value problem is solved most often by a nite dierence method:
advance from u(t, x) to u(t + h, x) where t should be replaced by z in case
of the Fresnel equation. There are various possibilities, but only some of
them are stable. The Crank-Nicholson scheme is stable for both prototype
propagation problems, we study it in detail. We also discuss the classic Feit
and Fleck beam propagation algorithm.
3.1 Stability considerations
Von Neumann has studied the stability of various propagation algorithms.
Both prototypes, the heat equation and the Fresnel equation, are of rst
order with respect to the propagation coordinate (t or z) and of second order
with respect to the cross section coordinates. To keep thing simple, we allow
for one space coordinate x only (besides the propagation coordinate.
We represent the eld u = u(t, x) or u = u(z, x) on a mesh:
u
n
r
= u(n, rh) where n, r Z . (56)
When the propagation step size and the cross section spacing h are choosen
very, very small, the coecients in front of the partial derivative operators
become constant, and multiplication terms even drop out. Locally, there is a
polynomial of dierentiation operators which is best dealt with by a Fourier
decomposition. So, for a particular space frequency k we may write
u
n
r
= (k)
n
e
ikrh
. (57)
The propagation scheme u
n
r
u
n+1
r
is stable if |(k)| 1 for all real space
frequencies k.
Explicit forward
Naively you might write for the heat equation u
t
= u
xx
u
n+1
r
u
n
r

=
u
n
r+1
2u
n
r
+u
n
r1
h
2
. (58)
We call this scheme forward in time, centered in space. Inserting (57) yields
(k) = 1
4
h
2
sin
2
kh
2
. (59)
For the Fresnel equation we obtain
(k) = 1 i
4
h
2
sin
2
kh
2
. (60)
Forward in time, centered in space is stable for the heat equation provided
2 h
2
. It is never stable for the Fresnel equation.
34
Implicit forward
One might employ the above propagation scheme backward. The new co-
ecients u
n+1
r
are chosen such that the values u
n
r
result. This scheme is
therefore implicit and termed backward in time, centered in space. We set
u
n+1
r
u
n
r

=
u
n+1
r+1
2u
n+1
r
+u
n+1
r1
h
2
(61)
and obtain
1
(k)
= 1 +
4
h
2
sin
2
kh
2
(62)
for the heat equation and
1
(k)
= 1 +i
4
h
2
sin
2
kh
2
(63)
for the Fresnel equation. In both cases the propagation scheme is stable.
Averaging explicit and implicit forward
Both schemes presented so far are biased with respect to the propagation
direction. Therefore, it is an appealing idea to combine them symmetrically.
We propagate forward from n by /2 and backward from (n+1) by /2
and demand that the results are equal:
u
n
r
+

2
u
n
r+1
2u
n
r
+u
n
r1
h
2
= u
n+1
r


2
u
n+1
r+1
2u
n+1
r
+u
n+1
r1
h
2
. (64)
The heat equations gives rise to
(k) =
1 (2/h
2
) sin
2
(kh/2)
1 + (2/h
2
) sin
2
(kh/2)
, (65)
the Fresnel equation to
(k) =
1 i(2/h
2
) sin
2
(kh/2)
1 +i(2/h
2
) sin
2
(kh/2)
. (66)
This propagation scheme, which was suggested by Crank and Nicholson, is
always stable. Moreover, it is correct to one more order of the time step .
35
3.2 Crank-Nicholson scheme
As an example we will implement the Crank-Nicholson scheme for the prop-
agation of light as described by the Fresnel equation. We restrict ourselves
to a one-dimensional cross section. We have to solve
u
z
=
i
2

u
xx
+k
2
0

2

(67)
where u(x, 0) = u
0
(x) is prescribed.
Homogeneous medium
Recall that is a reference propagation constant which should be chosen
such that the envelop function u in (52) depends but slowly on z. Here
we discuss a homogeneous medium, the permittivity prole (x) does not
depend on x. We therefore choose = k
0

so that
u
z
=
i
2
u
xx
(68)
has to be solved.
Let us rst setup the wavelength (He-Ne laser), the refractive index (glass),
and the computational window. All lengths are in microns.
1 % this file is propag_hm.m
2 lambda=0.633;
3 k0=2*pi/lambda;
4 rfr_ndx=1.50;
5 beta=k0*rfr_ndx;
6 hx=0.1;
7 wx=10.0;
8 x=[-0.5*wx:hx:0.5*wx];
9 N=size(x,1);
Now we formulate the propagation scheme. Stepping forward h
z
/2 is achieved
by matrix F, stepping backward by matrix B. d
m
and d
n
are the main and
next to main diagonals. We have implemented (59) for the Fresnel equation
by inserting i at the proper place and dividing by 2.
10 hz=1.0;
11 f=0.5*i*hz/(2*beta);
12 dm=f*(-2*ones(N,1))/hx2;
13 dn=f*ones(N-1,1)/hx2;
14 F=eye(N)+diag(dn,-1)+diag(dm,0)+diag(dn,1);
15 B=eye(N)-diag(dn,-1)-diag(dm,0)-diag(dn,1);
36
It remains to step forward by solving u B
1
Fu. The intensity history is
recorded and plotted.
16 Z=60;
17 history=zeros(N,Z);
18 width=1.0;
19 u=exp(-(x/width).2);
20 for n=1:Z
21 history(:,n)=abs(u).2;
22 u=B\(F*u);
23 end
24 mesh(1:Z,x,history)
The result is plotted as Figure 14.
0
10
20
30
40
50
60
5
0
5
0
0.2
0.4
0.6
0.8
1
Figure 14: A Gaussian beam enters from left and propagates in a homogeneous
medium. The light intensity is plotted vs. x for 60 propagation steps. The compu-
tational window is 10 m wide, propagation is in steps of 1.0 m.
What has gone wrong? Nothing initially, the Gaussian beams spreads out
as it should until it reaches the edges of the computational window. In
fact, we silently assumed that the eld vanishes outside the computational
window which amounts to reection at the window boundaries. Figure 14
37
correctly describes the propagation of light between two parallel mirrors,
with beautiful interference eects.
Transparent boundary conditions
However, we wanted to study the propagation of light in a transparent
medium. Headly
13
has suggested a method for simulating a transparent
computational window. He assumes, and enforces, the eld to be outgoing
waves at the boundaries.
Let us discuss the upper end x = W/2. If u e
ikx
, and if the real part
of k is positive, then the waves ow outward, as they should. If not, the
boundary conditions are manipulated in such a way that the real part of k
vanishes.
Assume the points within the window to be labeled by 1, 2, . . . , N. From
the old eld u
n
we work out the wave number k by setting
e
ikh
x
=
u
n
N
u
n
N1
. (69)
If the real part of k should turn out to be negative, we change it to zero.
With the possibly modied wave number

k we dene
= e
i

kh
x
, (70)
and we program as if
u
n
N+1
= u
n
N
and u
n+1
N+1
= u
n+1
N
(71)
would hold, thereby enforcing outgoing waves at the boundary.
An analogous procedure is applied to the lower end of the computational
window.
The new program is the same up to the propagation statement:
1 % this file is propag_tb.m
2 TINY=1.0e-4;
3 lambda=0.633;
4 k0=2*pi/lambda;
5 rfr_ndx=1.50;
6 beta=k0*rfr_ndx;
7 hx=0.1;
8 wx=10.0;
13
G. R. Headley, Transparent boundary conditions for the beam propagation method,
IEEE Journal of Quantum Electronics 28 (1992) 363-370
38
9 x=[-0.5*wx:hx:0.5*wx];
10 N=size(x,1);
11 hz=1.0;
12 f=0.5*i*hz/(2*beta);
13 dm=f*(-2*ones(N,1))/hx2;
14 dn=f*ones(N-1,1)/hx2;
15 F=eye(N)+diag(dn,-1)+diag(dm,0)+diag(dn,1);
16 B=eye(N)-diag(dn,-1)-diag(dm,0)-diag(dn,1);
17 Z=60;
18 history=zeros(N,Z);
19 width=1.0;
20 u=exp(-(x/width).2);
21 for n=1:Z
22 history(:,n)=abs(u).2;
At this point we had written u=B\(F*u);, which proved to be too simple.
We modify:
23 FF=F;
24 BB=B;
25 if abs(u(1))>TINY
26 k=i/hx*log(u(2)/u(1));
27 if real(k)<0 k=i*imag(k); end
28 tbc=exp(i*k*hx)*f/hx2;
29 FF(1,1)=FF(1,1)+tbc;
30 BB(1,1)=BB(1,1)-tbc;
31 end
32 if abs(u(N))>TINY
33 k=-i/hx*log(u(N)/u(N-1));
34 if real(k)<0 k=i*imag(k); end
35 tbc=exp(i*k*hx)*f/hx2;
36 FF(N,N)=FF(N,N)+tbc;
37 BB(N,N)=BB(N,N)-tbc;
38 end
39 u=BB\(FF*u);
The rest remains,
40 end;
41 mesh(1:Z,x,history)
And indeed, the Gaussian gets wider and wider, but there is no reection
from the boundaries of the articial computational window which is con-
vincingly demonstrated in Figure 15.
39
0
10
20
30
40
50
60
5
0
5
0
0.2
0.4
0.6
0.8
1
Figure 15: Same as Figure 14, but this time transparent boundary conditions have
been implemented.
Propagation in a waveguide
Assume there is a slab of thickness 3.0 m the refractive index of which is
slightly higher. Let us increase the permittivity of the slab by 0.025, for
example.
We add one line of code,
del_eps=0.025*(abs(x)<=1.5);
and change the denition of the main diagonal into
dm=f*(-2*ones(N,1)/hx2+k02*del_eps);
Moreover, we propagate over a longer distance, Z=300.
The entire program listing can be found in the appendix.
With these slight modications we have simulated how a guided mode is
excited by a Gaussian beam, as shown in Figure 16
We have plotted the power within the computational window by the follow-
ing simple program:
40
50 100 150 200 250 300
5
4
3
2
1
0
1
2
3
4
5
Figure 16: The propagation of an initially Gaussian beam in a slab waveguide.
Contour lines refer to the light intensity. Part of the light is radiated away, the rest
nally propagates as a guided wave.
1 pw=sum(history,1);
2 plot(pw/pw(1));
3 axis([0,Z*hz,0.8,1]);
Let us recall the dimensions: the computational window is 10 m wide, the
guiding layer has a thickness of 3.0 m. It takes more than 200 m until the
mode has formed. This is no problem for a glass ber, but intolerable for an
integrated optical device. The beam prole must be shaped smoothly by a
taper in order to avoid insertion losses and reduce mode formation lengths.
It is left as an exercise to the reader to play with parameters. What hap-
pens if the Gaussian beam and the guided mode have a better overlap?
What happens if the waveguide is excited o-center? Make the waveguide
narrower, the permittivity of the guiding slab higher, and so forth.
41
0 50 100 150 200 250 300
0.8
0.82
0.84
0.86
0.88
0.9
0.92
0.94
0.96
0.98
1
Figure 17: Normalized power within the computational window vs. propagation
distance (microns). 86 % of the initial power nally propagates without further
radiation losses. Note that it takes a certain distance until the incoming light has
reached the border of the computational window.
42
A Matlab
Matlab is concerned with matrices of numbers. Natural, integer, rational,
real, and complex numbers are treated alike, as implied by N Z Q
R C. An mn matrix consists of m rows of length n, or of n columns of
length m.
Generating matrices
A 2 3 matrix like
a =

a
11
a
12
a
13
a
21
a
22
a
23

1 2 3
4 5 6

has two rows and three columns. Here a column vector is a 2 1 matrix, a
row vector is a 1 3 matrix.
Matrices are constructed within square brackets [. . .]. Submatrices (in par-
ticular numbers) are concatenated horizontally by the , operator and ver-
tically by the ; operator. On the Matlab prompt we may write
>> a=[1,2,3;4,5,6]
or
>> a=[[1,2,3];[4,5,6]]
or
>> a=[[1;4],[2;5],[3;6]]
or
>> a=[[[1,2];[4,5]],[3;6]]
Another possibility is
>> r1=[1,2,3]
>> r2=[4,5,6]
>> a=[r1;r2]
Likewise
>> c1=[1;4]
>> c2=[2;5]
>> c3=[3;6]
>> a=[c1,c2,c3]
43
Instead of inserting numbers one after the other, one may generate equally
spaced sequences by the from:step:to construct. [0:0.1:10] is an exam-
ple, a row vector with 101 elements. x:y is short for x:1:y.
The operator eects transposing and complex conjugating. As an exam-
ple,
>> row=[1,2+3i]
>> col=row
is the same as
>> col=[1;2-3i]
Note the letter i for the imaginary unit.
zeros(m,n) generates an mn matrix of zeros. ones(m,n) works likewise.
eye(m) creates an m m (square) unit matrix
14
. rand(m,n) produces an
mn matrix of random numbers which are uniformly distributed in [0, 1].
randn generates normally distributed random numbers with mean 0 and
variance 1.
Adding by the + operator, subtracting by the - operator and multiplying
matrices by the * operator works as expected. It is an error if the matrices
to be combined do not match. Matrices of equal shape may be multiplied or
divided elementwise by the .* or ./ operators. Most functions, like sin,
sqrt etc. operate elementwise on matrices. Try sqrt([0:2]*[0:4]).
Displaying matrices
The result of an assignment like a=rand(2,2) is automatically displayed.
Numbers, vectors and matrices are sent to the Matlab command window.
The output format command (see >> help format) allows to toggle be-
tween legibility and accuracy. Output to the Matlab window is stopped if
the command is terminated by a semicolon.
In many cases, a vector represents the values of a variable which should be
displayed graphically.
In its simplest form the plot command plots a vector vs. index. Try the
following command sequence:
>> x=pi*[0:0.1:2];
>> y=sin(x);
>> plot(y)
14
The symbols I and i are already reserved for the imaginary unit. The unity matrix is
therefore denoted by eye which is pronounced in English as I.
44
However, plot(x,y) is better. Note that we delimited the rst two com-
mands by a semicolon thus suppressing output to the Matlab command
window.
Saving and loading matrices
The following two lines produce a matrix a and save it to a le:
>> a=rand(8,4);
>> save test.dat a -ascii
The corresponding load command is
>> load test.dat -ascii
It produces a matrix test which (almost) reproduces the original matrix
a. Almost, because data are written in legible ascii format with eight digits
precision. If this is not sucient, write -ascii -double instead. Without
the ascii modier, data would be written in binary format, and such binary
data les are useless for any other program than Matlab.
There are also low-level le input/output operations which are useful if data
come from or are intended for another program.
Accessing matrix elements
The elements of a matrix are addressed by specifying a (row,column) index
pair, such as a(2,3) for a
23
. The result is a number.
Instead of an index, one may also specify a vector of indices. Thus, if a is
the above 2 3 matrix, the assignment b=a([1,2],[1,3]) results in
b =

1 3
4 6

The double colon : is short for all indices. We could also write b=a(:,[1,3]).
a(m,:) is the mth row and a(:,n) the nth column. a(:,:) is a itself.
The dimensions of a matrix a are determined by [m,n]=size(a). Thus,
erasing the rst row and the rst column may be programmed as
>> [rows,cols]=size(a);
>> b=a([2:rows],[2:cols]);
There are also functions for extracting diagonals (diag) and lower or up-
per triangular parts (tril and triu). Creating a symmetric matrix S of
normally distributed random numbers may be achieved by
45
a=randn(dim);
b=triu(a,0)+triu(a,1);
Solving linear equations
Solving a*x=y for a square matrix a and a column vector y is surprisingly
simple. Just say
>> x=a\y;
This command has the same eect as x=inv(a)*y where inv(a) is the
inverse a
1
of a. The division operator \ however is much faster. After
all, inv(a) solves all linear equations with a, not only one.
The following piece of code is self-explaining:
>> a=randn(5); % an arbitrary 5 by 5 matrix
>> y=randn(5,1); % an arbitrary column vector
>> x=a\y; % solve a*x=y
>> norm(y-a*x) % y-a*x should be zero
Note that text following a percent sign is ignored (comment).
For completeness: there is also a left division operator / . It solves equations
like x*a=y for row vectors y by x=y/a which is a row vector as well.
Diagonalization
Recall that a square matrix A (not necessarily hermitian) may be written as
AV = V D where D is diagonal. The diagonal elements d
(k)
= D
kk
are the
eigenvalues of A, the column vectors v
(k)
i
= V
ik
the corresponding eigenvec-
tors. The eig command comes in two versions. d=eig(A) computes only
the eigenvalues of A. [V,D]=eig(A) computes the matrix V of eigenvectors
as well as the diagonal matrix D of eigenvalues. Try this:
>> A=randn(5);
>> d=eig(A);
>> [V,D]=eig(A);
>> norm(d-diag(D))
>> v1=V(:,1);
>> d1=d(1);
>> norm(A*v1-d1*v1)
By the way, such a long sequence of instructions should be written line
by line to a script le (without the Matlab prompt >> ). Call this le
test_eig.m, the extension indicating Matlab. You may now issue the
command
46
>> test_eig
Matlab searches for a le test_eig.m and executes its instructions.
Ordinary dierential equations
Ordinary dierential equations can be transformed into systems of rst order
dierential equations. The state x, an n-dimensional vector, depends on a
real parameter t. The system of rst order dierential equations is described
by the slope eld f = f(t, x) such that
dx
i
(t)
dt
= f
i
(t, x
1
(t), x
2
(t), . . . , x
n
(t))
holds for all t.
Before we can solve a system of ordinary dierential equations, we must
describe it. We have to specify a function, the input being a scalar t and a
vector x, the output is x.
Let us come back to the Kepler problem of planetary motion. The planet
moves in a plane. At a certain time t it is located at x
1
, x
2
and has a velocity
x
3
= v
1
= x
1
, x
4
= v
2
= x
2
. In suitable units we have to solve
x
1
= x
3
; x
2
= x
4
; x
3
= x
1
/r
3
and x
4
= x
2
/r
3
where r =

x
2
1
+x
2
2
.
You should create a le kepler.m with the following lines of text:
function xdot = kepler(t, x)
r3=(x(1)2+x(2)2)(3/2);
xdot=[x(3);x(4);-x(1)/r3;-x(2)/r3];
This slope eld describes the system of ordinary dierential equations (ODE)
to be solved.
The standard Matlab program is ode45. It requires a function (here:
kepler in le kepler.m), a vector of times for which a state is to be cal-
culated, and an initial state. We will start the planet at (1, 0) with initial
velocity (0, 0.8). We also set the desired relative accuracy.
>> options=odeset(RelTol,1e-6);
>> [t,x]=ode45(kepler,[0:0.1:10],[1,0,0,0.8],options);
>> x1=x(:,1); x2=x(:,2);
>> plot(x1,x2);
One may check the accuracy of this solution by working out the total energy:
47
>> v1=x(:,3); v2=x(:,4);
>> te=(v1.2+v2.2)/2-1./sqrt(x1.2+x2.2);
>> max(te)-min(te)
Consult the documentation for more ODE solvers and for control options.
. . . and much more
Matlab knows many special functions (Airy, Bessel, Beta, etc.).
fminsearch nds the minimum of a real valued function of many ar-
guments (simplex method of Nelder and Mead).
fft performs the fast Fourier transform of vectors or matrices (colum-
nwise).
polyfit ts data to a polynomial.
Matlab can work with sparse matrices. This is particularly import for
partial dierential equations. Call >> help sparfun for a summary
of MLs sparse matrix capabilities.
There is a rich set of graphic functions, 2D and 3D as well.
Avoid loops!
After a while you think about x=linspace(-1,1,1024) as a variable. Of
which your need the square, x2=x.2, for example. Such block operations
are fast in Matlab. Loops (repetitive commands) however are interpreted,
they should be avoided. Just look at the following example where we have
multiplied two matrices, rst by a matrix command, then by looping:
% this file is test_loop.m
% compare matrix and loop operations
dim=300;
a=randn(dim);
b=randn(dim);
tic;
c=a*b;
toc;
tic;
c=zeros(dim,dim);
for i=1:dim
for k=1:dim
sum=0;
for j=1:dim
48
sum=sum+a(i,j)*b(j,k);
end;
c(i,k)=sum;
end;
end;
toc;
On my laptop (2008) the cpu-times were 0.020 s and 110 s. The factor 5,000
in execution time stresses what I said before: avoid loops whenever possible,
in particular nested loops.
49
B Program listing
The following program has produced Figure 16.
1 TINY=1.0e-4;
2 lambda=0.633;
3 k0=2*pi/lambda;
4 rfr_ndx=1.50;
5 beta=k0*rfr_ndx;
6 hx=0.1;
7 wx=10.0;
8 x=[-0.5*wx:hx:0.5*wx];
9 N=size(x,1);
10 del_eps=0.025*(abs(x)<=1.5);
11 hz=1.0;
12 f=0.5*i*hz/(2*beta);
13 dm=f*(-2*ones(N,1)/hx2+k02*del_eps);
14 dn=f*ones(N-1,1)/hx2;
15 F=eye(N)+diag(dn,-1)+diag(dm,0)+diag(dn,1);
16 B=eye(N)-diag(dn,-1)-diag(dm,0)-diag(dn,1);
17 Z=300;
18 history=zeros(N,Z);
19 u=exp(-(x/1.0).2);
20 for n=1:Z
21 history(:,n)=abs(u).2;
22 FF=F;
23 BB=B;
24 if abs(u(1))>TINY
25 k=i/hx*log(u(2)/u(1));
26 if real(k)<0 k=imag(k); end
27 tbc=exp(i*k*hx)*f/hx2;
28 FF(1,1)=FF(1,1)+tbc;
29 BB(1,1)=BB(1,1)-tbc;
30 end
31 if abs(u(N))>TINY
32 k=-i/hx*log(u(N)/u(N-1));
33 if real(k)<0 k=imag(k); end
34 tbc=exp(i*k*hx)*f/hx2;
35 FF(N,N)=FF(N,N)+tbc;
36 BB(N,N)=BB(N,N)-tbc;
37 end
38 u=BB\(FF*u);
39 end
40 contour(1:Z,x,history,24,k-);
50

Você também pode gostar