Sie sind auf Seite 1von 49

Partial Differential Equations with

SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
PARTIAL DIFFERENTIAL EQUATIONS 2
D De ef fi in ni it ti io on ns s 2
C Cl la as ss si if fi ic ca at ti io on n o of f l li in ne ea ar r, , s se ec co on nd d- -o or rd de er r P PD DE Es s 3
Analytical solutions of PDEs 3
Analytical solutions to parabolic equations: one-dimensional solution of the heat equation 3
Analytical solutions to parabolic equations: One-dimensional solution of the wave
equation 9
Analytical solutions to hyperbolic equations: Two-dimensional solution to Laplace's
equation in a rectangular domain. 13
More solutions to Laplace equation in a rectangular domain 17
Superposition of solutions for cases [1] and [2] 21
Laplacian in polar-cylindrical coordinates 24
Solution to Laplace's equation in an annulus 24
Solution to the Laplace equation in a disk 28
Numerical solutions to partial differential equations 32
Numerical solution to parabolic PDEs: heat equation 32
A function for the explicit numerical solution of the heat equation 33
Numerical solution to hyperbolic PDEs: the wave equation 35
A function for the solution of the wave equation 36
Numerical solution to elliptic PDEs: Laplace equation 38
A function to solve Laplace equation in a rectangular domain 40
Exercises 42
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
Partial Differential Equations
This chapter introduces basic concepts and definitions for partial differential equations (PDEs)
and solutions to a variety of PDEs. Applications of the method of separation of variables are
presented for the solution of second-order PDEs. The application of this method involves the
use of Fourier series that were introduced in Chapter 13. This chapter also presents numerical
solutions to simple second-order PDEs that describe physical systems. The aim of the chapter
is to demonstrate SCILAB applications in the solution of well-known PDEs from mathematical
physics, rather than being a comprehensive presentation on partial differential equations.
D De ef fi in ni it ti io on ns s
Equations involving one or more partial derivatives of a function of two or more
independent variables are called partial differential equations (PDEs).
Well known examples of PDEs are the following equations of mathematical physics in
which the notation: u =u/x, u
xy
=u/yx, u
xx
=
2
u/ x
2
, etc., is used:
[1] One-dimensional wave equation: u
tt
= c
2
u
xx
[2] One-dimensional heat equation: u
t
= c
2
u
xx
[3] Laplace equation: u
xx
+u
yy
= 0, (2-D), or u
xx
+u
yy
+u
zz
= 0 (3-D)
[4] Poisson equation: u
xx
+u
yy
= f(x,y ),(2-D), or u
xx
+u
yy
+u
zz
= f(x,y,z) (3-D)
The order of the highest derivative is the order of the equation. For example, all of
the PDEs in the examples shown above are of the second order.
A PDE is linear if the dependent variable and its functions are all of first order. All of
the PDEs shown above are also linear.
A PDE is homogeneous if each term in the equation contains either the dependent
variable or one of its derivatives. Otherwise, the equation is said to be non-homogeneous.
Equations [1], [2], and [3] above are homogeneous equations. Equation [4] is non-
homogeneous.
A solution of a PDE in some region R of the space of independent variables is a
function, which has all the derivatives that appear on the equation, and satisfies the
equation everywhere in R. For example, u = x
2
y
2
, u = e
x
cos(y), and u = ln(x
2
+y
2
), are all
solutions to the two-dimensional Laplace equation (equation [3] above).
In general there should be as many boundary or initial conditions as the highest order
of the corresponding partial derivative. For example, the one dimensional heat equation
(equation [2]) applied to a insulated bar of length L, will require an initial condition, say
u(x,t=0) = f(x), 0 < x < L,
as well as two boundary conditions, e.g., u(x=0,t) = u
0
and u(x=L,t) = u
L
, or, u
x
(x=0,t) = u
x0
and u
x
(x=L) = u
xL
, or some combination of these, for t >0.
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
C Cl la as ss si if fi ic ca at ti io on n o of f l li in ne ea ar r, , s se ec co on nd d- -o or rd de er r P PD DE Es s
Linear, second-order PDEs, as the examples shown above as equations [1] through [4], are
commonly encountered in science and engineering applications. For that reason special
attention is paid in this section to this type of equations. First, we learn how to classify linear,
second-order PDEs as follows:
An equation of the form:
Au
xx
+ 2Bu
x
+ Cu
yy
= F(x,y,u,u
x
,u
y
),
is said to be:
elliptic, if AC B
2
>0, e.g., heat flow and diffusion-type problems.
parabolic, if AC B
2
= 0, e.g., vibrating systems and wave motion problems.
hyperbolic, if AC B
2
< 0, e.g., steady-state, potential-type problems.
Analytical solutions of PDEs
There are a variety of methods for obtaining symbolic, or closed-form, solutions to differential
equations. The method of separation of variables can be used to obtain analytical solutions
for some simple PDEs. The method consists in writing the general solution as the product of
functions of a single variable, then replacing the resulting function into the PDE, and
separating the PDE into ODEs of a single variable each. The ODEs are solved separately and
their solutions combined into the solution of the PDE.
In many cases, the ODEs resulting from the separation of variables produce solutions that
depend on a parameter known as an eigenvalue (if the eigenvalue appears in a sine or cosine
function that depends on time, it is referred to as an eigenfrequency). The solutions involving
eigenvalues are known as eigenfunctions.
A An na al ly yt ti ic ca al l s so ol lu ut ti io on ns s t to o p pa ar ra ab bo ol li ic c e eq qu ua at ti io on ns s: : o on ne e- -d di im me en ns si io on na al l
s so ol lu ut ti io on n o of f t th he e h he ea at t e eq qu ua at ti io on n
The flow of heat in a thin, laterally insulated homogeneous rod is modeled by
u/t = k(
2
u/x
2
),
where u = temperature, k = a parameter resulting from combining thermal conductivity and
density. The PDE is subject to the initial condition
u(x,0) = f(x)
and constant-value boundary conditions
u(0,t) = u
0
, and u(L,t) = u
L
.
The physical phenomenon described by this PDE and its initial and boundary conditions is
illustrated in the figure below with u
0
= u
L
= 0.
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
We will try to find a solution by the method of separation of variables. This method assumes
that the solution, u(x,t), can be expressed as the product of two functions, X(x) and T(t):
u(x,t) = X(x)T(t).
With this substitution, the initial condition, u(x,t=0) = f(x) = X(x)T(t), can be treated as the set
of conditions: X(x) = f(x), when t = 0 [i.e., T(t) = 1]. Also, the boundary conditions, u(0,t) =
X(0)T(t) = u
0
, and u(L,t) = X(L)T(t) = u
L
, can be treated as X(0) = u
0
, and X(L) = u
L
, as long as
T(t) 0.
The derivatives of u(x,t) are calculated as follows:
and
Replacing these derivatives in the heat equation we get:

2
x
2
( ) X x ( ) T t

t
( ) u , x t ( ) X x

t
( ) T t
= ( ) X x

t
( ) T t k

2
x
2
( ) X x ( ) T t

2
x
2
( ) u , x t
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
Dividing by u(x,t) = X(x)T(t):
This result is only possible if both sides of the equation are equal to a constant, say -, since
the left-hand side is only a function of t and the right hand side is only function of x. The left-
hand side of the heat equation produces an ODE with independent variable t:
Whose solution is:
On the other hand, the right-hand side of the heat equation produces an ODE with independent
variable is x:
A general solution for X(x) is:
Next, we replace the boundary condition X(0) = 0, which results in the equation 0 = _C2, or
_C2 = 0. With this result, the solution simplifies to:
= ( ) T t e
( ) t
= ( ) X x _C1

sin

k
x
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
The second boundary condition, X(L) = 0, produces:
The latter result indicates an eigenfunction problem. We need to find all possible values of
for which this equation is satisfied. Since we want _C1 0, then we set
This equation has multiple solutions located at,
! ! , 3 , 2 , , 0 , , 2 , 3

=
k
L
i.e.,
or,
Therefore, with these values of the solution for X(x) now becomes:
=

