AME 301: Differential Equations, Vibrations and Controls: Notes On Finite-Difference Methods For

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Notes on Finite-Difference Methods for

AME 301: Differential Equations, Vibrations and Controls


Mihir Sen
Department of Aerospace and Mechanical Engineering
University of Notre Dame
Notre Dame, IN 46556, U.S.A.
November 28, 2005

Introduction

It is common to use numerical methods to compute the solution of nonlinear or otherwise complicated
ordinary differential equations. For partial differential equations it is necessitated also by a geometry for
which an analytical solution may not exist. There are many numerical methods for solving differential
equations, both ordinary and partial, but here we will look only at the finite-difference method in some
detail.
Since numerical solutions are usually calculated on digital computers, the use of numerical methods
require knowledge and efficient use of modern programming languages and techniques. There are a variety
of languages currently in use; some are compilable, such as Fortran, C and C++, while there are others
like Matlab which are more suitable for graphing and use in conjunction with toolboxes. Matlab is usually
sufficient to illustrate the use of a numerical method and hence will be shown below, but readers can convert
them to a compilable language for faster runs.

2
2.1

Numerical differentiation
Functions of a single variable

Let u = u(x), so that on expanding in Taylor series around a point x = x, we have


u(x + x)
u(x x)

1
= u(x) + u0 (x)x + u00 (x)x2 + O(x3 ),
2
1
0
= u(x) u (x)x + u00 (x)x2 + O(x3 ).
2

(1)
(2)

Terms which have xn are indicated simply as of the order of xn and written as O(xn ).
At this point we will change the notation somewhat to make it easier to write computer programs.
Thus, referring to Fig. 1, we will write x(i 1) = x x, x(i) = x and x(i + 1) = x + x with x =
x(i + 1) x(i) = x(i) x(i 1) being the step size. Similarly, we will write u(x + x) = u(i + 1), u(x) = u(i)
and u(x x) = u(i 1). The indices will indicate the location where the quantity is being evaluated.
From Eq. (1) we can obtain the forward-difference approximation
u0 (i) =

1
[u(i + 1) u(i)] + O(x2 ).
x
1

(3)

Similarly from Eq. (2), we have the backward-difference formula


u0 (i) =

1
[u(i) u(i 1)] + O(x2 ).
x

(4)

Subtracting Eq. (2) from Eq. (1) gives the central difference
u0 (i) =

1
[u(i + 1) u(i 1)] + O(x3 )).
2x

(5)

On adding Eqs. (1) and (2), we get the second derivative


u00 (i) =

1
[u(i + 1) 2u(i) + u(i 1)] + O(x3 ).
x2

(6)

u(x)

u(i 1)

x(i 1)

u(i)

x(i)

u(i + 1)

x(i + 1)

Figure 1: Function of a single variable.

Example
Find numerically the derivatives of the function u = sin(2x). Fig. 2 shows a comparison between the numerical and
analytical values for the first and second derivative. It is seen that the smaller values of x gives better results. The
numerical codes used to produce the results are similar to the ones shown in Section 7.2.

2.2

Functions of two variables

Let u = u(x, y) where (x, y) belongs to some two-dimensional area. The region may be divided up by
a grid according to the coordinate system used. Here we will work with a Cartesian grid as shown in
Fig. 3 and use two indices to indicate where a quantity is being evaluated, the first for the x-direction
and the second for the y. The mesh size is then x = x(i + 1, j) x(i, j) = x(i, j) x(i 1, j) and
y = x(i, j + 1) x(i, j) = x(i, j) x(i, j 1).
Expansions in Taylor series around a point (i, j) give
u(i + 1, j) =
u(i 1, j) =
u(i, j + 1) =
u(i, j 1) =

1
u(i, j) + ux (i, j)x + uxx (i, j)x2 + O(x3 ),
2
1
u(i, j) ux (i, j)x + uxx (i, j)x2 + O(x3 ),
2
1
u(i, j) + uy (i, j)y + uyy (i, j)y 2 + O(y 3 ),
2
1
u(i, j) uy (i, j)y + uyy (i, j)y 2 + O(y 3 ).
2
2

