You are on page 1of 17

ChemE 109 - Numerical and Mathematical Methods

in Chemical and Biological Engineering


Fall 2007
Solution to Homework Set 5
1. The set of linear ordinary dierential equations which describe the monomolecular kinetics of
the chemical reaction:
A B

C
has the form:
dC
A
dt
= k
1
C
A
dC
B
dt
= k
1
C
A
k
2
C
B
+ k
3
C
C
dC
C
dt
= k
2
C
B
k
3
C
C
where C
A
, C
B
, C
C
denote the concentrations of species A, B, C, respectively, k
1
= 1, k
2
= 2,
k
3
= 3 and the initial conditions for the three components are C
A
(0) = 1, C
B
(0) = 0 and
C
C
(0) = 0.
For the above system of ODEs:
(a) Compute its analytical solution and the steady-state values of the variables C
A
, C
B
and
C
C
.
(b) Write a MATLAB code, which uses Euler predictor-corrector, with h = 0.1, to compute
C
A
(10), C
B
(10), C
C
(10).
(c) Write a MATLAB code, which uses fourth-order Runge-Kutta, with h = 0.1, to compute
C
A
(10), C
B
(10), C
C
(10).
Solution:
(a) Compute its analytical solution and the steady-state values of the variables C
A
, C
B
and C
C
.
A =
_

_
k
1
0 0
k
1
k
2
k
3
0 k
2
k
3
_

_ =
_

_
1 0 0
1 2 3
0 2 3
_

_
1
= 5
2
= 0
3
= 1
(A
1
I)
1
= 0
1
=
_

_
0
1
1
_

_
(A
2
I)
2
= 0
2
=
_

_
0
3
2
_

_
(A
3
I)
3
= 0
3
=
_

_
2
1
1
_

_
1
T = [
1

2

3
] =
_

_
0 0 2
1 3 1
1 2 1
_

_ T
1
=
_

_
0.1 0.4 0.6
0.2 0.2 0.2
0.5 0 0
_

_
e
At
= T
_

_
e
5t
0 0
0 1 0
0 0 e
t
_

_T
1
=
_

_
e
t
0 0
0.6 0.1e
5t
0.5e
t
0.6 + 0.4e
5t
0.6 0.6e
5t
0.4 + 0.1e
5t
0.5e
t
0.4 0.4e
5t
0.4 + 0.6e
5t
_

_
x(t) = e
At
x(0) =
_

_
e
t
0.6 0.1e
5t
0.5e
t
0.4 + 0.1e
5t
0.5e
t
_

_
At steady state:
dC
A
dt
=
dC
B
dt
=
dC
C
dt
= 0
So _

_
C
A,ss
= 0
C
A,ss
2C
B,ss
+ 3C
C,ss
= 0
2C
B,ss
3C
C,ss
= 0

_
C
A,ss
= 0
C
B,ss
= 1.5C
C,ss
We also have the following equation from the mass conservation law
C
A
+ C
B
+ C
C
= Const. C
A,ss
+ C
B,ss
+ C
C,ss
= C
A
(0) + C
B
(0) + C
C
(0) = 1
Then the values of steady state can be solved as
_

_
C
A,ss
= 0
C
B,ss
= 0.6
C
C,ss
= 0.4
We can also solve get the same steady state from the analytical solution by assigning
t = .
(b) Write a MATLAB code, which uses Euler predictor-corrector, with h = 0.1, to compute
C
A
(10), C
B
(10), C
C
(10).
The results using Euler predictor-corrector are
_

_
C
A
(10) = 0.0000
C
B
(10) = 0.6000
C
C
(10) = 0.4000
From the results of the rst question, the solutions at t = 10 are approaching the steady
state.
Sample Code:
clear all;
x(1,:)=[1,0,0,];
t(1)=0;
t_end=10;
h=.1;
coef=[1/2,1/2];
for i=1:t_end/h
t(i+1)=t(i)+h;
2
k_1=func5_1(x(i,:),t(i));
k_2=func5_1(x(i,:)+h*k_1,t(i)+h);
x(i+1,:)=x(i,:)+h*coef*[k_1;k_2];
end
x(end,:)
where func5 1 is dened as
function dx=func5_1(x,t)
k_1=1;
k_2=2;
k_3=3;
dx(1)=-k_1*x(1);
dx(2)=k_1*x(1)-k_2*x(2)+k_3*x(3);
dx(3)=k_2*x(2)-k_3*x(3);
end
Plot:
0 1 2 3 4 5 6 7 8 9 10
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Homework 5.1 Euler Predictorcorrector
t
C