sin L

k
0
:=
n
2

2
k
L
2
= ( ) X x _C1

sin
n
2

2
L
2
x
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
The value of _C1 remains somewhat arbitrary, requiring a different approach to find it. To
simplify notation we will replace _C1 with b
n
:
With the value of found earlier, the solution for T(t) is now:
There will be a different expression for u(x,t) = X(t)T(t) for each value of n = 0 , 1, 2, 3, ....
Therefore, we will call the solution corresponding to a particular value of n u
n
(x,t) and write:
The form of the n-th solution, u
n
, suggests an expansion similar to a Fourier series expansion
for the overall solution, u(x,t), restricting the values of n to positive integers, i.e.,
. exp sin ) , ( ) , (
2
2 2
1 1

= =

=
L
kt n
L
x n
b y x u y x u
n
n
n
n n

with the values of b
n
obtained from the boundary condition, u(x,0) = f(x), i.e.,
The latter result is a Fourier sine series with the coefficients b
n
given by:
= ( ) X x b
n

sin
n
2

2
L
2
x
= ( ) T t e

n
2

2
k t
L
2
=
= n 1

sin
n x
L
b
n
( ) f x
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
Example 1 Determine the solution for the one-dimensional heat equation subjected to u(0,t)
= u(L,t) = 0, if the initial conditions are given by u(x,0) = f(x) = 4(x/L) (1-x/L)
.
Use values of
k=1 and L=1.
-->deff('[w]=f(x)','w=4*(x/L)*(1-x/L)')//Define initial conditions, u(x,0)=f(x)
-->deff('[w]=g(x)','w=f(x)*sin(n*%pi*x/L)')//Integrand for Fourier coefficients
-->deff('[bb]=b(n)','bb=intg(0,L,g,0.001)') //Define Fourier coefficients
-->L=1;k=1;
-->bb=[];for n=1:40, bb=[bb b(n)]; end; //Calculate Fourier coefficients
Next, we define the function u(x,t), and calculate values of u(x,t) in the ranges 0<x<1,
0<t<0.25, which are plotted in three dimensions:
-->deff('[uu]=u(x,t)',['uu=0';'for j=1:40';...
-->'uu=uu+bb(j)*sin(j*%pi*x/L)*exp(-j^2*%pi^2*k*t/L^2)';'end'])
-->xx=[0:0.05:1];tt=[0:0.025:0.25];
-->uu=feval(xx,tt,u);
-->plot3d(xx,tt,uu,45,45,'x@t@u(x,t)')
Plots of the functions u(x,t
0
) = f
0
(x), for specific values of t (i.e., t = t
0
) are shown in the
following plot:
-->plot2d([xx' xx' xx' xx' xx'],[uu(:,1),uu(:,3),uu(:,6),uu(:,9),uu(:,11)],...
-->[1:1:5],'111','t=0@t=0.075@t=0.130@t=0.195@t=0.25',[0,0,1,0.5])
-->plot2d([xx' xx' xx' xx' xx'],[uu(:,1),uu(:,3),uu(:,6),uu(:,9),uu(:,11)],...
-->[-1:-1:-5],'111','t=0@t=0.075@t=0.130@t=0.195@t=0.25',[0,0,1,0.5])
-->xtitle('Heat equation solution','x','u(x,t)')
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
A An na al ly yt ti ic ca al l s so ol lu ut ti io on ns s t to o p pa ar ra ab bo ol li ic c e eq qu ua at ti io on ns s: : O On ne e- -d di im me en ns si io on na al l s so ol lu ut ti io on n o of f
t th he e w wa av ve e e eq qu ua at ti io on n
The wave equation, shown below, can be used to model the displacement of an elastic string or
the longitudinal vibration of a beam:
For the case of an elastic string,
= c
2
T

, where T is the constant tension in the string and

is
the mass per unit length of the string. For the case of longitudinal vibration of a beam,
= c
2
g E

, where g is the acceleration of gravity, E is the modulus of elasticity, and

is the
density of the beam.
Suppose we solve the wave equation for a vibrating string of length L using separation of
variables with the boundary conditions u(0,t) = u(L,0)= 0. Also, the initial shape of the string is
given by u(x,0) = f(x), and the initial speed of the string is given by
=

t
( ) u , x t ( ) g x
at
= t 0
. We
postulate a solution of the form u(x,t) = X(x)T(t), and replace this result in the original PDE:
= ( ) X x

2
t
2
( ) T t c
2

2
x
2
( ) X x ( ) T t
Dividing both sides of the equation by u(x,t), we get:
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
This equation is only possible if the two sides of the equations are equal to a constant, say,

2
. With this we can write two ODEs, one for each side of the equation WaveEqn1, i.e.,
The solutions to these equations are:
Note: the constants _C1 and _C2 in the two solutions are not the same. The boundary
conditions u(0,t) = u(L,t) = 0 translate into X(0) = X(L) = 0. With these boundary conditions, we
can form the following algebraic equations:
:= _C2 0
We have an eigenvalue equation given by
=

sin
L
c
0
. The solution to this equation is
L
c
=
0,

,
2
,..., or

= 0,
c
L
,
2 c
L
,.... Using only the positive values, we can write
=
n c
L
,
n
= 0,1,2,.... Next, using the value of

and _C2 = 1, we obtain an expression for X(x) as


follows:
:=
n c
L
:= Sol2 = ( ) X x

sin
n x
L
Function T(t) gets written as:
= ( ) T t + _C1

cos
n c t
L
_C2

sin
n c t
L
The function u(x,t) is now:
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
Application of the initial conditions provide the following equations:
Since the last two equations need to apply for n = 0,1,2, ..., we recognize in them the
equations that define Fourier series if we use _C1 = a
n
, and _C2 = b
n
, i.e.,
= ( ) f x a
n

sin
n x
L
= ( ) g x

sin
n x
L
b
n
n c
L
The equations defining coefficients a
n
and b
n
are given by:
a
n
=
2
L
d

0
L
( ) f x

sin
n x
L
x , and b
n
=
2
n c
d

0
L
( ) g x

sin
n x
L
x , for
n
=
, , 1 2 3
,...
The final solution is, therefore,
Example 1 - Consider the case of a vibrating string with the initial displacement given by f(x)
=
x
L
(
1
x
L
), and the initial velocity given by g(x) =

x
L
2
(
1
x
L
). The boundary conditions
are u(0,t) = 0, u(L,t) = 0. Determine the solution u(x,t) for this problem using components of
the resulting Fourier series for n = 1, 2, ..., 20, if c = 1 and L = 1.
The first three definitions correspond to function f(x), the integrand for a
n
, and a
n
itself. The
next three definitions correspond to function g(x), the integrand for b
n
, and b
n
itself.
-->deff('[w]=f(x)','w=(x/L)*(1-x/L)')
-->deff('[w]=ff(x)','w=f(x)*sin(n*%pi*x/L)')
-->deff('[aa]=a(n)','aa=(2/L)*intg(0,L,ff,0.001)')
-->deff('[w]=g(x)','w=(x/L)^2*(1-x/L)')
-->deff('[w]=gg(x)','w=g(x)*sin(n*%pi*x/L)')
-->deff('[bb]=b(n)','bb=(2/(n*%pi*c))*intg(0,L,gg,0.001)')
Next, we define the values of L and c and calculate the coefficients a
n
and b
n
:
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
-->L=1;c=1;
-->aa=[];for n=1:20, aa=[aa a(n)]; end;
-->bb=[];for n=1:20, bb=[bb b(n)]; end;
The next step is to define the function u(x,t) and calculate values of the same for x in the
range 0<x<1 and t in the range 0<t<4. A three dimensional plot of u(x,t) is shown next:
-->deff('[uu]=u(x,t)',['uu=0';'for n=1:20';
-->'uu=uu+sin(n*%pi*x/L)*(aa(n)*cos(n*%pi*c*t/L)+bb(n)*sin(n*%pi*c*t/L))';
-->'end'])
-->x=[0:0.05:1];t=[0:0.1:4];
-->uu=feval(x,t,u);
-->plot3d(x,t,uu,45,45,'x@t@u(x,t)')
An alternative way to present the result is to plot u(x,t
0
) vs. x for selected values of t
o
as shown
in the next SCILAB commands:
-->plot2d([x' x' x' x' x'],[uu(:,1),uu(:,5),uu(:,10),uu(:,15),uu(:,20)],...
-->[-1:-1:-5],'111','t=0.05@t=0.25@t=0.50@t=0.75@t=1.0',[0 -0.3 1 0.3])
-->plot2d([x' x' x' x' x'],[uu(:,1),uu(:,5),uu(:,10),uu(:,15),uu(:,20)],...
-->[1:1:5],'011','t=0.05@t=0.25@t=0.50@t=0.75@t=1.0',[0 -0.3 1 0.3])
-->xtitle('Wave equation solution','x','u(x,t)')
The plot thus generated is shown below:
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
A An na al ly yt ti ic ca al l s so ol lu ut ti io on ns s t to o h hy yp pe er rb bo ol li ic c e eq qu ua at ti io on ns s: : T Tw wo o- -d di im me en ns si io on na al l
s so ol lu ut ti io on n t to o L La ap pl la ac ce e' 's s e eq qu ua at ti io on n i in n a a r re ec ct ta an ng gu ul la ar r d do om ma ai in n. .
Laplace's equation in two-dimensions is given by
= +

2
x
2
( ) u , x y

2
y
2
( ) u , x y 0 .
In problems related to heat transfer, the two-dimensional Laplace equation describes the
steady state distribution of temperature u(x,y) in the x-y plane. In fluid mechanics, u(x,y)
could describe the velocity potential or the streamfunction for a two-dimensional potential
flow. The problem requires two boundary conditions in the two independent variables x and
y.
Laplace equation is solved in a rectangular domain so that 0<x<L, 0<y<H, a suitable set of
boundary conditions may be

= ( ) u , x 0 0
,
= ( ) u , x H ( ) g x
,
( ) u , 0 y
=
0
,
( ) u , L y
=
0
.
as illustrated in the figure below:
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
Separation of variables suggests that we use a solution of the form,
= ( ) u , x y ( ) X x ( ) Y y
.
Solving the equation through separation of variables proceeds in the following fashion:
=

2
x
2
( ) X x ( ) Y y ( ) X x

2
y
2
( ) Y y
Dividing by u(x,y) = X(x)Y(y) results in:
For the two sides of the resulting equation to be equal, they both must be equal to a constant,

2
, resulting in the following two ordinary differential equations:
The solution to the first ODE is:
We next use the boundary conditions:
= ( ) X 0 0
, and X(L) =
0
, to determine the constants of
integration:
_C2 = 0
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
This second equation produces an eigenvalue equation with the eigenvalues given by
=
n
L
.
:=
n
L
The solution for X(x), with _C1 = 1 (since the value _C1 is arbitrary), is, therefore:
= ( ) X x

cos
n x
L
The solution to the second ODE is:
Utilizing the boundary condition: Y(0) = 0, we find for Y(y):
:= _C3 0
= ( ) Y y _C4

sinh
n y
L
The product X(x)Y(y), which now depends on the value of the eigenvalue
=
n
n
L
, is referred
to as = ( ) v
n
, x y ( ) X x ( ) Y y , with the constant replaced by a
n
:
The solution will be the sum of all possible functions ( ) v
n
, x y , i.e.,
:= u ( ) , x y
= n 1

( ) v
n
, x y
The expression for the function
( ) u , x y
is:
If we now evaluate the boundary condition,
= ( ) u , x H ( ) g x
, we find the following equation:
This result is a Fourier series expansion to
( ) g x
, such that the constants a
n
are calculated by
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
Example 1: Suppose that the dimensions of the solution domain are L = 2 and H = 1, and the
boundary condition at y = H is given by
= ( ) g x 100

x
(
L x
)
3
.
We start by defining function g(x), the integrand for the Fourier series coefficients, gg(x), and
the Fourier series coefficients themselves, a(n).
-->deff('[w]=g(x)','w=100*x*(L-x)^3')
-->deff('[w]=gg(x)','w=g(x)*sin(n*%pi*x/L)')
-->deff('[aa]=a(n)','aa=2*intg(0,L,gg,0.0001)/(L*sinh(n*%pi*H/L))')
Next, we define the values of L and H, calculate 20 Fourier coefficients, and define and
evaluate the u(x,y) in the ranges 0<x<L, 0<y<H. A three-dimensional plot of the function is
shown:
-->L=2;H=1;
-->aa=[];for n=1:20, aa=[aa a(n)]; end;
-->deff('[uu]=u(x,y)',['uu=0';'for n=1:20';...
-->'uu=uu+aa(n)*sin(n*%pi*x/L)*sinh(n*%pi*y/L)';'end'])
-->x=[0:L/20:L];y=[0:H/20:H];
-->uu=feval(x,y,u);
-->plot3d(x,y,uu,45,45,'x@y@u(x,y)')
Solutions to Laplaces equation in two-dimensions can also be represented by contour plots as
shown below:
-->contour(x,y,uu,15)
-->xtitle('Contour plots for Laplace equation solution','x','y')
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
More solutions to Laplace equation in a rectangular domain
The solution obtained above was facilitated by the use of zero boundary conditions in three of
the boundaries. The zero boundary conditions at x = 0 and x = L produced the eigenvalues
=
n
n
L
,
while the zero boundary condition at y = 0 produced the series solution
= ( ) u , x y
= n 1

a
n

sin
n x
L

sinh
n y
L
.
Finally, the non-zero boundary condition at y = H, produced the coefficients a
n
for the
corresponding Fourier series. The case solved above is referred to as Case [1] in the figure
below.
Solutions to the case of a rectangular domain where only one of the boundaries is non-zero can
be found for cases [2] through [4] in the figure below using a procedure similar to that outlined
above for case [1].
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
Because Laplace's equation is a linear equation, i.e., , solutions
with different boundary conditions, in the same domain, can be superimposed. Therefore,
linear combinations of the solutions for the four cases illustrated in the figure above can be
added to solve problems involving non-zero boundary conditions in more than one boundary.
Suppose that ( ) u
1
, x y , ( ) u
2
, x y , ( ) u
3
, x y , and ( ) u
4
, x y , represent the solution for cases
[1],[2],[3], and [4], respectively, in the figure above. The most general problem in the
rectangular domain will use the following non-zero boundary conditions:
= ( ) u , x 0
1
( ) g x , = ( ) u , x H
2
( ) f x , = ( ) u , 0 y
3
( ) p y , and = ( ) u , L y
4
( ) q y .
The solution will be obtained as
= ( ) u , x y + + +
1
( ) u
1
, x y
2
( ) u
2
, x y
3
( ) u
3
, x y
4
( ) u
4
, x y .
Solution to case [2]
To solve case [2] in the figure above, we start by using separation of variables, u(x,y) =
X(x)Y(y), on the Laplace equation:
=

2
x
2
( ) X x ( ) Y y ( ) X x

2
y
2
( ) Y y
Dividing by u(x,y) = X(x)Y(y) results in:
For the two sides of the resulting equation to be equal, they both must be equal to a constant,

2
, resulting in the following two ordinary differential equations:
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
The solution to the first ODE is:
We next use the boundary conditions:
= ( ) X 0 0
, and X(L) =
0
, to determine the constants of
integration:
0 = _C2
0 = _C1 sin(x)
This second equation produces an eigenvalue equation with the eigenvalues given by
=
n
L
.
The solution for X(x), with _C2 = 1 (since the value _C2 is arbitrary), is, therefore:
= ( ) X x

cos
n x
L
The solution to the second ODE is:
Utilizing the boundary condition: Y(H) = 0, we find for Y(y):
or
:= _C4
_C3

cosh
n H
L

sinh
n H
L
= ( ) Y y
_C3

cosh
n y
L

sinh
n H
L

cosh
n H
L

sinh
n y
L

sinh
n H
L
The product X(x)Y(y), which now depends on the value of the eigenvalue
=
n
n
L
, is referred
to as = ( ) v
n
, x y ( ) X x ( ) Y y , with the constant replaced by a
n
:
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
The solution will be the sum of all possible functions ( ) v
n
, x y , i.e.,
The expression for the function
( ) u , x y
is:
If we now evaluate the boundary condition,
= ( ) u , x 0 ( ) g x
, we find the following equation:
This result is a Fourier series expansion to
( ) f x
, such that the constants a
n
are calculated by
Example 1: Suppose that the dimensions of the solution domain are L = 2 and H = 1, and the
boundary condition at y = H is given by
= ( ) f x 100

x
(
L x
)
3
.
-->L=2;H=1;
-->deff('[w]=f(x)','w=100*x*(L-x)^3') //Function f(x)
-->deff('[w]=ff(x)','w=f(x)*sin(n*%pi*x/L)') //Integrand Fourier series coeff.
-->deff('[aa]=a(n)','aa=(2/L)*intg(0,L,ff,0.0001)') //Fourier coeff. defined
-->aa=[]; for n=1:20, aa=[aa a(n)]; end; //Calculate Fourier coeff.
The following statements define the solution u(x,y):
-->deff('[uu]=u(x,y)',['uu=0';'for n=1:20';
-->'uu=uu+aa(n)*sin(n*%pi*x/L)/sinh(n*%pi*H/L)*...
-->(cosh(n*%pi*y/L)*sinh(n*%pi*H/L)-cosh(n*%pi*H/L)*sinh(n*%pi*y/L))';
-->'end'])
To produce a three-dimensional plot of the solution we use:
-->x=[0:L/20:L];y=[0:H/20:H];uu=feval(x,y,u);
-->plot3d(x,y,uu,45,45,'x@y@u(x,y)')
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
The solution can also be shown as a contour plot.
-->contour(x,y,uu,15)
-->xtitle('Laplace equation - case 2','x','y')
Superposition of solutions for cases [1] and [2]
The superposition of the solutions for cases [1] and [2] will be the solution to the problem
whose boundary conditions are illustrated in the figure below:
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
We use for the solution
= ( ) f x x ( ) L x
3
, and
= ( ) g x x
3
( ) L x
. The following SCILAB statements
produce the solution to this problem.
-->deff('[w]=f(x)','w=x*(L-x)^3')
-->deff('[w]=g(x)','w=x^3*(L-x)')
-->deff('[w]=ff(x)','w=f(x)*sin(n*%pi*x/L)')
-->deff('[w]=gg(x)','w=g(x)*sin(n*%pi*x/L)')
-->deff('[aa1]=a1(n)','aa1=2*intg(0,L,gg,0.0001)/(L*sinh(n*%pi*H/L))')
-->deff('[aa2]=a2(n)','aa2=(2/L)*intg(0,L,ff,0.0001)')
-->L=2;H=1;
-->aa1=[]; for n=1:20, aa1=[aa1 a1(n)]; end;
-->aa2=[]; for n=1:20, aa2=[aa2 a2(n)]; end;
-->deff('[uu1]=u1(x,y)',['uu1=0';'for n=1:20';...
-->'uu1=uu1+aa1(n)*sin(n*%pi*x/L)*sinh(n*%pi*y/L)';'end'])
-->deff('[uu2]=u2(x,y)',['uu2=0';'for n=1:20';...
-->'uu2=uu2+aa2(n)*sin(n*%pi*x/L)/sinh(n*%pi*H/L)*...
-->(cosh(n*%pi*y/L)*sinh(n*%pi*H/L)-cosh(n*%pi*H/L)*sinh(n*%pi*y/L))';...
-->'end'])
-->x=[0:L/20:L];y=[0:H/20:H];
-->uu1=feval(x,y,u1);uu2=feval(x,y,u2);uu=uu1+uu2;
-->plot3d(x,y,uu,45,45,'x@y@u(x,y)')
-->contour(x,y,uu,15)
-->xtitle('Laplace equation - cases 1 & 2','x','y')
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
Suppose now, that the boundary conditions are such that = ( ) f
1
x 3 ( ) f x =
3 x ( ) L x
3
and
= ( ) g
1
x 5 ( ) g x =
5 x
3
( ) L x
. Since we already have the solutions corresponding to f(x) and g(x),
we can obtain the solution to the problem with the new set of boundary conditions by
constructing the following solution = ( ) u , x y + 3 ( ) u
1
, x y 5 ( ) u
2
, x y :
-->uu=3*uu1+5*uu2;
The three-dimensional plot corresponding to this solution is shown below:
-->plot3d(x,y,uu,45,45,'x@y@u(x,y)')
Contour plots of the solution are produced by using:
-->contour(x,y,uu,15)
-->xtitle('Laplace equation - cases 1 & 2','x','y')
Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz
Laplacian in polar-cylindrical coordinates
Consider the function
= u ( ) u , , r z
in polar coordinates. The Laplacian of this function in polar
coordinates can be obtained from Maple using:
+ + +

r
( ) u , , r z r

2
r
2
( ) u , , r z

2
( ) u , , r z
r
r

2
z
2
( ) u , , r z
r
You can check that the first two terms are equivalent to

r
r

r
( ) u , , r z
, i.e.,
+

r
( ) u , , r z r

2
r
2
( ) u , , r z
Thus, the Laplacian of u(x,y,z) can be written as:
=
1
r
+

r
r

r
( ) u , , r z
1
r
2

2
( ) u , , r z +

2
z
2
( ) u , , r z
In the next section we will discuss the solution of the Laplace equation in two dimensions using
polar-cylindrical coordinates, the corresponding Laplacian, taking
= u ( ) u , r
and
=

z
u 0
, is
=
1
r
+

r
r

r
( ) u , r
1
r
2

2
( ) u , r
Solution to Laplace's equation in an annulus
Consider the domain of solution to Laplace equation to be an annulus defined, in polar
coordinates, as r
1
<
r
< r
2
, and 0 <

<
2
, subjected to the boundary conditions:
= ( ) u , r
1
u
1
= constant, and = ( ) u , r
2
u
2
= constant, as illustrated in the figure below:
Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz
In polar coordinates, Laplace's equation is written as:
1
r
+

r
r

r
( ) u , r
1
r
2

2
( ) u , r = 0.
If the solution is assumed to depend only on the radial coordinate,
= u ( ) u r
, and
=

u 0
.
Thus, Laplace's equation reduces to
1
r

r
r

r
( ) u r
= 0. The later result represents an
ordinary differential equation and we could write it as
1
r

d
dr
(
r

du
dr
) = 0,
or
d
dr
(
r

du
dr
) = 0.
Integration of this equation produces: u(r) = _C1 + _C2 ln(r).
Using the boundary conditions indicated above we obtain:
.
ln
) ln( ) ( ) ln( ) ln(
) (
2
1
1 2 2 1 1 2


=
r
r
r u u r u r u
r u
For example, if u
1
=1, r
1
=1, u
2
= 10, r
2
= 5, the distribution of u(r) is illustrated in the following
figure.
-->u1=1;r1=1;u2=10;r2=5;
-->deff('[uu]=u(r)','uu=(u2*log(r1)-u1*log(r2)-(u2-u1)*log(r))/log(r1/r2)')
-->r=[r1:(r2-r1)/100:r2];uu=u(r);
-->plot(r,uu,'r','u(r)','Laplace equation in a disk')
Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
__________________________________________________________________________________
Three-dimensional surface plot in polar coordinates
To produce a three-dimensional surface plot of a function z = f(r,) we propose the following
function, plotpolar. The call to the function is
plotpolar(r_range,theta_range,nx,ny,f)
where r_range is the vector [rmin,rmax], theta_range is the vector [theta_min, theta_max]
where the function f is defined. Since the function uses function plot3d to produce the surface
plot, we need to produce a rectangular x-y grid. The number of divisions in the x and y
directions are given by the numbers nx and ny, respectively. The function returns two plots, a
three-dimensional surface plot and a contour plot of the function f. A listing of function
polarplot follows:
function [] = plotpolar(rrange,trange,n,m,f)
//A function to produce a three-dimensional
//plot out of a function z = f(r,theta)
xmin=-rrange(2);xmax=+rrange(2);xrange=xmax-xmin;
ymin=-rrange(2);ymax=+rrange(2);yrange=ymax-ymin;
xx=[xmin:xrange/n:xmax];
yy=[ymin:yrange/m:ymax];
nx=length(xx);ny=length(yy);
zz=zeros(nx,ny);
for i = 1:nx
for j = 1:ny
rr=norm([xx(i) yy(j)]);
if xx(i) == 0 & yy(j) == 0 then
th = 0.0
else
th=atan(xx(i),yy(j));
end;
if rr>=rrange(1) & rr<=rrange(2) &...
th>=trange(1) & th<=trange(2) then
zz(i,j) = f(rr,th);
end;
end;
end;
Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz
xset('window',1);xbasc();plot3d(xx,yy,zz);
xset('window',2);xbasc();contour(xx,yy,zz,10);
The range of values for both x and y is [-rmax,rmax]. To obtain a full surface plot of the
function f use theta_range = [-%pi,%pi]. To obtain half-a-plot use theta_range=[0,%pi].
The function f must be defined as a function of both r and , even if it depends only on one of
the variables. Thus, for the solution of the Laplace equation in an annulus obtained above, we
will re-define the function u as shown below.
-->u1=1;r1=1;u2=10;r2=5;
-->deff('[uu]=u(r,t)','uu=(u2*log(r1)-u1*log(r2)-(u2-u1)*log(r))/log(r1/r2)')
To produce a half-plot of the function u(r) we use:
-->r_range = [r1,r2]; theta_range = [0,%pi];
-->plotpolar(r_range,theta_range,40,40,u)
Next, we redefine the range of to produce a full plot of the function:
-->r_range = [r1,r2]; theta_range = [-%pi,%pi];
Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz
-->plotpolar(r_range,theta_range,40,40,u)
__________________________________________________________________________________
Solution to the Laplace equation in a disk
Consider the case in which
= u ( ) u , r
as solution to the Laplace equation on a disk centered at
the origin and subjected to the boundary condition,
= ( ) u , R ( ) f
, where R is the radius of the
disk. Using separation of variables we can write
= ( ) u , r ( ) R r ( ) T
, requiring that
( ) T
be
periodic in

with period
2
. Thus, we can write
( ) T
as a Fourier series, i.e.,
( ) T
= +
a
0
2

= k 1

( ) + a
k
( ) cos k b
k
( ) sin k .
The procedure for the solution using separation of variables proceeds as follows. The Laplace
equation in cylindrical coordinates is written as:
To separate the equation into ordinary differential equations we use u(r, ) = R(r)T():
Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz
The differential equations are:
Since the expression
( ) ln r
is equivalent to
( ) ln r

, we can re-write R(r) as:


+ _C1


1
2
r

1
2
1
r

_C2

+
1
2
r

1
2
1
r

Or, redefining the constants,


Typically, the values of

are positive values, therefore, the term


r
( )
grows unbounded as
r 0
. This requires that we eliminate the term
r
( )
from the solution by making _C2 = 0,
thus:
= ( ) R r _C1 r

The second ODE obtained from separation of variables and its solution are:
The latter result indicates that the initial assumption of a periodic function for
( ) T
is
acceptable if we take
= k
. We now replace the solution to the second ODE with:
The combined solution,
( ) u , r
=
( ) R r ( ) T
, is now:
The boundary condition,
= ( ) u , R ( ) f
, produces:
With the coefficients of the resulting Fourier series given by:

= a
0
1

0
2
( ) f
, a
k
=
1
R
k

d

0
2
( ) f ( ) cos k
, and b
k
=
1
R
k

d

0
2
( ) f ( ) sin k
.
Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz
For example, if R = 10, and
= ( ) f + ( ) sin 2.5 ( ) sin 2 1.5 ( ) cos 3
, we can obtain the solution
u(r,) using the following SCILAB commands:
-->deff('[w]=f(t)','w=sin(t)-2.5*sin(2*t)+1.5*cos(3*t)') //define f(theta)
-->R = 10;
-->a0=(1/%pi)*intg(0,2*%pi,f,0.0001) //Calculate a0
a0 = - 1.414E-16
-->deff('[w]=fc(t)','w=f(t)*cos(k*t)') //Integrand for cosine coefficients
-->deff('[w]=fs(t)','w=f(t)*sin(k*t)') //Integrand for sine coefficients
-->deff('[aa]=a(k)','aa=intg(0,2*%pi,fc,0.0001)/(%pi*R^k)')//cosine coeff. def
-->deff('[bb]=b(k)','bb=intg(0,2*%pi,fs,0.0001)/(%pi*R^k)')//sine coeff. def.
-->aa=[];for k=1:20, aa=[aa a(k)]; end; aa = clean(aa) //cosine coeff.
aa =
column 1 to 11
! 0. 0. .0015 0. 0. 0. 0. 0. 0. 0. 0. !
column 12 to 20
! 0. 0. 0. 0. 0. 0. 0. 0. 0. !
-->bb=[];for k=1:20, bb=[bb b(k)]; end; bb = clean(bb) //sine coeff.
bb =
column 1 to 11
! .1 - .025 0. 0. 0. 0. 0. 0. 0. 0. 0. !
column 12 to 20
! 0. 0. 0. 0. 0. 0. 0. 0. 0. !
Next, we define the function u(r,), and produce a plot of the solution using function
plotpolar, first on an x-y grid of 30 30 and then on a 40 40 grid.
-->deff('[uu]=u(r,t)',['uu=a0/2';'for k=1:20';
-->'uu=uu+r^k*(aa(k)*cos(k*t)+bb(k)*sin(k*t))';'end'])
-->r_range=[0,R]; theta_range=[-%pi,%pi];
-->getf('plotpolar')
-->plotpolar(r_range,theta_range,30,30,u)
Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz
-->plotpolar(r_range,theta_range,40,40,u)
A contour plot of the function u(r,) on a 40 40 x-y grid is shown next.
Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz
Numerical solutions to partial differential equations
The numerical solution to partial differential equations is a subject that may fill several
volumes. In this section we present some simple numerical solutions to elliptic, parabolic, and
hyperbolic partial differential equations. The examples presented herein are aimed at
demonstrating the use of SCILAB in simple numerical solutions to PDEs.
Numerical solution to parabolic PDEs: heat equation
The unsteady heat equation in one-dimension is given by
u/t = K(
2
u/x
2
),
for 0<x<L, t>0. In this equation u(x,t) represents the temperature on a uniform bar at location
x and at time t, and K is a constant containing information on the heat transfer ability of the
material under consideration. The solution to this equation requires one initial condition,
u(x,0)=f(x), and two boundary conditions, e.g., u(0,t) = u
0
(t), u(L,t) = u
L
(t).
Replacing the second-order partial derivatives
2
u/x
2
with a centered finite difference
approximation and the first-order partial derivatives with a forward difference approximation
results in
Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz
,
) (
) 2 (
2
, 1 , 1 1 ,
x
u u u K
t
u u
j i ij j i ij j i

+
=

+ +
or,
u
i,j+1
= u
ij
+(u
i-1,j
-2u
ij
+u
i+1,j
),
for i=1,2,,n, and j=1,2,, with
= Kt/(x)
2
.
The result shown above for u
i,j+1
= u(x
i
,t
j+1
) is an explicit solution for the temperature. Stability
conditions for this explicit solution requires that < 0.5.
To produce a solution we use a solution grid as shown below:
We load the initial conditions at t=0 and the boundary conditions at x = 0 and x = L for any time
t > 0, and proceed to calculate u
i,j+1
= u
ij
+(u
i-1,j
-2u
ij
+u
i+1,j
), for i=2,3,,n-1, and j=2,3,. The
result is a matrix u such that u
ij
= u(x
i
,t
j
).
A A f fu un nc ct ti io on n f fo or r t th he e e ex xp pl li ic ci it t n nu um me er ri ic ca al l s so ol lu ut ti io on n o of f t th he e h he ea at t e eq qu ua at ti io on n
The following function, HeatPDE1, produces the explicit solution to the heat equation. The
call to the function is
[u]=HeatPDE1(xrange,trange,uinit,u0bound,uLbound,K)
where xrange is a vector of length n containing the range of values of x uniformly distributed,
i.e., xrange = [x1:Dx:xn], trange is a vector of length m containing the range of values of t,
also uniformly distributed, i.e., trange = [t0:Dt:tm]. The vector uinit is a vector of length n
containing the initial conditions for u(x,t). Vectors u0bound and uLbound are vectors of length
m containing the values of the lower and upper boundary conditions for the different times t in
Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz
trange. The value K is the constant from the heat equation. The function return the matrix u
of size nxm representing the solution of the heat equation.
function [u]=HeatPDE1(xrange,trange,uinit,u0bound,uLbound,K)
n=length(xrange);
m = length(trange);
u = zeros(n,m);
Dx = (xrange(n)-xrange(1))/(n-1);
Dt = (trange(m)-trange(1))/(m-1);
u(:,1)=uinit';
u(1,:)=u0bound;
u(n,:)=uLbound;
alpha = K*Dt/(Dx)^2;
if alpha >= 0.5 then
printf('WARNING: The value of alpha is larger than or equal to 0.5.');
printf('The explicit solution is unstable and it will not be calculated.');
abort;
end;
for j = 1:m-1
for i = 2:n-1
u(i,j+1) = u(i,j)+alpha*(u(i-1,j)-2*u(i,j)+u(i+1,j));
end;
end;
Example of application of function HeatPDE1
We will solve the heat equation using an x range of [0:0.1:1] with a t range [0:0.005:1.5] and K
= 1.0. The initial conditions will be simply zero values, while the boundary conditions will be
u(0,t) = exp(-2t)sin(50t) and u(1,t) = exp(-3t)cos(50t).
-->K=1.0; //Heat eqn. constant
-->x=[0:0.1:1];t=[0:0.005:1.5];ui=zeros(x); //x,t,initial conditions
-->u0=exp(-2*t).*sin(50*t);uL=exp(-3*t).*cos(50*t); //boundary conditions
-->[u]=HeatPDE1(x,t,ui,u0,uL,K); //Solution u(x,t)
-->plot3d(x,t,u,45,45,'x@t@u(x,t)') //Three-dim. plot
-->xtitle('Heat equation solution')
Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz
Animation of the results can be produced by using the following SCILAB commands. The
animation presents the values of u(x,t) as function of x as time increases.
-->xset('pixmap',1);xbasc();
-->for j = 1:301,plot2d(x',u(:,j),1,'011',' ',[0 -1 1 1]),...
-->xset('wshow'),xset('wwpc'),end
This animation may run too fast to appreciate the details of the temperature variation. To
slow down the animation include an empty loop inside the main loop, as shown next.
-->for j = 1:301,plot2d(x',u(:,j),1,'011',' ',[0 -1 1 1]), for k=1:1000,end,
-->xset('wshow'),xset('wwpc'),end
The reader is invited to try the animations in his or her own SCILAB application.
Numerical solution to hyperbolic PDEs: the wave equation
The wave equation is given by

2
u/t
2
= c
2
(
2
u/x
2
),
where c is the velocity of propagation of one-dimensional waves in a medium along the x-
direction, with 0<x<L, and t>0. The equation requires for its solution two initial conditions,
typically, u(x,0) = f(x), and u/t|
t=0
= g(x), and two boundary conditions, u(0,t) = u
L
(t), and
u(L,t) = u
L
(t).
For the numerical solution of the wave equation we use a grid similar to that used in the
solution of the heat equation, with x increment x, and time increment t. The numerical
solution seeks to find the values u
ij
= u(x
i
,y
j
). The PDE is replaced by an algebraic equation
resulting of approximating the second-order derivatives with centered finite-difference
approximations:
(u
i,j-1
-2u
ij
+u
i,j+1
)/(t)
2
= c
2
(u
i-1,j
-2u
ij
+u
i+1,j
)/(t)
2
.
Defining a parameter

2
= c
2
(t)
2
/(x)
2
,
Download at InfoClearinghouse.com 36 2001 Gilberto E. Urroz
an explicit solution for u
i
,
j+1
= u(x
i
,t
j+1
) is
u
i,j+1
=
2
(u
i-1,j
+u
i+1,j
)+2(1-
2
)u
ij
- u
i,j-1
.
To introduce the initial conditions into the solution we first use the fact that u
i1
= u(x
i
,t
1
) =
g(x
i
).
The result obtained above for u
i,j+1
, when j=1 introduces the term u
i0
which is not defined:
u
i,2
=
2
(u
i-1,1
+u
i+1,1
)+2(1-
2
)u
i1
- u
i0
.
However, when we introduce the derivative initial condition, u/t|
t=0
= g(x), re-writing it in
terms of the centered finite-difference approximation,
(u
i,2
-u
i,0
)/(2t) = g(x
i
),
the term u
i0
appears again. From the last expression, we can write
u
i0
= -2tg(x
i
) + u
i2
,
which replaced in the expression for u
i2
results in
u
i2
=
2
(u
i-1,1
+u
i+1,1
)+2(1-
2
)u
i1
+ 2tg(x
i
) - u
i2
.
Solving for u
i2
we get
u
i2
=
2
(u
i-1,1
+u
i+1,1
)/2 + (1-
2
)u
i1
+ tg(x
i
) .
Thus, the latter result is used to calculate u for the first time step. For values of j=2,3,, we
calculate the solution with
u
i,j+1
=
2
(u
i-1,j
+u
i+1,j
)+2(1-
2
)u
ij
- u
i,j-1
.
A A f fu un nc ct ti io on n f fo or r t th he e s so ol lu ut ti io on n o of f t th he e w wa av ve e e eq qu ua at ti io on n
The following solution, WavePDE1, can be used to obtain the solution u(x,t) for the wave
equation. The call to the function is
[u] = WavePDE1(xrange,trange,uinit,duinit,u0bound,uLbound,c)
where xrange is a vector of length n containing the range of values of x uniformly distributed,
i.e., xrange = [x1:Dx:xn], trange is a vector of length m containing the range of values of t,
also uniformly distributed, i.e., trange = [t0:Dt:tm]. Vector uinit is a vector of length n
containing the initial conditions for u(x,t), while vector duinit is also a vector of length n
containing the derivative boundary condition, u/t|
t=0
= g(x). Vectors u0bound and uLbound
are vectors of length m containing the values of the lower and upper boundary conditions for
the different times t in trange. The value c is the constant from the wave equation. The
function return the matrix u of size nxm representing the solution of the wave equation.
function [u] = WavePDE1(xrange,trange,uinit,duinit,u0bound,uLbound,c)
n=length(xrange);
m = length(trange);
u = zeros(n,m);
Dx = (xrange(n)-xrange(1))/(n-1);
Download at InfoClearinghouse.com 37 2001 Gilberto E. Urroz
Dt = (trange(m)-trange(1))/(m-1);
u(:,1)=uinit';
u(1,:)=u0bound;
u(n,:)=uLbound;
alpha2 = c^2*Dt^2/Dx^2;
if alpha2 > 1.0 then
printf('WARNING: The value of alpha is larger than 1.0.');
printf('The explicit solution is unstable and it will not be calculated.');
abort;
end;
//first time step
for i = 2:n-1
u(i,2) = ...
alpha2*(u(i-1,1)+u(i+1,1))/2+(1-alpha2)*u(i,1)+Dt*duinit(i);
end;
//subsequent time steps
for j = 2:m-1
for i = 2:n-1
u(i,j+1)=...
alpha2*(u(i-1,j)+u(i+1,j))+2*(1-alpha2)*u(i,j)-u(i,j-1);
end;
end;
Example of application of function WavePDE1
In this example we solve the wave equation in the range 0 < x < 1, for 0 < t < 1.5, with initial
conditions f(x) = x(1-x), and g(x) = 0.2. The boundary conditions are u(0,t) = u(1,t) = 0. The
results obtained are shown as a three-dimensional surface u(x,t).
-->x=[0:0.1:1];t=[0:0.01:1.5];uinit=x.*(1-x);duinit=0.2*ones(x);
-->u0=zeros(t);uL=zeros(t);c=1;
-->u=WavePDE1(x,t,uinit,duinit,u0,uL,c);
-->plot3d(x,t,u,45,45,'x@t@u(x,t)')
-->xtitle('Wave Equation Solution')
Download at InfoClearinghouse.com 38 2001 Gilberto E. Urroz
Next, we re-calculate the solution to the wave equation for the time range 0 < t < 9 in order to
produced an animation of the function u(x,t) vs. x for different values of t. The instructions
for the animation follow. The user is invited to try the animation in his or her own SCILAB
application.
-->x=[0:0.1:1];t=[0:0.01:9];uinit=x.*(1-x);duinit=0.2*ones(x);
-->u0=zeros(t);uL=zeros(t);c=1;
-->u=WavePDE1(x,t,uinit,duinit,u0,uL,c);
-->size(u)
ans =
! 11. 901. !
-->min(u),max(u)
ans = - .2770746
ans = .2821966
-->xset('pixmap',1);xbasc();
-->for j = 1:901,plot2d(x',u(:,j),1,'011',' ',[0 -0.3 1 0.3]),...
-->xset('wshow'),xset('wwpc'),end
N Nu um me er ri ic ca al l s so ol lu ut ti io on n t to o e el ll li ip pt ti ic c P PD DE Es s: : L La ap pl la ac ce e e eq qu ua at ti io on n
Consider the solution to the two-dimensional Laplace equation

2
u/x
2
+
2
u/y
2
= 0,
in the solution region R = {x
1
<x<x
n
,y
1
<y<y
m
}, subject to the boundary conditions uy1 = u(x,y
1
) =
f
1
(x), uym = u(x,y
m
) = f
2
(x), ux1 = u(x
1
,y) = g
1
(y), uxn = u(x
n
,y) = g
2
(y), as illustrated below.
The names uy1, uym, ux1, uxn, will be used to store the boundary condition values as vectors
when using SCILAB.
Download at InfoClearinghouse.com 39 2001 Gilberto E. Urroz
To proceed with the solution we divide the solution region R into a grid defined by
x
1
= 0, x
n
= L, y
1
= 0, y
m
= H, x = (x
n
-x
1
)/(n-1), y = (y
m
-y
1
)/(m-1),
so that the grid in the x direction consists of the values [x
1
, x
2
, , x
n
] and the grid in the y
direction consists of the values [y
1
, y
2
, , y
m
]. The grid values are calculated as
x
i
= x
1
+ (i-1) x, y
j
= y
1
+ (j-1)y.
The figure below shows a sketch of the solution grid.
The original PDE gets replaced by an algebraic equation by using centered finite difference
approximations for the second order derivatives, i.e.,

2
u/x
2
(u
i+1,j
- 2u
ij
+ u
i-1,j
)/(x)
2
,
2
u/y
2
(u
i,j+1
- 2u
ij
+ u
i,j-1
)/(y)
2
.
The resulting algebraic equation is
(u
i+1,j
- 2u
ij
+ u
i-1,j
)/(x)
2
+ (u
i,j+1
- 2u
ij
+ u
i,j-1
)/(y)
2
= 0,
from which we can get an explicit expression for u
ij
:
.
] ) ( ) [( 2
) ( ) ( ) ( ) (
2 2
, 1 , 1
2
1 , 1 ,
2
y x
u u y u u x
u
j i j i j i j i
ij
+
+ + +
=
+ +
To proceed with the solution we will first load the boundary conditions in vectors uy1, uym,
ux1, uxn. Vectors uy1, uym are vectors of length n, while vectors ux1, uxn are vectors of
length m. Then, we calculate improved values for u
ij
, i=1,2,,n, and j=1,2,,m, using the
equation developed above. We will repeat the calculations for u
ij
and keep recalculating until
the values of u
ij
converge to their final values. The convergence criteria to be used will be to
ensure that |(u
ij
)
k+1
-(u
ij
)
k
| < , where is a tolerance value, and k represents the current
approximation step.
Download at InfoClearinghouse.com 40 2001 Gilberto E. Urroz
A A f fu un nc ct ti io on n t to o s so ol lv ve e L La ap pl la ac ce e e eq qu ua at ti io on n i in n a a r re ec ct ta an ng gu ul la ar r d do om ma ai in n
The following function, LaplacePDF1, calculates the solution to Laplace equation in a
rectangular domain. The general call to the function is
[u,k] = LaplacePDE1(xrange,yrange,ux1,uxn,uy1,uym,epsilon,nmax)
where xrange is a vector of n elements, i.e., xrange = [x1:Dx:xn], and yrange is a vector of m
elements, i.e., yrange = [y1:Dy:ym]. Vectors xrange and yrange define the solution grid. The
values ux1, uxn, uy1, uym, are vectors representing the boundary conditions at x = x
1
, x = x
n
, y
= y
1
, and y = y
m
, respectively. The value epsilon represents the tolerance for convergence,
and nmax represents the maximum number of iterations allowed. The function returns a nxm
matrix u, whose elements represent the solution, i.e., u
ij
=u(x
i
,y
j
), and the value k which
indicates the number of the iteration at which a solution was achieved, if any.
A listing of the function follows:
function [u,kk] = LaplacePDE1(xrange,yrange,ux1,uxn,uy1,uym,epsilon,nmax)
n=length(xrange);m=length(yrange);
Dx=(xrange(n)-xrange(1))/(n-1);
Dy=(yrange(m)-yrange(1))/(m-1);
u=ones(n,m);uk=ones(n,m);
u(:,1)=uy1';u(:,m)=uym';
u(1,:)=ux1;u(n,:)=uxn;
printf('Iteration started.');
for k =1:nmax
ke = 0;kk=k;
if modulo(kk,10) == 0 then
printf('Current iteration is number %g.',k);
end;
for i = 2:n-1
for j = 2:m-1
uk(i,j)=...
(Dx^2*(u(i,j-1)+u(i,j+1))+Dy^2*(u(i-1,j)+u(i+1,j)))/(2*(Dx^2+Dy^2));
if abs(uk(i,j)-u(i,j))>epsilon then
ke = ke+1
end;
u(i,j)=uk(i,j);
end;
end;
if ke == 0 then
break
end;
end;
if kk==nmax then
printf('Maximum number of iterations %g reached',kk);
end;
Example of application of function LaplacePDE1
Consider the case in which the solution region is defined by R = {0<x<1,0<y<1}, with boundary
conditions given by u(0,y) = 0, u(1,y) = 1, u(x,0) = 1-x
2
, u(x,1) = 0. The following application of
function LaplacePDE1 will calculate the solution to this Laplace equation problem. The first
solution using x = y = 0.1, = 0.001, converges after 42 iterations.
Download at InfoClearinghouse.com 41 2001 Gilberto E. Urroz
-->getf('LaplacePDE1')
-->x=[0:0.1:1];y=[0:0.1:1];
-->ux1=zeros(y);uxn=ones(y);uy1=1-x^2;uym=zeros(x);
-->epsilon=0.001;nmax=1000;
-->[u,k]=LaplacePDE1(x,y,ux1,uxn,uy1,uym,epsilon,nmax);
(Output suppressed)
-->k
k = 42
-->plot3d(x,y,u,45,45,'x@y@u(x,y)')
A solution with a more refined grid, x = y = 0.05, is produced next. Convergence is achieved
after 128 iterations.
-->x=[0:0.05:1];y=[0:0.05:1];
-->ux1=zeros(y);uxn=ones(y);uy1=1-x^2;uym=zeros(x);
-->epsilon=0.001;nmax=1000;
-->[u,k]=LaplacePDE1(x,y,ux1,uxn,uy1,uym,epsilon,nmax);
(output suppressed)
-->k
k = 128.
-->plot3d(x,y,u,45,45,'x@y@u(x,y)')
Download at InfoClearinghouse.com 42 2001 Gilberto E. Urroz
The solution is also shown as contour curves:
-->contour(x,y,u,10)
-->xtitle('Solution to Laplace equation','x','y')
Exercises
In the following exercises we use the following PDEs from physical science:
One-dimensional wave equation: u
tt
= c
2
u
xx
One-dimensional heat equation: u
t
= c
2
u
xx
Two-dimensional Laplace equation: u
xx
+u
yy
= 0
Two-dimensional Poisson equation: u
xx
+u
yy
= f(x,y)
With the notation u
t
= u/t, u
xx
=
2
u/
2
x, etc.
[1]. Determine if the following functions u(x,t) satisfy the one-dimensional wave equation:
(a) u(x,t) = cxt (b) u(x,t) = ln(x
2
-c
2
t
2
)
(c) u(x,t) = x
2
+ c
2
t
2
(d) u(x,t) = x/(x
2
+c
2
t
2
)
[2]. Determine if the following functions u(x,t) satisfy the one-dimensional heat equation:
(a) u(x,t) = exp(k(x+kc
2
t)) (b) u(x,t) = t+x
2
/(2c
2
)
(c) u(x,t) = 2c
2
/(2c
2
t+x
2
) (d) u(x,t) = x
2
+ 2c
2
t
2
[3]. Determine if the following functions u(x,y) satisfy the two-dimensional Laplace equation:
(a) u(x,t) = k(x
2
+y
2
) (b) u(x,t) = tan
-1
(y/x)
(c) u(x,t) = ln(x
2
+y
2
) (d) u(x,t) = x
3
-3xy
2
Download at InfoClearinghouse.com 43 2001 Gilberto E. Urroz
[3]. Determine if the functions u(x,y) satisfy the two-dimensional Poisson equation for the
given function f(x,y):
(a) u(x,y) = x cos y, f(x,y) = -x cos y
(b) u(x,y) = x
2
-2xy+y
2
, f(x,y) = 4
(c) u(x,y) = x ln y, f(x,y) = -x/y
2
(d) u(x,y) = (x+y)
3
, f(x,y) = 12(x+y)
[4]. Classify the following second-order partial differential equations as parabolic, hyperbolic,
or elliptic:
(a) u
t
- u
tx
- u
xx
= 0 (b) u
xx
- 2u
xy
+ u
yy
= xy
(c) u
xx
+ u
xt
- u
tt
+ u
x
+ u
t
= 0 (d) u
xx
+ u
yy
= 0
Solve problems [5] through [12] using separation of variables and Fourier series expansions.
[5]. Solution to the heat equation with derivative boundary conditions. Using separation of
variables, solve the one-dimensional heat equation for the temperature u(x,t) on a bar of
length L = 1, using as boundary conditions u/x = 0 for x = 0 and x = L. Assume that the initial
temperature distribution is given by u(x,0) = f(x) = x(1-x), and use k=1.
[6]. Solution to the heat equation with heat convection loss . Consider the one-dimensional
heat equation including convection losses: u/t = k(
2
u/x
2
) - u, and subject to the boundary
conditions: u(0,t) = 0, u(L,t) = 0, and the initial condition: u(x,0) = f(x).
(a) Using the transformation u(x,t) = exp(-t)w(x,t), verify that the original PDE is
transformed into w/t = k(
2
w/x
2
), and that the corresponding boundary and initial
conditions transform into w(0,t) = 0, w(L,t) = 0, and w(x,0) = f(x).
(b). For L = 1, k = 1, = 0.01, and f(x) = x(1-x) determine the solution u(x,t) to the heat
equation with convection loss.
[7]. Solution to the diffusion-convection equation with constant flow velocity. Many
equations related to diffusion in moving fluids include a term involving the flow velocity v in
the x-direction: u/t + v(u/x)= k(
2
u/x
2
). If v is a constant, this equation can be reduced
to the form: w/t = k(
2
w/x
2
),by using the transformation
(a) Verify that the transformation indicated above produces the modified PDE: w/t =
k(
2
w/x
2
).
(b) Solve the diffusion-convection equation with constant flow velocity in the domain
0<x<L for L = 1, k = 1, v = 1, and k = 1, subject to the boundary conditions u(0,t) = 0,
and u(L,t) = 0, and initial conditions u(x,0) = x(1-x).
[8]. Solve the one-dimensional wave equation for c = 1 in the domain 0 < x < L, with L = 1,
given the boundary conditions u(0,t) = 0, and u/x = 0 at x = L, and the initial condition u(x,0)
= f(x) = cos(x).
[9]. Prove that u(x,t) = F(x+ct) + F(x-ct) is a solution to the one-dimensional wave equation.
Plot the result if F(x) = exp(-x
2
/2) with c = 1 in the interval -10 < x < 10, and -10 < t < 10.
Produce a three-dimensional surface plot as well as an animation of the results.
). , ( )]
2
1
(
2
exp[ ) , ( t x w t v x
k
v
t x u

=
Download at InfoClearinghouse.com 44 2001 Gilberto E. Urroz
[10]. Referring to the following figure, solve case [3] of Laplaces equation in the rectangular
domain L = 2, H = 1, if p(y) = (y/H)(1-y/H). Produce a surface plot as well as a contour plot of
the results.
[11]. Referring to the figure of problem [12], solve case [4] of Laplaces equation in the
rectangular domain L = 2, H = 1, if q(y) = (y/H)
2
(1-y/H). Produce a surface plot as well as a
contour plot of the results.
[12]. Using the results of problems [12] and [14] determine the solution to Laplaces equation
in the rectangular domain L = 2, H = 1, if the boundary conditions are u(x,0) = u(x,H) = 0, u(0,y)
= p(y) = (y/H)(1-y/H), and u(L,y) = q(y) = (y/H)
2
(1-y/H). Produce a surface plot as well as a
contour plot of the results.
[13]. Solve problem [5] numerically using finite differences.
[14]. Solve problem [6] numerically using finite differences.
[15]. Solve problem [7] numerically using finite differences.
[16]. Solve problem [8] numerically using finite differences.
[17]. Solve problem [9] numerically using finite differences.
[18]. Solve problem [10] numerically using finite differences.
[19]. Solve problem [11] numerically using finite differences.
[20]. Solve problem [12] numerically using finite differences.
[21]. The solution to the first degree PDE
u/t + c (u/x) = 0
Download at InfoClearinghouse.com 45 2001 Gilberto E. Urroz
can be accomplished by using the following finite-difference explicit scheme (known as the Lax
method)
) (
2
) (
2
1
, 1 , 1 , 1 , 1 1 , k i k i k i k i k i
u u
x
t c
u u u
+ + +



+ = ,
where u
i,k
= u(x
i
,t
k
), x
i
=x
1
+(i-1)x, for i=1,2,,n, and t
k
=t
1
+(k-1)t, for k=1,2,,m. For the
method to be stable -- i.e., to dampen out any error in the solution as the solution progresses -
- the Lax method must satisfy the so-called Courant-Friedrichs-Lewy stability condition, i.e.,
. 1
| |

x
t c
Write a SCILAB function to solve the PDE: u/t + c (u/x) = 0, with c = 1 subject to the initial
condition u(x,0) = f(x) = (x/L)(1-x/L), in the interval 0 < x < L, with L = 1, for 0 < t < 10, using
(a) x = 0.10, t = 0.05
(b) x = 0.10, t = 0.1
(c) x = 0.10, t = 0.2
[22]. A finite-difference approximation to the first-order PDE: u/t + c (u/x) = 0, is given
by
). (
2
, 1 , 1
1 ,
k i k i
ik k i
u u
x
c
t
u u
+
+

Re-write the finite-difference approximation as an equation of the form


u
i,k+1
-u
ik
+ (/2)(u
i+1,k
-u
i-1,k
) = 0,
with = ct/x, and obtain an implicit solution to the PDE for the conditions of problem [21].
Download at InfoClearinghouse.com 46 2001 Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Download at InfoClearinghouse.com 47 2001 Gilberto E. Urroz
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.

Das könnte Ihnen auch gefallen