(7)
(8)
(9)
(10)

(a)
u0 (x)

10
5
0
5
10

0.2

0.4

x
(b)

0.6

0.8

0.2

0.4

0.6

0.8

u00 (x)

40
20
0
20
40

Figure 2: (a) First and (b) second derivatives of u = sin(2x) using central differences; x = 0.2 (), 0.1
(), 0.02 (); continuous line is analytical.
These can be manipulated to give the finite-difference approximations. For example, the central-difference
approximations for the two first-order partial derivatives are
ux (i, j) =
uy (i, j) =

1
[u(i + 1, j) u(i 1, j)] + O(x3 ),
2x
1
[u(i, j + 1) u(i, j 1)] + O(y 3 ).
2y

(11)
(12)

Similarly the approximations for the second-order partial derivatives are


uxx (i, j) =
uyy (i, j) =

1
[u(i + 1, j) 2u(i, j) + u(i 1, j)] + O(x3 ),
x2
1
[u(i, j + 1) 2u(i, j) + u(i, j 1)] + O(y 3 ).
y 2

The Laplacian operator defined as

2 u = uxx + uyy

(13)
(14)

(15)

can be approximated by
2 u

1
[u(i + 1, j) 2u(i, j) + u(i 1, j)]
x2
1
+ 2 [u(i, j + 1) 2u(i, j) + u(i, j 1)] + O(max(x3 , y 3 )).
y

(16)

If for simplicity we take x = y = h, then this becomes


2 u =
Example

1
[u(i + 1, j) + u(i 1, j) + u(i, j + 1) + u(i, j 1) 4u(i, j)] + O(h3 ).
h2

(17)

Find the Laplacian of the function u = sin [x(1 x)y(1 y)] in 0 x 1 and 0 y 1 shown in Fig. 4.
The result obtained using Eq. (17) is shown in Fig. 5. The code in Section 7.3 was used to generate Figs. 4 and 5.

(i, j + 1)
(i 1, j)

(i, j)

(i + 1, j)

(i, j 1)

Figure 3: Cartesian mesh for two independent variables.

u(x, y)

0.2

0.15

0.1

0.05

0
1
1
0.8

0.5

0.6
0.4

0.2
0

Figure 4: Function u = sin [x(1 x)y(1 y)] shown using a 2020 mesh.

Ordinary differential equations

The finite differences defined in Section 2.1 can be introduced in an ordinary differential equation to reduce
it to algebraic form. At this point there are an infinite number of solutions for the values of the unknown.
To make the solution unique, boundary conditions have to be introduced. Boundary conditions can be of
the following forms. Each one of them can be reduced to algebraic form and used.
3.1

Dirichlet

In this case the value of the function is prescribed at the boundary. For example, one can have
u(1) = a

(18)

where i = 1 is the node at the end where the value of u is given as a. u(1) is then no longer an unknown
quantity.

2 u

0
1
1
0.8

0.5

0.6
0.4

0.2
0

Figure 5: Laplacian of u = sin [x(1 x)y(1 y)] (for clarity multiplied by 1); h = 0.1.
3.2

Neumann

Here the derivative of the function u0 is known at the boundary, so that a finite-difference approximation
has to be used. As an example, if u0 = a at the boundary, we can approximate the condition by
u(2) u(1)
=a
x
where i = 1 lies on the boundary and i = 2 is the node next to it.
3.3

(19)

Robin

Sometimes a linear combination of the function u and its derivative u0 is known at the boundary, i.e.
au + bu0 = c. Once again an algebraic form can be obtained using finite differences, so that
u(2) u(1)
=c
x
where i = 1 and i = 2 are again the nodes on the boundary and next to it.
au(1) + b

(20)

Example
Solve