C
A
C
B
C
C
(c) Write a MATLAB code, which uses fourth-order Runge-Kutta, with h = 0.1, to compute
C
A
(10), C
B
(10), C
C
(10).
The results using fourth-order Runge-Kutta are
_

_
C
A
(10) = 0.0000
C
B
(10) = 0.6000
C
C
(10) = 0.4000
3
From the results of the rst question, the solutions at t = 10 are approaching the steady
state.
Sample Code:
clear all;
x(1,:)=[1,0,0,];
t(1)=0;
t_end=10;
h=.1;
coef=[1/6,1/3,1/3,1/6];
for i=1:t_end/h
t(i+1)=t(i)+h;
k_1=func5_1(x(i,:),t(i));
k_2=func5_1(x(i,:)+h/2*k_1,t(i)+h/2);
k_3=func5_1(x(i,:)+h/2*k_2,t(i)+h/2);
k_4=func5_1(x(i,:)+h*k_3,t(i)+h);
x(i+1,:)=x(i,:)+h*coef*[k_1;k_2;k_3;k_4];
end
x(end,:)
the same function func5 1 as in question b is used here.
Plot:
0 1 2 3 4 5 6 7 8 9 10
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Homework 5.1 FourthOrder RungeKutta
t
C


C
A
C
B
C
C
4
2. Determine the solution of the following second order ODE:
d
2
x
dt
2
+
dx
dt
x = x
2
+ t
at t = 10, i.e. x(10), using fourth-order Runge-Kutta (write the appropriate MATLAB code)
with h = 0.1, and initial conditions x(0) = 1 and
dx
dt
(0) = 1.
Solution:
Given the following denitions of variables
_

_
x
1
= x
x
2
=
dx
dt
The second-order ODE can be rewritten as
_

_
dx
1
dt
= x
2
dx
2
dt
= x
2
+ x
1
x
2
1
+ t
Then the problem can be solved using fourth-order Runge-Kutta. The solution at t = 10 is
x(10) = 3.6793
Sample Code:
clear all;
x(1,:)=[1,1];
t(1)=0;
t_end=10;
h=.1;
coef=[1/6,1/3,1/3,1/6];
for i=1:t_end/h
t(i+1)=t(i)+h;
k_1=func5_2(x(i,:),t(i));
k_2=func5_2(x(i,:)+h/2*k_1,t(i)+h/2);
k_3=func5_2(x(i,:)+h/2*k_2,t(i)+h/2);
k_4=func5_2(x(i,:)+h*k_3,t(i)+h);
x(i+1,:)=x(i,:)+h*coef*[k_1;k_2;k_3;k_4];
end
x(end,1)
the same function func5 2 as in question b is used here.
Plot:
5
0 1 2 3 4 5 6 7 8 9 10
1
1.5
2
2.5
3
3.5
4
Homework 5.2 FourthOrder RungeKutta
t
x
3. Consider the following system of ordinary dierential equations
dx
1
dt
= x
1
(1 x
1
) x
1
x
2
dx
2
dt
= x
1
x
2
x
2
(1 x
2
)
with x
1
(0) = 1 and x
2
(0) = 1.
(a) We consider the explicit Euler method for numerical integration. What is the maximum
value for the time step h that guarantees numerically stable integration over a single time
step? Do you expect the integration to be sti in the vicinity of t = 0? Justify your
answer.
(b) Is the steady-state (x
1
, x
2
) = (0, 0) stable or unstable?
Solution:
(a) We consider the explicit Euler method for numerical integration. What is the maximum
value for the time step h that guarantees numerically stable integration over a single time
step? Do you expect the integration to be sti in the vicinity of t = 0? Justify your
answer.
The Jacobian matrix of the multi-variable nonlinear functions F(x) is
J = F(X) =
_

