IAS-NASI-INSA
Summer Research Fellowship Programme 2014
Spectral methods for solving Differential
Equations
Rajesh Aggarwal
Faculty of Engineering and Technology
Gurukula Kangri Vishwavidyalaya Haridwar
Under the guidance of
Dr. Pravin Kumar Gupta
Department of Earth Sciences
Indian Institute of Technology, Roorkee
Uttarakhand
NAME OF SRF:
Rajesh Aggarwal
REGISTRATION NUMBER:
MATS605
INSTITUTE WHERE WORKING:
Indian Institute of
Technology, Roorkee
DATE OF JOINING THE PROJECT:
11/06/2014
DATE OF COMPLETION OF THE PROJECT: 06/08/2014
NAME OF THE GUIDE:
Dr. Pravin Kr. Gupta
PROJECT TITLE:
Spectral methods for
solving Differential
equations
Dr. Pravin Kumar Gupta
Rajesh Aggarwal
Department of Earth Sciences
MATS605
Indian Institute of Technology
Date:
Roorkee, Uttarakhand
Date:
06/08/2014
06/08/2014
Dr. Pravin Kumar Gupta
Department of Earth Sciences,
Indian Institute of Technology
Roorkee, Uttarakhand
E-mail: pkgptfes@iitr.ernet.in
CERTIFICATE
This is to certify that the project entitled Spectral methods for solving
Differential Equations is a bonafide work carried out by Rajesh
Aggarwal, Second Year B.Tech in Electrical Engineering , Faculty of
Engineering and Technology, Gurukula Kangri Vishwavidyalaya,
Haridwar at Indian Institute of Technology, Roorkee under my guidance,
during the period of 11
th
June 2014 to 06
th
August 2014.
(Dr. Pravin Kumar Gupta)
Acknowledgement
I would like to thank Dr. Pravin Kumar Gupta for the continuous guidance
and patience. Being my 1
st
summer project I had lot of things to learn and
the amount of knowledge and experience I have gained from him is
matchless.
I owe my sincere thanks to him.
I thank Mr. Rahul Dehiya for his guidance and overall for making it a
great learning experience.
I also thank IAS-NASI-INSA for their support throughout the programme
and for giving me this great opportunity. It would have not been possible
without this programme.
Rajesh Aggarwal
Table of Contents
1. Introduction...................................................................... 6
1.1. Analytical methods for solving Differential Equation (D.E.)
...7
1.2. Numerical Methods....8
1.2.1. Conventional finite difference method (CFDM)...8
1.2.2. Spectral finite difference method (SFDM)9
2. Methods ........................................................................ 10
2.1. Methods for solving D.E by CFDM.11
2.1.1. Matlab code for solving D.E by CFDM12
2.2. Methods for solving D.E. by SFDM14
2.2.1. Matlab code for solving D.E by SFDM15
2.2.2. Matlab code for function interpolant_raj..16
2.3. Solving Problem on Half-space with different number of grid
points ..18
2.3.1. Conventional finite difference method....18
2.3.1.1. Matlab code for CFDM18
2.3.2. Spectral finite difference method..20
2.3.2.1. Matlab code for SFDM.20
2.3.2.2. Matlab code for function points_raj_finite..23
2.3.2.3. Matlab code for function earths_k.24
2.4. Layered earth Problem....25
2.4.1. Conventional finite difference method....25
2.4.1.1. Matlab code for CFDM...25
2.4.2. Spectral finite difference method..27
2.4.2.1. Matlab code for SFDM....27
3. Conclusion........................................................30
4. References..................................................................... 32
6
Introduction
7
1.1. Analytical methods for solving differential equations:
Analytical solutions are exact solutions of differential equations.
It satisfies differential equations and boundary conditions.
For example: For specific problem of Newton laws of cooling we have differential
equations associated with it as:
( ) 2 25
dy
y
dt
= With initial condition ( ) 0 40 y =
This problem can be easily solved analytically and give exact results having solution
as:
1
2
(25 )
dy
dt y
=
=> 2 ,
(25 )
dy
dt
y
=
=> 2 ,
(25 )
dy
dt
y
=
} }
=>
0
( 1) ln 25 2 y t C = +
=> ln 25 2 y t C = +
=>
2 2
25
t C t C
y e e e
+
= =
=>
2
25
C t
y e e
=
=>
2 2
25 25 ,
C t t
y e e Ae
= = +
On putting initial conditions we get
2
25 15
t
y e
= +
But all real physical system do not provide such simple differential equation
And it is tough task to solve complicated differential equations with analytical methods.
So, we need another methods that can be used to solve differential equations
associated with physical system.
8
1.2. Numerical Methods:
Numerical methods provide numerical approximation to the solutions of differential
equations.
Numerical methods do not provide exact solutions of differential equations. They give
approximation to the results.
We will deal with finite difference method methods for solving differential equations:
(1). Conventional finite difference method (CFDM)
(2). Spectral finite difference method (SFDM)
1.2.1. Conventional finite difference method:
In conventional finite difference method we convert our differential equation to
difference equation and then solve it to get our desired result.
Differential equations can be converted to difference equation by replacing first and
second prime by respective difference formula.
The derivative of a function f at a point x is defined by the limit
0
( ) ( )
( ) lim
h
f x h f x
f x
h
+
' =
And, double derivative of a function f at a point x is defined by the limit
2
0
( ) 2 ( ) ( )
( ) lim
h
f x h f x f x h
f x
h
+ +
'' =
The above two formula can be derived by considering Taylor series of function f.
2
( ) ( )
( ) ( ) ( )
1! 2!
f a f a
f a h f a h h O h
' ''
+ = + + +
Where ( ) O h denotes higher order terms or by simply neglecting all higher order terms
we can write
( )
( ) ( )
1!
f a
f a h f a h
'
+ ~ +
Or,
( ) ( )
( )
f a h f a
f a
h
+
' ~
9
1.2.2 Spectral finite difference methods:
When data are smooth Spectral finite difference methods can be used to solve
ordinary differential equations and partial differential equations with high accuracy in
simple domain.
Spectral finite difference methods can achieve high accuracy then conventional finite
difference methods (spectral finite difference methods can achieve accuracy up to ten
digits while finite difference can up to 2 digits with the same number of grid points).
In spectral finite difference methods we write the differential equations as the sum of
basic functions and find there coefficient such that the resultant function can satisfy
the differential equations.
For example: To solve any differential equation we can take a function
( ) ( )
n n
n
f x a x |
=
=
Such that ( ) f x satisfy ( )
j j
f x u =
Where
j
u is corresponding value of function at
j
x
Here we are free to choose ( )
n
x | , ( )
n
x | can be either
(i) trigonometric,
(ii) exponentials or
(iii) Simple combinations of polynomials.
10
Methods
11
2.1. Methods for solving differential equations by CFDM:
Let the given differential equation be:
2
2
2
0
d y
k y
dx
=
.. <01>
With boundary conditions (0) 1 y = & (1)
k
y e = and k=1
To solve this equation by conventional finite difference method we need to convert this
differential equations to difference equations using:
0
( ) ( )
( ) lim
h
f x h f x
f x
h
+
' = , And
2
0
( ) 2 ( ) ( )
( ) lim
h
f x h f x f x h
f x
h
+ +
'' =
Or say
1 1
2
2
( )
i i i
i
y y y
y x
h
+
+
'' = where
1 1
( )
i i
y y x
+ +
= (value of y at
1 i
x
+
),
i
x denotes the grid
points where we have to evaluate the value of differential equation and denotes space
between these grid points.
Then our differential equation will become:
=>
2 1 1
2
2
0
i i i
i
y y y
k y
h
+
+
=
Or,
2 2
1 1
(2 ) 0
i i i
y k h y y
+
+ + = --------------------- <02>
Let we have to solve this differential equation in interval [0, 1], here we can discretise
our data in as many interval we want by choosing appropriate value of h .
And get points as
i
x ih = where i =0, 1, 2,
Let h =0.25 then from equation 02 we get set of algebraic equations as:
2 2
2 1 0
(2 (0.25) ) 0 y k y y + + =
2 2
3 2 1
(2 (0.25) ) 0 y k y y + + =
2 2
4 3 2
(2 (0.25) ) 0 y k y y + + =
2 2
5 4 3
(2 (0.25) ) 0 y k y y + + =
Now writing these set of equations in matrix form as:
Ay B =
And then after putting boundary conditions and solving the matrix equation for y we
get our desired result.
12
2.1.1 Matlab codes for solving this differential equations
clc; clear;
A=input('enter lower limit');
B=input('enter upper limit');
h=input('enter value of h');
K=input('enter value of k');
P=(B-A)/h+1;
for i=0:P-1
Q(i+1)=A+i*h;
end
for i=1:P+1;
E(i,i)=1;
E(i,i+1)=-2;
E(i,i+2)=1;
end
E=E./(h^2);
E=E(1:P,2:P+1);
disp('Enter boundary conditions');
a=input('Enter initial condition');
b=input('Enter final condition');
% solving differential equation starts here
R=E; I=eye(P);L=zeros(P,P);
for k=1:P
L(k,k)=(K^2).*I(k,k);
end
m=R-L;
j=1; p=P;
y(j)=a; y(p)=b; e=zeros(1,P);
c=zeros(1,P); d=zeros(1,P);
for i=1:P;
e(i)=-1*(m(i,j)*y(j));
end
for i=1:P;
d(i)=-1*(m(i,p)*y(p));
end
for i=1:P;
c(i)=e(i)+d(i);
end
c=c(2:P-1);
c=c';
q=m;
q=q(2:P-1,2:P-1);
l=q\c; O=zeros(1,P);
for i=1:P
if i==1
O(1)=y(j);
elseif i==P
O(P)=y(p);
else
O(i)=l(i-1);
end
end
plot (Q,abs(O));
13
Table 1: solution of differential equations
2
2
2
0
d y
k y
dx
=
by CFDM
Graph 1: Solution of given differential equation by conventional finite difference
method
x Calculated
y
Exact y Error
0 1.0000 1.0000 0
0.25 1.2847 1.2840 0.0006803
0.50 1.6497 1.6487 0.0009844
0.75 2.1178 2.1170 0.0008121
1.0 2.7183 2.7183 0
0
0.5
1
1.5
2
2.5
3
0 0.2 0.4 0.6 0.8 1 1.2
y
-
v
a
l
u
e
x-value
solution of
14
2.2. Methods for solving differential equations by SFDM:
WE can solve the same problem by spectral finite difference methods:
Our differential equation is:
2
2
2
0
d y
k y
dx
=
.. <01>
With boundary conditions (0) 1 y = & (1)
k
y e = and k=1
To solve the given equation by spectral finite difference methods we have to choose
an interpolant ( ) ( )
i i
y x L x y =
and ( ) ( )
i i
y x L x y '' '' =
Now on putting value of ( ) y x '' and ( ) y x in equation (01) we get a matrix in form:
Ay B =
.
And then after putting boundary conditions and solving the matrix equation for y we
get our desired result.
15
2.2.1 Matlab codes for solving this differential equations by SFDM:
clc; clear;
A=input('enter lower limit');
B=input('enter upper limit');
h=input('enter value of h');
K=input('enter value of k');
P=(B-A)/h+1;
for i=0:P-1
Q(i+1)=A+i*h;
end
E=interpolant_raj(Q); % this function provides D-matrix
disp('Enter boundary conditions');
a=input('Enter initial condition');
b=input('Enter final condition');
% solving differential equation starts here
R=E^(2); I=eye(P);L=zeros(P,P);
for k=1:P
L(k,k)=(K^2).*I(k,k);
end
m=R-L;
j=1; p=P;
y(j)=a; y(p)=b; e=zeros(1,P);
c=zeros(1,P); d=zeros(1,P);
for i=1:P;
e(i)=-1*(m(i,j)*y(j));
end
for i=1:P;
d(i)=-1*(m(i,p)*y(p));
end
for i=1:P;
c(i)=e(i)+d(i);
end
c=c(2:P-1);
c=c';
q=m;
q=q(2:P-1,2:P-1);
l=q\c; O=zeros(1,P);
for i=1:P
if i==1
O(1)=y(j);
elseif i==P
O(P)=y(p);
else
O(i)=l(i-1);
end
end
plot (Q,O);
16
2.2.2 Matlab code for function interpolant_raj.
function E=interpolant_raj(T) % T is vector containing points where we
have to interpolate
% E is D matrix
x=T; s=x;
[~, n]=size(x);
A=zeros(1,n); D=zeros(1,n);
B=ones(1,n); E=zeros(n,n);
k=1;
for j=1:n
for i=1:n
if i~=j
A(k)=A(k)+1/(s(j)-x(i));
B(k)=B(k).*(s(j)-x(i));
D(k)=B(k).*A(k);
E(k,k)=A(k);
end
end
k=k+1;
end
G=ones(n,n); k=1;
if n>2
for j=1:n
l=1:n;
l(:,j)=[];
m=1;
for v=1:n-1
z=l(v);
for i=1:n
if i~=j && i~=z
G(k,m)=G(k,m).*(s(j)-x(i));
E(k,z)=G(k,m)./B(z);
end
end
m=m+1;
end
k=k+1;
end
else
for j=1:n
l=1:n;
l(:,j)=[];
m=1;
for v=1:n-1
z=l(v);
for i=1:n
E(k,z)=G(k,m)./B(z);
end
m=m+1;
end
k=k+1;
end
end
17
Table 2: solution of differential equations
2
2
2
0
d y
k y
dx
=
by SFDM
Graph 2: Solution of given differential equation by SFDM
X Calculated y Exact y Error
0
1.000000000000000 1.000000000000000 0
0.25
1.283972703420206
1.284025416687741
0.00005271
0.50
1.648733207743882
1.648721270700128
-0.00001193
0.75
2.117079044491258
2.117000016612675
-0.00007902
1.0
2.718281828459046
2.718281828459046
0
0
0.5
1
1.5
2
2.5
3
0 0.2 0.4 0.6 0.8 1 1.2
y
-
v
a
l
u
e
s
x-values
Solution of D.E.
18
2.3. Solving Problem on Half-space with different number of grid
points and comparing results:
Let our differential equation be
2
2
2
0
d E
k E
dx
+ = this differential equation is arises due
to decay of electric field when it pass through earth surface of different conductivity.
Here
2
k jeo = where o = earth conductivity, = Permeability having value,
=
7
4 10 t