u00 + u = 0,
(21)
with the Dirichlet boundary conditions u(0) = a and u(1) = b.
Divide the interval [0, 1] into N sub-intervals, each of length x = 1/N , and number the nodes as i = 1, 2, . . . , N + 1
so that x(i) = (i 1)x. Using Eq. (6) in (21), we get
u(i + 1) (2 x2 )u(i) + u(i 1) = 0,

(22)

at i = 2, 3, . . . , N . Applying the boundary conditions u(1) = a and u(N + 1) = b, we get N 1 algebraic equations in
the unknowns u(2), u(3), . . . , u(N ). In matrix form these are

where c = 2

x2 .

c 1
1 c
0
1
..
..
.
.
0
0

0
1
c
..
.
...

u(2)

u(3)

..

u(N
1)

u(N )
1 c
...
0
1
..
.

0
...
...
..
.

0
.
..
,
=

Methods to solve Eq. (23) are in Section 5, and the solution is shown in Fig. 6.

(23)

1
0.9
0.8

u(x)

0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

0.2

0.4

0.6

0.8

Figure 6: Dots indicate the numerical solution of Eq. (21) with x = 0.01, and continuous line the analytical
solution u = sin x/ sin(1).

Partial differential equations

Finite differences described in Section 2.2 can be applied to PDEs to reduce them to algebraic equations.
Boundary conditions may be of the types indicated in Sections 3.1, 3.2 and 3.3, the only difference being that
derivatives are in a direction normal to the boundary, and they can be approximated in a similar fashion.
For the three conditions, one can, for example, write
u(n) = a
u(n) u(n + 1)
=a
n
u(n) u(n + 1)
=c
au(1) + b
n

(24)
(25)
(26)

respectively, where the indices n and n + 1 refer to points at the boundary and a distance n away from it
in a direction normal to the boundary.
Numerical solutions should converge as the mesh becomes finer. Furthermore, the result obtained should
be consistent in that the solution obtained should correspond to that of the original PDE. The numerical
method chosen should also be stable.
4.1

Heat equation in one spatial variable

The heat equation is1


uxx = ut ,

(27)

with suitable boundary conditions. An explicit marching method can be used to calculate the values of
u(x, t) at time from a previous instant using the finite-difference form of Eq. (27)

1 Note

[u(i, j + 1) u(i, j)] ,


[u(i + 1, j) 2u(i, j) + u(i 1, j)] =
x2
t

(28)

that W.E. Boyce and R.C. DiPrima, Elementary Differential Equations and Boundary Value Problems, Eighth Edition,
John Wiley, New York, 2005 has 2 instead of in the equation.

where we have used a central difference in x and a forward difference in t. Thus


u(i, j + 1) = u(i, j) +

t
[u(i + 1, j) 2u(i, j) + u(i 1, j)] .
x2

(29)

which can be repeatedly applied to calculate forward in time. It can be shown that this method is numerically
stable as long as t/x2 < 0.5.
Example

Solve Eq. (27) with = 1 cm2 /s, and the Dirichlet boundary conditions u(0, t) = 0, u(L, t) = 0, and u(x, 0) = f (x) =
20 C. The analytical solution using the method of separation of variables is



 nx 
X
n2 2 t
u(x, t) =
sin
cn exp
,
(30)
2
L
L
n=1
where

Z
 nx 
2 L
f (x) sin
dx.
(31)
L 0
L
Specific values of the parameters have to be assumed for numerical methods, and here we will take L = 50 cm.
The finite-difference mesh to be used is shown in Fig. 7. The distance between x = 0 and x = L is divided into
N parts (here N = 10 is shown in the figure) so that x = L/N . The index i = 1, 2, 3, . . . , N + 1 is used to represent
x so that x(i) = (i 1)x. Similarly, the vertical time axis is divided into steps of t; j = 1, 2, 3, . . . represent t, and
t(j) = (j 1)t.
cn =

x=0

x = 50

Figure 7: Finite difference mesh for Eq. (27).