_
f
1
(X)
x
1
f
1
(X)
x
2
f
2
(X
x
1
f
2
(X)
x
2
_

_
=
_
1 2x
1
x
2
x
1
x
2
x
1
1 + 2x
2
_
6
where
F(X) =
_
x
1
(1 x
1
) x
1
x
2
x
1
x
2
x
2
(1 x
2
)
_
and
X =
_
x
1
x
2
_
The linearization around X(0) is
J(X(0)) = J
__
1
1
__
=
_
2 1
1 2
_
The maximum value of the time step h is
h <
2
|
max
|
where
|
max
| = max
1,2
|
i
(J(X))|
In the vicinity of X(0), the ODE system is linearized as

X = J(X(0))X
The eigenvalues of J(X(0)) are
1,2
=

3.
The stiness in the vicinity of X(0) is
S =
|
max
|
|
min
|
= 1 O(1)
Thus the integration in the vicinity of t = 0 wont be sti.
(b) Is the steady-state (x
1
, x
2
) = (0, 0) stable or unstable?
J(0) =
_
1 0
0 1
_
Eigenvalue:
1,2
= 1. Since there is a positive eigenvalue, the steady-state is unstable.
4. Consider the ODE-Boundary Value Problem:
d
2
y
dx
2
+ (1 x)y + y
2
= 0
with boundary conditions:
dy
dx
(x = 1) = 0 and
dy
dx
(x = 1) = 0
(a) Sketch the one-dimensional domain for this problem, indicating nodes, node numbering
and numbering for the unknowns. The node number is N 3.
(b) Apply centered nite dierences to discretize the equation and boundary conditions.
(c) Show the form of all vectors and matrices needed for solution using Newtons method.
Solution:
7
0 1 i-1 i i+1 N N+1
(a) Sketch the one-dimensional domain for this problem, indicating nodes, node numbering
and numbering for the unknowns. The node number is N 3.
The total node number is N, the width of lattice is h =
2
N 1
(b) Apply centered nite dierences to discretize the equation and boundary conditions.
Discretization using centered nite dierence
d
2
y
dx
2
=
y
i+1
2y
i
+ y
i1
h
2
dy
dx
=
y
i+1
y
i1
2h
For node i
y
i+1
2y
i
+ y
i1
h
2
+ (1 x
i
)y
i
+ y
2
i
= 0, i = 1, 2, . . . , N
where
x
i
= x
1
+
x
N
x
1
N 1
(i 1) = 1 +
2(i 1)
N 1
For boundary conditions
i = 1 :
y
2
y
0
2h
= 0 y
2
= y
0
i = N :
y
N+1
y
N1
2h
= 0 y
N+1
= y
N1
Substitute back into the equations of node 1 and N
i = 1 :
y
2
2y
1
+ y
2
h
2
+ (1 x
1
)y
1
+ y
2
1
= 0
i = N :
y
N1
2y
N
+ y
N1
h
2
+ (1 x
N
)y
N
+ y
2
N
= 0
where x
1
= 1 and x
N
= 1.
(c) Show the form of all vectors and matrices needed for solution using Newtons method.
Using multi-variable Newtons method
Y =
_

_
y
1
y
2
.
.
.
y
N
_

_
F(Y ) =
_

_
f
1
f
2
.
.
.
f
N
_

_
=
_

_
1
h
2
(y
2
2y
1
+ y
2
) + (1 x
1
)y
1
+ y
2
1
1
h
2
(y
3
2y
2
+ y
1
) + (1 x
2
)y
2
+ y
2
2
.
.
.
1
h
2
(y
N1
2y
N
+ y
N1
) + (1 x
N
)y
N
+ y
2
N
_

_
8
Jacobian matrix J
J = F(X) =
_

2
h
2
+ (1 x1) + 2y1
2
h
2
0 0
1
h
2

2
h
2
+ (1 x
2
) + 2y
2
0 0
0
1
h
2
0 0
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
0 0
1
h
2
0
0 0
2
h
2
+ (1 x
N1
) + 2y
N1
1
h
2
0 0
2
h
2

2
h
2
+ (1 xN) + 2yN
_

_
J(Y
(M)
)
(M)
y
= F(Y
(M)
)
Y
(M+1)
= Y
(M)
+
(M)
y
= Y
(M)
+ J(Y
(M)
)
1
F(Y
(M)
)
5. Consider the second order ODE-BVP:
d
2
y
dx
2
2y
3
= 0
with y(x = 1) = 1 and y(x = 2) =
1
2
.
(a) Demonstrate that y(x) =
1
x
satises the dierential equation and the boundary conditions.
(b) Write a MATLAB code, solve the problem numerically using the shooting method.
(c) Write a MATLAB code, solve the problem numerically using the nite-dierence method
with 5 equidistant interior nodes.
Plot the analytical and numerical solutions clearly.
Solution:
(a) Demonstrate that y(x) =
1
x
satises the dierential equation and the boundary conditions.
y =
1
x

dy
dx
=
1
x
2

d
2
y
dx
2
=
2
x
3
2y
3
=
2
x
3
So we have
d
2
y
dx
2
2y
3
= 0
Boundary conditions:
y(x = 1) =
1
1
= 1 y(x = 2) =
1
2
Thus y(x) =
1
x
satises the dierential equation and the boundary conditions.
(b) Write a MATLAB code, solve the problem numerically using the shooting method.
Given the following denitions of variables
_

_
y
1
= y
y
2
=
dy
dt
9
The second-order ODE can be rewritten as
_

_
dy
1
dt
= y
2
dy
2
dt
= 2y
3
1
with the initial condition y
1
(1) = 1.
Using explicit Euler method to integrate. The two initial guesses of z
2
(1) are z
(1)
2
(1) = 0
and z
(2)
2
(1) = 2. The results are listed as follows:
i z
(i)
2
(1) z
(i)
1
(2)
1 0.0000 2.2690
2 -2.0000 -0.6926
3 -1.1946 0.2495
4 -0.9804 0.5344
5 -1.0063 0.4988
6 -1.0054 0.5000
where h = 0.1 and err < 0.001 Sample Code:
clear all;
node=11;
x_e=1;
h=x_e/(node-1);
y_end=1/2;
x=1:h:2;
dy=[0,-2];
j=1;
err=1;
while err>=0.001
if (j>2)
dy(j)=dy(j-2)+(y_end-y_hit(j-2))/(y_hit(j-1)-y_hit(j-2))*(dy(j-1)-dy(j-2));
end
y(1,:)=[1,dy(j)];
for i=1:node-1
y(i+1,:)=y(i,:)+h*func5_5(y(i,:));
end
y_hit(j)=y(end,1);
err=abs(y_hit(j)-y_end);
disp([int2str(j) & num2str(dy(j),%2.4f) & num2str(y_hit(j),%2.4f) \\])
j=j+1;
end
where func5 5 is dened as
function dy=func5_5(y)
10
dy(1)=y(2);
dy(2)=2*y(1)^3;
end
Plot:
1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2
0.4
0.5
0.6
0.7
0.8
0.9
1
Homework 5.5 Shooting method
x
y
(c) Write a MATLAB code, solve the problem numerically using the nite-dierence method
with 5 equidistant interior nodes.
For 5 interior nodes, there are 5 unknown variables y
2
, y
3
, y
4
, y
5
, y
6
. y
1
= 1 and y
7
=
1
2
are the boundaries conditions. Apply centered nite dierences to discretize the equation
and boundary conditions:
d
2
y
dx
2
=
y
i+1
2y
i
+ y
i1
h
2
where h is the lattice size of the discretization and h = 0.2 with 5 equidistant interior
nodes.
The discretized equation for node i = 2, 3, , 6 is
y
i+1
2y
i
+ y
i1
h
2
2y
3
i
= 0, i = 2, 3, . . . , 6
Dene
Y =
_

_
y
2
y
3
.
.
.
y
6
_

_
F(Y ) =
_

_
f
2
f
2
.
.
.
f
6
_

_
=
_

_
1
h
2
(y
3
2y
2
+ y
1
) 2y
3
2
1
h
2
(y
4
2y
3
+ y
2
) 2y
3
3
.
.
.
1
h
2
(y
N+1
2y
N
+ y
N1
) 2y
3
N
_

_
11
J = F(X) =
_

2
h
2
6y
2
2
1
h
2
0 0 0
1
h
2

2
h
2
6y
2
3
1
h
2
0 0
0
1
h
2

2
h
2
6y
2
4
1
h
2
0
0 0
1
h
2

2
h
2
6y
2
5
1
h
2
0 0 0
1
h
2

2
h
2
6y
2
6
_

_
J(Y
(M)
)
(M)
y
= F(Y
(M)
)
Y
(M+1)
= Y
(M)
+
(M)
y
= Y
(M)
+ J(Y
(M)
)
1
F(Y
(M)
)
Sample Code:
clear all;
node=7;
x=linspace(1,2,node);
delta_x=2/(node-1);
y_1=1;
y_n=1/2;
y(1,:)=zeros(1,node);
j=1;
err=1;
while err>=0.0001
J(1,1)=1;
J(node,node)=1;
F(1)=y(j,1)-y_1;
F(node)=y(j,node)-y_n;
for i=2:node-1
J(i,i-1)=1/delta_x^2;
J(i,i)=-2/delta_x^2-6*y(j,i)^2;
J(i,i+1)=1/delta_x^2;
F(i)=(y(j,i+1)-2*y(j,i)+y(j,i-1))/delta_x^2-2*y(j,i)^3;
end
y(j+1,:)=y(j,:)-(inv(J)*F);
err=0;
for i=2:node-1
err=err+(y(j+1,i)-y(j,i))^2;
end
j=j+1;
end
Plot:
12
1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2
0.4
0.5
0.6
0.7
0.8
0.9
1
Homework 5.5 Finitedifference
x
y
6. Consider the equations:
0 =
d
2
y
1
dx
2
y
2
1
+ y
2
0 =
d
2
y
2
dx
2
+ y
2
1
y
2
where y
1
(x = 0) = 1 and y
1
(x = 1) = 0, y
2
(x = 0) = 0 and y
2
(x = 1) = 1, that describe
molecular diusion and reaction of two chemical species in one spatial dimension at steady-
state.
(a) Sketch the one-dimensional domain for this problem, indicating nodes, node numbering
and numbering of the unknowns according to a consistent naming scheme.
(b) Show the form of the Jacobian and the residual vector needed to solve the above equations
using the nite dierence method (with centered nite dierences) and Newtons method.
(c) Use the nite-dierence method with 10 equidistant interior nodes to compute the solution
of the above problem (write the appropriate MATLAB code).
Solution:
(a) Sketch the one-dimensional domain for this problem, indicating nodes, node numbering
and numbering of the unknowns according to a consistent naming scheme.
The number of nodes in the plot is N. Thus there are 2N variables, namely y
1,1
, y
2,1
,
y
1,2
, y
2,2
, ... y
1,N
, y
2,N
, where the rst subindex denotes the substance and the second
denotes the position on the lattice. Since the boundary conditions are given as values at
the boundaries, the number of unknowns is 2(N 2). Correspondingly, there are 2(N 2)
equations from each interior nodes and substances.
13
1 i-1 i i+1 N
h
Apply centered nite dierences to discretize the equation and boundary conditions:
d
2
y
1
dx
2
=
y
1,i+1
2y
1,i
+ y
1,i1
h
2
d
2
y
2
dx
2
=
y
2,i+1
2y
2,i
+ y
2,i1
h
2
where h is the lattice size of the discretization and h = 0.2 with 5 equidistant interior
nodes.
The discretized equations for node i = 2, 3, , N 1 are
y
1,i+1
2y
1,i
+ y
1,i1
h
2
= y
2
1,i
y
2,i
, i = 2, 3, . . . , N 1
y
2,i+1
2y
2,i
+ y
2,i1
h
2
= y
2
1,i
+ y
2,i
, i = 2, 3, . . . , N 1
with the boundary conditions
y
1,1
= y
1
(x = 0) = 1, y
1,N
= y
1
(x = 1) = 0
y
2,1
= y
1
(x = 0) = 0, y
2,N
= y
1
(x = 1) = 1
(b) Show the form of the Jacobian and the residual vector needed to solve the above equations
using the nite dierence method (with centered nite dierences) and Newtons method.
Dene
Y =
_

_
y
1,2
y
1,3
.
.
.
y
1,N1
y
2,2
y
2,3
.
.
.
y
2,N1
_

_
F(Y ) =
_

_
y
1,3
2y
1,2
+ y
1,1
h
2
y
2
1,2
+ y
2,2
y
1,4
2y
1,3
+ y
1,2
h
2
y
2
1,3
+ y
2,3
.
.
.
y
1,N
2y
1,N1
+ y
1,N2
h
2
y
2
1,N1
+ y
2,N1
y
2,3
2y
2,2
+ y
2,1
h
2
+ y
2
1,2
y
2,2
y
2,4
2y
2,3
+ y
2,2
h
2
+ y
2
1,3
y
2,3
.
.
.
y
2,N
2y
2,N1
+ y
2,N2
h
2
+ y
2
1,N1
y
2,N1
_

_
14
J = F(X) =
_

2
h
2
2y
1,2
1
h
2
0 1 0 0
1
h
2

2
h
2
2y
1,3
0 0 1 0
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
0 0
2
h
2
2y
1,N1
0 0 1
2y
1,2
0 0
2
h
2
1
1
h
2
0
0 2y
1,3
0
1
h
2

2
h
2
1 0
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
0 0 2y
1,N1
0 0
2
h
2
1
_

_
J(Y
(M)
)
(M)
y
= F(Y
(M)
)
Y
(M+1)
= Y
(M)
+
(M)
y
= Y
(M)
+ J(Y
(M)
)
1
F(Y
(M)
)
(c) Use the nite-dierence method with 10 equidistant interior nodes to compute the solution
of the above problem (write the appropriate MATLAB code).
Sample Code:
N=12;
Y=zeros(2*N,1);
Y(1)=1;
Y(N)=0;
Y(N+1)=0;
Y(2*N)=1;
dx = 1/(N-1);
X=0:dx:1;
err=1;
tol=0.00001;
inner=[(2:N-1);(N+2:2*N-1)];
while(err>tol)
Y_Old=Y;
Y(inner)=Y(inner)-inv(prob6_J(Y,dx))*prob6_F(Y,dx);
err = sum((Y_Old-Y).^2);
end
plot(X,Y(1:N),b-)
hold on
plot(X,Y(N+1:2*N),r-)
where prob6 J returns the Jacobian matrix and is dened as
function J = prob6_J(Y,dx)
n=size(Y,1)/2;
J=zeros(2*(n-2),2*(n-2));
for i=2:n-1
ii=i+n;
j=i-1;
jj=n-2+j;
15
J(j,j)=-2/dx^2-2*Y(i); %dF(j)/dY(i)
if j<n-2
J(j,j+1)=1/dx^2; %dF(j)/dY(i+1)
end
if j>1
J(j,j-1)=1/dx^2; %dF(j)/dY(i-1)
end
J(j,jj)=1; %dF(j)/dY(ii)
J(jj,jj)=-2/dx^2-1; %dF(jj)/dY(ii)
if j<n-2
J(jj,jj+1)=1/dx^2; %dF(jj)/dY(ii+1)
end
if j>1
J(jj,jj-1)=1/dx^2; %dF(jj)/dY(ii-1)
end
J(jj,j)=2*Y(i); %dF(jj)/dY(i)
end
and prob6 F returns the function value and is dened as
function F=prob6_F(Y,dx)
n=size(Y,1)/2;
j=0;
F=zeros(2*(n-2),1);
for i=[2:n-1]
ii=i+n;
j=i-1;
jj=n-2+j;
F(j)=(Y(i+1)-2*Y(i)+Y(i-1))/dx^2-Y(i)^2+Y(ii);
F(jj)=(Y(ii+1)-2*Y(ii)+Y(ii-1))/dx^2-Y(i)^2+Y(ii);
end
Plot:
16
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Homework 5.6 Finitedifference / Multivariable Newtons Method
x
y


y
1
y
2
17

You might also like