You are on page 1of 15

APPLIED NUMERICAL

ANALYSIS LABORATORY
JADAVPUR UNIVERSITY
DEPARTMENT OF CHEMICAL
ENGINEERING
CLASS: UG-II
SECTION: A-1
GROUP NUMBER: 4
GROUP MEMBERS:
DEBASMITA KUMAR
(ROLL:001310301014)
ANJIK
CHOWDHURY(ROLL:001310301015)
ASHWIN
AGARWAL(ROLL:001310301016)

BIPRATEEP
BANERJEE(ROLL:001310301017)
DATE OF SUBMISSION: - 24/03/2015

DAY 8
PROBLEM STATEMENT:
A batch experiment in liquid-liquid extraction is being
carried out in a laboratory by dispersing one phase into
the other. Obtain the concentration profiles evolving in
the drops assuming that the concentration in the
continuous phase is constant. The concentration
profiles in the drops is described by:
c D 2 c
=
r
t r2 r
r

) ( cr )

r=0

=0;

(c )r=R =c 0 ;

Debasmita Kumar

MATHEMATICAL
BACKGROUND
To solve this partial differential equation we shall use finite difference
method followed by Heuns Predictor Corrector Method. The Finite Difference
method converts the PDE to a set of simultaneous ODE-IVP which are then
solved using Heuns method.
According to finite difference method:
d 2 y y i+12 y i + y i1
=
2
2
dx
h
dy y i +1 y i1
=
dx
2h

Here we shall divide the line from r=0 to r=R(here R=1) in 4 equal parts
(h=0.25) such that at
=1, r=0
=2, r=0.25
=3, r=0.5
=4, r=0.75
=5, r=1

Now according to the boundary conditions:

( cr )

r=0

=0;

(c )r=R =c 0 ;

From the boundary conditions we get that,


c 0=c2
d c5
12 c 4 +2
dr
c 6=
12

We get the residual equations as:

R1: At i=1, r=0


d c1
=32 c 232 c1
dt

R2: At i=2, r=0.25


d c2
=32 c 332 c 2
dt

R3: At i=3, x=0.50


d c3
=24 c 432 c 3+ 8 c 2
dt

R3: At i=4, x=0.75


d c 4 64
32
= c 532 c 4 + c 3
dt
3
3

R3: At i=5, x=1.0


c 5=2

Now we will represent these residuals in the form of a system of


simultaneous ODE-IVPs

32 32
0
0
0 32 32
0
0
8 32 243 +
32
0
0
32
3

[]

[]
0
0
0
128
3

d c1
dt
d c2
c1
dt = c 2 X
d c3
c3
dt
c4
d c4
dt

[]

The above set of equations can be solved by using Heuns Predictor


Corrector method.