All values for j = 1 are given by the initial condition. To calculate the values for j = 2, we can use
t
[u(i + 1, 1) 2u(i, 1) + u(i 1, 1)] .
(32)
x2
Writing this for i = 2, 3, . . . , N , (i.e. excluding the two ends points i = 1 and i = N + 1 which are always known because
of the boundary conditions), u(i, 2) can be calculated. The method is explicit since the values of u(i, 2) can be explicitly
written in terms of u(i, 1). Once u(i, 2), is known the same procedure is repeated to get u(i, 3), and then as many times
as necessary to march forward in time.
A numerically obtained solution using this method is shown in Fig. 8. The Matlab code in Section 7.5 calculates
both the analytical and numerical solutions; for simplicity the former is not shown. We have taken x = 1 and t = 0.1
which satisfies the numerical stability criterion.
u(i, 2) = u(i, 1) +

Example
Using a hand calculator, solve Eq. (27) with = 1, L = 1 and u(0, t) = u(L, t) = 0 and u(x, 0) = f (x), where f (x) is
shown in Fig. 9. Choose N = 6.

u(x, t)

20
15
10
5
0
0
50
40

100
30
20

200

10
300
0

Figure 8: Numerical solution of Eq. (27) with = 1; x = 1, t = 0.1. The analytical solution is very
similar.

0.1

x=0

x = 1/3

x=1

Figure 9: Initial value f (x).


Since N = 6, the spatial step is x = L/N = 1/6 = 0.1667. Choose t = 0.01, so that t/x2 = 0.36 (this should
be less than 0.5 for stability). The calculated results are entered in Table 1. The initial conditions are given in the row
j = 1, and the boundary conditions in the columns i = 1 and i = 7. Use Eq. (29) to enter the rest of the values in the
table. As many time steps as necessary can be calculated, but three steps are shown.

4.2

Heat equation in two spatial variables

With two spatial variables, the heat equation is


(uxx + uyy ) = ut

(33)

with suitable initial and boundary conditions.


An explicit marching method, similar to that in Section 4.1 can be used. Using central difference in space
and forward difference in time we get


1
1
[u(i
+
1,
j,
k)

2u(i,
j,
k)
+
u(i

1,
j,
k)]
+
[u(i,
j
+
1,
k)

2u(i,
j,
k)
+
u(i,
j

1,
k)]
=

x2
y 2
1
[u(i, j, k + 1) u(i, j, k)] . (34)
t
8

t = 0.00
t = 0.01
t = 0.02
t = 0.03
t = 0.04
t = 0.05

j
j
j
j
j
j

=1
=2
=3
=4
=5
=6

x=0
i=1
0.0000a
0.0000a
0.0000a
0.0000a
0.0000a
0.0000a

x = 0.1667
i=2
0.0500c
0.0500d
0.0403d
0.0348d
0.0300d
0.0264d

x = 0.3333
i=3
0.1000c
0.0730d
0.0654d
0.0563d
0.0499d
0.0441d

x = 0.500
i=4
0.0750c
0.0750d
0.0653d
0.0598d
0.0538d
0.0487d

x = 0.6667
i=5
0.0500c
0.0500d
0.0500d
0.0465d
0.0436d
0.0401d

x = 0.8333
i=6
0.0250c
0.0250d
0.0250d
0.0250d
0.0237d
0.0223d

x = 1.0000
i=7
0.0000b
0.0000b
0.0000b
0.0000b
0.0000b
0.0000b

From boundary condition at x = 0.


From boundary condition at x = 1.
c
From initial condition at t = 0.
d
From Eq. (29).
a
b

Table 1: Values of u(i, j).


Indices i, j and k correspond to x, y and t. If, for simplicity, we take x = y = h, then
u(i, j, k +1) = u(i, j, k)+

t
[u(i + 1, j, k) + u(i 1, j, k) + u(i, j + 1, k) + u(i, j 1, k) 4u(i, j, k)] . (35)
h2

This is analogous to Eq. (29), and can be used similarly to calculate u(x, y, t) forward in time starting from
the initial conditions. Once again, we should choose t/h2 < 0.5 for stability
4.3

Laplaces equation

This is the equation


uxx + uyy = 0

(36)

in a given domain and with suitable boundary conditions. From Eq. (17), we get
u(i, j) =

1
[u(i + 1, j) + u(i 1, j) + u(i, j + 1) + u(i, j 1)] .
4

(37)

using central differences. The central value is thus the average of its four nearest neighbors. In the relaxation
method, the domain is swept repeatedly to calculate updated values of u at the mesh points.
Example

Solve Eq. (36) in the rectangular domain 0 x 3, 0 y 2 with the Dirichlet boundary conditions u(x, 0) = 0,
u(x, 2) = 0, u(0, y) = 0, and u(3, 0) = f (y) where

y
for 0 y 1,
f (y) =
(38)
2y
for 1 y 2.
The analytical solution is
u(x, y) =

cn sinh

n=1

where

 nx 
2

sin

 ny 
2

(39)

8 sin (n/2)
.
(40)
n2 2 sinh (3n/2)
The rectangle is divided with N divisions in the x-direction and M in the y. A mesh is shown in Fig. 10 with N = 12
and N = 8. The relaxation solution obtained using the code in Section 7.6 is shown in Figs. 11 and 12.
cn =

y=2
j

y=0

x=0

x=3

Figure 10: Finite difference mesh for Eq. (36).

u(x, t)

1
0.8
0.6
0.4
0.2
0
3
2

1.5
1

0.5
0

Figure 11: Numerical solution of Eq. (36) in functional form with h = 0.1. The analytical solution is very
similar.
4.4

Wave equation

A finite difference approximation of the wave equation

is

a2 uxx = utt

(41)

u(i, j + 1) = 2(1 r2 ) u(i, j) + r2 (u(i + 1, j) + u(i 1, j)) u(i, j 1),

(42)

where x and t are the spatial and time steps, and r = a t/x. For stability of the numerical method,
we must take r < 1.

5
5.1

Solution of algebraic equations


Relaxation

Eq. (22) can be written as

u(i + 1) + u(i 1)
.
(43)
2 x2
In the relaxation method the right-side values are assumed to be known from a previous iteration (the first
time they are guessed), and the equation applied repeatedly until convergence is achieved. Section 7.4 shows
the numerical code which performed the integration.
u(i) =

10

0.1

0.

0.5

2.5

0.5

0.

0.3

0.9
0.7

0.1

0.1

0.1

1.5

0.5

0.5

1.5

Figure 12: Numerical solution of Eq. (36) in contour form with h = 0.1. The analytical contours are very
similar.
5.2

Other algorithms

There are many ways to solve algebraic equations of the form Ax = y where A is a square non-singular
matrix, and x and y are appropriate vectors. x is unknown while A and y are known. The solution may be
schematically represented as x = A1 y.

Conclusions

This has been a very brief introduction to finite-difference methods for the numerical solution of boundary
value problems. Many other methods such as finite element and spectral methods are also commonly used.

7
7.1

Appendices
Plotting using Matlab

The following are some of the commands that may be used to display numerical results.
Simple plots: plot(x,y)
Plot with symbols: plot(x,y,o);
Multiple plots: plot(x1,y1,s1,x2,y2,s2,x3,y3,s3,...)
3D plot: plot3(x,y,z)
3D surface: surf(x,y,z)
3D contours: contour(x,y,z)
7.2

Differentiation of functions of a single variable

A Matlab code similar to the one below can be used to produce the results used in Fig. 2.
%Finite difference derivatives
%Function y = sin(2 pi x)
%dy is first and ddy second derivative
clear
%Analytical solution
xa=[0:0.01:1];
11