A predictorcorrector method (known as Heun's method) can be constructed


from the Euler method (an explicit method) and the Crank Nicholson
method (an implicit method).
Consider the differential equation
and denote the step size by h.
First, the predictor step: starting from the current value , calculate an initial
guess value
via the Explicit-Euler method,
Next, the corrector step: improve the initial guess using Crank Nicholson
method,

That value is used as the next step.

ALGORITHM
Step 1: Start
Step 2: Declaring variables n, i, j, steps, iter of type int and r, R,

c0, d, h, time and size of type long double


Step 3: Declaring arrays B[4][4], c_old[4], c_new1[4], c_new2[4],
D[4], sum[4], prod[4] of type long double
Step 4: Read values for R, c0, d, h, steps, n
Step 5: Define B[4][4], D[4] matrices
Step 6: Print B[4][4], D[4] matrices
Step 7: User is prompted to enter the initial guess of C, and the
value is stored in matrix c_old.
Step 8: In the explicit euler part, The product of matrix B and c_old
is calculated and stored in array prod. This is added to constant
matrix D ,and the sum is multiplied by h. This gives us the first
iteration of c, and the value is stored in matrix c_new1
Step 9: In the crank Nicholson part, the sum of c_new1 and c_old
is stored in array sum
Step 10: A for loop is run, where the value of c_new2 is stored in
c_old, and the value of c_new2 is modified as c_new2[i]+ (h*D[i]).
The value of prod is initialised to 0.
Step 11: Another for loop is run to modify the value of array prod
as prod[i]+ (B[i][j]*sum[j]).

Step 12: The value of prod is calculated as prod[i]*(h/2) , and


c_new2 is calculated as c_new2[i]+ prod[i].
Step 13: The value of time is incremented by h.
Step 14: Another for loop is run to print the value of c_new2 for
each value of t
Step 15: Stop

SOURCE CODE (C++)


//Day-8
//Program to solve a Partial Differential Equation numerically
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
//Declaring variables
//i,j, iter are counters
//steps stores the number of time steps for calculation
//n sotres the number of points for finite difference method
int n, i, steps,j, iter;
//B stores the coefficient matrix B
//c_old stores the previous iterate value of c
//c_new1 stores the value calculated by predictor
//c_new2 stores the value calculated by corrector
//D stores the constant matrix D
//sum and prod are used to store the sum and product of matrices
//r, R, c0, d are constants used in the equation
//h stores the step size
//time and size are used to store time steps
long double B[4][4], c_old[4], c_new1[4], c_new2[4], D[4], sum[4], prod[4], r, R, c0, d,
h, time=0, size;
cout<<"This program solves a partial differential equation by Finite Difference
method followed by Heun's Predictor-Corrector Method."<<endl;
cout<<"\nFirst we convert the PDE to a set of simultaneous ODEs by Finite
Difference Method"<<endl;
cout<<"\nEnter the value of R"<<endl;
cin>>R;
cout<<"\nEnter the value of c0"<<endl;
cin>>c0;
cout<<"\nEnter the value of D"<<endl;
cin>>d;
cout<<"\nEnter step size for Heun's Predictor-Corrector method"<<endl;
cin>>h;
cout<<"\nEnter number of iterations for time step"<<endl;

cin>>steps;
cout<<"\nEnter number of points for Finite Difference method"<<endl;
cin>>n;
size= (R-0)/n;
//Matrix-B is the coefficient matrix
B[0][0]=-32;
B[0][1]=32;
B[0][2]=0;
B[0][3]=0;
B[1][0]=0;
B[1][1]=-32;
B[1][2]=32;
B[1][3]=0;
B[2][0]=0;
B[2][1]=8;
B[2][2]=-32;
B[2][3]=24;
B[3][0]=0;
B[3][1]=0;
B[3][2]=(32/3);
B[3][3]=-32;
//Matrix-D is the constant matrix
D[0]=0;
D[1]=0;
D[2]=0;
D[3]=(128/3);
cout<<"\nThe matrix B is"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<B[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"\nThe matrix D is"<<endl;
for(i=0;i<4;i++)
cout<<D[i]<<endl;
cout<<"\nEnter initial guess for c at t=0"<<endl;
for(i=0;i<n;i++)
cin>>c_old[i];

for(iter=0;iter<steps;iter++)
{
//Predictor: Explicit Euler Method
for(i=0;i<n;i++)
{
c_new1[i]=c_old[i];
prod[i]=0;
for(j=0;j<n;j++)
{
prod[i]= prod[i]+ (B[i][j]*c_old[j]);
}
prod[i]= h*(prod[i]+D[i]);
c_new1[i]=c_new1[i]+ prod[i];
}
//Corrector: Crank-Nicholson Method
for(j=0;j<n;j++)
{
sum[j]=c_old[j]+c_new1[j];
}

for(i=0;i<n;i++)
{
c_new2[i]=c_old[i];
c_new2[i]=c_new2[i]+ (h*D[i]);
prod[i]=0;
for(j=0;j<n;j++)
{
prod[i]=prod[i]+ (B[i][j]*sum[j]);
}
prod[i]=(prod[i]*(h/2));
c_new2[i]=c_new2[i]+ prod[i];
}
time=time+h;
cout<<"\nFor step size="<<size<<endl;
cout<<"\nAt t="<<time<<" the values of c are"<<endl;
for(i=0;i<n;i++)
{
cout<<"c["<<i<<"]="<<c_new2[i]<<endl;
c_old[i]=c_new2[i];
}
}
return 0;
}//main function ends

SAMPLE OUTPUT
This program solves a partial differential equation by Finite
Difference method
followed by Heun's Predictor-Corrector Method.
First we convert the PDE to a set of simultaneous ODEs by Finite
Difference Meth
od
Enter the value of R
1
Enter the value of c0
1
Enter the value of D
1

Enter step size for Heun's Predictor-Corrector method


0.1
Enter number of iterations for time step
10
Enter number of points for Finite Difference method
4
The matrix B is
-32

32

-32

32

-32

24

10

-32

The matrix D is
0
0
0
42
Enter initial guess for c at t=0
0
0
0
0
For step size=0.25

At t=0.1 the values of c are


c[0]=0
c[1]=0
c[2]=5.04
c[3]=-2.52
At t=0.2 the values of c are
c[0]=25.8048
c[1]=-45.1584
c[2]=45.5616
c[3]=-23.9904
At t=0.3 the values of c are
c[0]=626.541
c[1]=-602.542
c[2]=457.221
c[3]=-219.659
At t=0.4 the values of c are
c[0]=8412.36
c[1]=-6593
c[2]=4694.31
c[3]=-2154.42
At t=0.5 the values of c are
c[0]=95013.7
c[1]=-69011.5
c[2]=48333.3
c[3]=-21843.4

At t=0.6 the values of c are


c[0]=1.01075e+006
c[1]=-713994
c[2]=497798
c[3]=-223935
At t=0.7 the values of c are
c[0]=1.05266e+007
c[1]=-7.36318e+006
c[2]=5.12712e+006
c[3]=-2.30337e+006
At t=0.8 the values of c are
c[0]=1.08825e+008
c[1]=-7.58653e+007
c[2]=5.28075e+007
c[3]=-2.37148e+007
At t=0.9 the values of c are
c[0]=1.12224e+009
c[1]=-7.81464e+008
c[2]=5.43897e+008
c[3]=-2.44228e+008
At t=1 the values of c are
c[0]=1.15632e+010
c[1]=-8.04902e+009
c[2]=5.60194e+009

c[3]=-2.51538e+009
-------------------------------Process exited after 10.8 seconds with return value 0
Press any key to continue . . .

PLOT
Value of c vs t
1.50E+10

1.00E+10

5.00E+09

0.00E+00

-5.00E+09

-1.00E+10

10

12

COMMENTS
A few points should be noted about the program.
The coefficient matrix and constant matrix have been
calculated from before and the values obtained have
been used in the program. The reason for doing this is
that it would be virtually impossible to perform
symbolic differentiation in a simplistic code without the
use of additional classes and header files. All the
values of the matrix elements are provided in the
program. All the necessary derivations and calculations
have been shown in the mathematical background
section. Also, all errors encountered in the Finite
Difference have been neglected.
The simultaneous system of ordinary differential
equation obtained are solved using Heuns PredictorCorrector method. The initial value of the unknown
matrix at t=0 has to be input by the user then for all
other time steps the value of c is calculated
accordingly.

You might also like