dya=2*pi*cos(2*pi*xa);
ddya=-4*pi*pi*sin(2*pi*xa);
%Divide interval into N parts
N=5;
dx=1/N;
%Evaluate needed values of function
for i=1:N+3
xx(i)=(i-2)/N;
yy(i)=sin(2*pi*xx(i));
end
%Evaluate derivatives
for i=2:N+2
dery(i)=(yy(i+1)-yy(i-1))/(2*dx);
ddery(i)=(yy(i+1)-2*yy(i)+yy(i-1))/(dx*dx);
end
%Transfer to vectors to be plotted
for i=2:N+2
x(i-1)=xx(i);
dy(i-1)=dery(i);
ddy(i-1)=ddery(i);
end
%Graphs
subplot(2,1,1),plot(xa,dya,x,dy,o)
xlabel(x)
ylabel(dy/dx)
title((a))
subplot(2,1,2),plot(xa,ddya,x,ddy,o)
title((b))
xlabel(x)
ylabel(d2y/dx2)
The Fortran 77 version of the same code is below. The results are stored in text files dera.dat and
dern.dat.
C Finite difference derivatives
C Function y = sin(2 pi x)
C dy is first and ddy second derivative
implicit none
integer M1,N,i
parameter(M1=100,N=5)
real dx,M2,M,pi,xa,dya,ddya,x,y,dy,ddy
dimension xa(M1+1),dya(M1+1),ddya(M1+1)
dimension x(N+3),y(N+3),dy(N+3),ddy(N+3)
C Open two files
open(unit=1,file=dera.dat)
open(unit=2,file=dern.dat)
C Define pi
pi=4*atan(1.0)
C Analytical solution
C Convert from integer to real
M2=float(M1)
dx=1.0/M2
12

C
C

do 100 i=1,M1+1
xa(i)=(i-1)*dx
dya(i)=2*pi*cos(2*pi*xa(i))
100 ddya(i)=-4*pi*pi*sin(2*pi*xa(i))
Numerical solution
Convert from integer to real
M=float(N)
dx=1.0/M
Evaluate needed values of function
do 200 i=1,N+3
x(i)=(i-2)*dx
200 y(i)=sin(2.0*pi*x(i))
Evaluate derivatives
do 300 i=2,N+2
dy(i)=(y(i+1)-y(i-1))/(2*dx)
300 ddy(i)=(y(i+1)-2*y(i)+y(i-1))/(dx*dx)
write(1,400) (xa(i),dya(i),ddya(i),i=1,M1+1)
write(2,400) (x(i),dy(i),ddy(i),i=2,N+2)
400 format(E15.8,2x,E15.8,2x,E15.8)
close(1)
close(2)
stop
end

A graphing program must be used to read and plot the data in the files dera.dat and dern.dat produced
by Fortran. A Matlab code that does this is shown below.
%Plot finite difference derivatives
%stored in files dera.dat and dern.dat
%obtained from Fortran code
clear
load(-ascii,dera.dat)
load(-ascii,dern.dat)
%Graphs
subplot(2,1,1),plot(dera(:,1),dera(:,2),dern(:,1),dern(:,2),o)
xlabel(x)
ylabel(dy/dx)
title((a))
subplot(2,1,2),plot(dera(:,1),dera(:,3),dern(:,1),dern(:,3),o)
title((b))
xlabel(x)
ylabel(d2y/dx2)
7.3

Differentiation of functions of two variables

The following Matlab code produces Figs. 4 and 5.


%Finite difference Laplacian
%Function u = sin(pi x(1-x)y(1-y))
clear
%Divide region into N by M parts
%for analytical purposes
13

N=20;
M=N;
%Calculate function at analytical mesh points
for i=1:N+1
for j=1:M+1
xa(i)=(i-1)/N;
ya(j)=(j-1)/M;
ua(i,j)=sin(pi*xa(i)*(1-xa(i))*ya(j)*(1-ya(j)));
end
end
%Plot function at analytical mesh points
surf(xa,ya,ua)
xlabel(x)
ylabel(y)
zlabel(z)
%Divide region into N by M parts
%for numerical purposes
N=10;
M=N;
h=1/N;
%Calculate function at numerical mesh points
for i=1:N+3
for j=1:M+3
xx(i)=(i-2)/N;
yy(j)=(j-2)/M;
u(i,j)=sin(pi*xx(i)*(1-xx(i))*yy(j)*(1-yy(j)));
end
end
%Calculate Laplacian
for i=2:N+2
for j=2:M+2
LL(i,j)=(u(i+1,j)+u(i-1,j)+u(i,j+1)+u(i,j-1)-4*u(i,j))/(h*h);
end
end
%Transfer to matrices to be plotted
for i=2:N+2
x(i-1)=xx(i);
end
for j=2:M+2
y(j-1)=yy(j);
end
for i=2:N+2
for j=2:M+2
L(i-1,j-1)=LL(i,j);
end
end
%Plot L at numerical mesh points
figure
surf(x,y,-L)
xlabel(x)
ylabel(y)
14

zlabel(z)
7.4

Ordinary differential equations

The Matlab code used to produce Fig. 6 is given below.


%Second-order ODE
% y+y=0, y(0)=0,y(1)=1
clear
N=100;
dx=1/N;
for i=1:N+1
x(i)=(i-1)/N;
end
y(1)=0;
y(N+1)=1;
for i=2:N
y(i)=0;
end
for j=1:10000
for i=2:N
y(i)=(y(i+1)+y(i-1))/(2-dx^2);
end
end
for i=1:N+1
ya(i)=sin(x(i))/sin(1);
end
plot(x,ya,x,y,.)
xlabel(x)
ylabel(y)
7.5

Partial differential equations: heat equation

The following Matlab code calculates the analytical and numerical solution of Eq. 27 and plots Fig. 8.
%Heat equation
%Boyce and DiPrima, 8th Ed., p. 608, Ex. 1
clear
%Analytial solution
N=50; % x divisions
M=25; %t divisions
NT=500; % number of terms in infinite series
for i=1:N+1
xa(i)=50*(i-1)/N; %calculate %space vector
end
for j=1:M+1
ta(j)=250*(j-1)/M; % calculate time vector
end
% Calculate u(x,t)
for j=1:M+1
for i=1:N+1
ua(j,i)=0;
15

for k=1:2:NT
ua(j,i)=ua(j,i)+(80/(k*pi))*exp(-k*k*pi*pi*ta(j)/2500)*sin(k*pi*xa(i)/50);
end
end
end
surf(xa,ta,ua)
xlabel(x)
ylabel(t)
zlabel(u)
clear
%Numerical solution
N=50; %x divisions
dx=50/N;
for i=1:N+1
xn(i)=(i-1)*dx;
end
dt=0.1;
%Initial conditions
i=1;
tn(i)=0;
for j=1:N+1
un(j,i)=20;
end
T=0;
while (tn(i)<251)
i=i+1;
un(1,i)=0;
for j=2:N
un(j,i)=un(j,i-1)+dt*(un(j-1,i-1)-2*un(j,i-1)+un(j+1,i-1))/(dx*dx);
end
un(N+1,i)=0;
tn(i)=tn(i-1)+dt;
end
M=length(tn);
for j=1:N+1
k=0;
for i=1:100:M
k=k+1;
tnn(k)=tn(i);
unn(j,k)=un(j,i);
end
end
figure
surf(tnn,xn,unn)
xlabel(t)
ylabel(x)
zlabel(u)
7.6

Partial differential equations: Laplaces equation

The Matlab code used to solve Eq. (36) analytically and numerically, and plot Figs. 11 and 12 is given below.

16

%Laplace equation
%p. 642, Ex. 1
clear
%Analytial solution
N=150; % x divisions
M=100; %t divisions
NT=50; % number of terms in infinite series
for i=1:N+1
xa(i)=3*(i-1)/N; %calculate x vector
end
for j=1:M+1
ya(j)=2*(j-1)/M; % calculate y vector
end
% Calculate u(x,y)
for j=1:M+1
for i=1:N+1
ua(j,i)=0;
for n=1:NT
Sn=8*sin(n*pi/2)/(n^2*pi^2*sinh(3*n*pi/2));
ua(j,i)=ua(j,i)+Sn*sinh(n*pi*xa(i)/2)*sin(n*pi*ya(j)/2);
end
end
end
surf(xa,ya,ua)
xlabel(x)
ylabel(y)
zlabel(u)
figure
v=[0.1,0.3,0.5,0.7,0.9];
[cs,h]=contour(xa,ya,ua,v);
xlabel(x)
ylabel(y)
zlabel(u)
clabel(cs,h)
%Numerical solution
N=30;
M=20;
for i=1:N+1
xn(i)=3*(i-1)/N; %calculate x vector
end
for j=1:M+1
yn(j)=2*(j-1)/M; % calculate y vector
end
%Boundary conditions
for i=1:N+1
un(i,1)=0;
un(i,M+1)=0;
end
for j=1:M/2+1
un(1,j)=0;
un(N+1,j)=yn(j);
17

end
for j=M/2+1:M+1
un(1,j)=0;
un(N+1,j)=2-yn(j);
end
%Relaxation
for k=1:100
for i=2:N
for j=2:M
un(i,j)=(un(i+1,j)+un(i-1,j)+un(i,j+1)+un(i,j-1))/4;
end
end
end
figure
surf(yn,xn,un)
xlabel(x)
ylabel(y)
zlabel(u)
figure
v=[0.1,0.3,0.5,0.7,0.9];
[cs,h]=contour(yn,xn,un,v);
xlabel(y)
ylabel(x)
zlabel(u)
clabel(cs,h)

Problems
In the following, write Fortran codes for numerical computations and Matlab to plot.
1. Determine the second derivative y 00 (x) if y(x) = sin 2x in 0 x 1 using finite differences for step
sizes of 0.1, 0.01, and 0.001. Plot (a) the analytical function y 00 (x), and (b) the numerically obtained
y 00 (x).
2. Determine ux , uy , uxx and uyy if u(x, y) = sin 2xy in 0 x 1, 0 y 1. Make 3D plots for each
of the derivatives.
3. Solve Eq. (21), with u(0) = 0 and u(1) = 1. Graph the solution.
4. Solve u00 + xu0 + u = 0 with u(0) = 0 and u(1) = 1. Graph the solution.
5. Solve uxx = ut with u(0, t) = 0, ux (1, t) = 0 and u(x, 0) = sin(x/2).
6. Solve uxx + uyy = 0 with the geometry, dimensions and boundary conditions indicated in Fig. 13.
7. Solve uxx + uyy = 0 in a rectangle 0 x 2, 0 y 1 with boundary conditions u(0, y) = 0,
u(2, y) = 2y, u(x, 0) = 0 and u(x, 1) = x.
8. Solve uxx + uyy = ut with u(x, y, t) in a square 0 x 1, 0 y 1. The initial condition is
u(x, y, 0) = sin [x(1 x)y(1 y)], and the boundaries are all at u = 0. Plot the u contours for
t = 0, 0.5, 1.0.
9. Solve uxx + uyy = 0 with the geometry, dimensions and boundary conditions indicated in Fig. 14. Plot
(a) the surface u(x, y), and (b) several u = constant contours.
10. Five sides of a unit cube have u = 0 while the sixth has u(x, y, 0) = sin [x(1 x)y(1 y)]. Solve
laplaces equations in three dimensions uxx + uyy + uzz = 0.
11. A stretched string, held firmly at both ends, is plucked at a point one-third of its length; the initial
displacement, f (x), is shown in Fig. 9. The initial velocity is zero. Taking N = 3, use a hand calculator
to determine values of the displacement, u(i, j), for two time steps using Eq. (42). Assume a = 1 and
t = 0.1. [Hint: Since the initial velocity is zero, u(i, 1) = u(i, 2).]

18

uy = 0
5 u=1
u=0

u=1
5

10

ux = 0

y
10
x

u=0

Figure 13: Corner.

u=0

ux = 0
0.5

ux = 0

uy = 0

0.5
u=1

y
1
x

uy = 0
Figure 14:

19

You might also like