You are on page 1of 5

Modeling Mass Transport with GNU Octave

Lecturer: Habiburrahman (Zulfikri), S.Si., M.Sc., Ph.D.


Department of Chemical Engineering, Universitas Indonesia

December 8th/9th, 2022. This material is not to be distributed outside the class.

1 Introduction to GNU Octave


GNU Octave as a calculator
>> 6^3 - (7 + 5)/2 + 9*4 >> while a*10 <= 100
ans = 246 disp(a*5);
>> (-2+sqrt(6))/2 a = a + 1;
ans = 0.2247 endwhile
>> 2^(-48) 5
ans = 3.5527e-15 10
>> ans^3 + sqrt(ans) 15
ans = 5.9605e-08 20
>> sin(90) 25
ans = 0.89400 30
>> sind(90) 35
ans = 1 40
>> sin(pi) 45
ans = 1.2246e-16 50
>> a = 1;

GNU Octave as an ODE solver


Let us consider a simple ODE as follow

dy
= −y − 5 exp(−t) sin(5t)
dt
y(t = 0) = y0 = 1

Figure 1: Solutions of a simple ODE

1
We can write the function file ode1.m defining the ODE equation to be solved:

%ode1.m
function dydt = ode1(t,y)
dydt = - y - 5*exp(-t)*sin(5*t);
endfunction

The next file, useode1.m, utilizes the defined function and plot the solutions. In the command prompt,
type useode1 to execute the script.

%useode1.m
tint = 0:0.1:3;
y0 = 1;
[t,y] = ode45(@ode1,tint,y0);
plot(t,y)

Alternatively, we can write everything in the command prompt as

clear all
dy = @(t,y) [-y - 5*exp(-t)*sin(5*t)];
tint = 0:0.01:3;
y0 = 1;
[t,y] = ode45(dy,tint,y0);
plot(t,y)

The dsolve built-in function in GNU Octave can symbolically provide analytical solution of an ODE,
which for our example above, is y = e−t cos 5t.

clear all
syms y(t)
DE = diff(y,t) == -y - 5*exp(-t)*sin(5*t)
sol = dsolve (DE, y(0) == 1)

Exercise
For every cases presented in the lecture about macroscopic mass transport:

1. Obtain the analytical solution using dsolve


2. Plot the solution obtained from the application of ode45 built-in function using reasonable as-
sumption on the values of contants.

2 One-Dimensional Diffusion
The presented case here is similar to that found in section 18.2 of the second-edition ”Transport Phenom-
ena” book of Bird, Lightfoot and Stewart. Consider binary gas-phase diffusion of component A during
evaporation of a pure liquid in a simple diffusion tube. A figure below shows a cylindrical tube where
liquid A is evaporating into a gas mixture of A and B from a liquid layer of pure A near the bottom of
the tube. Fick’s law for the flux of A can be expressed as
dNA
= 0 (1)
dz
dxA (1 − xA )NA
= − (2)
dz cDAB
−3
c = total concentration (kg-mol m )
2
DAB = molecular diffusivity of A in B (m · s−1 )
−2
NA = mole flux of A (kg-mol m · s−1 )
xA = mole fraction of A

2
Figure 2: Gas-phase diffusion of A through gas mixture of A and B

At z = z1 , xA is given by xA = xA1 = PA0 /P . The gas mixture is assumed to be ideal gas. Then, at
constant temperature and pressure, the total concentration c equals P/RT and DAB can be considered
constant. The concentration profile may be expressed as
 (z−z1 )/(z2 −z1 )
1 − xA 1 − xA2
= (3)
1 − xA1 1 − xA1
xA1 − xA2
NAz z=z = cDAB (4)
1 (z2 − z1 )xB,lm
xB2 − xB1 xA1 − xA2
xB,lm = xB2 =  1 − xA2  (5)
ln ln
xB1 1 − xA1

Problem
Methanol (A) is evaporated into a stream of dry air (B) in a cylindrical tube at 328.5 K. The distance
from the tube inlet to the liquid surface is z2 − z1 = 0.238 m. At T = 328.5 K, the vapor pressure of
methanol is PA0 = 68.4 kPa and the total pressure is P = 99.4 kPa. The binary molecular diffusion
coefficient of methanol in air under these conditions is DAB = 1.991 × 10−5 m2 /s. Calculate the constant
molar flux of methanol within the tube at steady state and plot the mole fraction profile of methanol
from the liquid surface to the flowing air stream. Compare the calculated molar flux with that obtained
analytically.

Solution
(1−xA )NA
At z = z1 , xA1 = P/PA0 . We guess NA , solve the differential equation dx dz = − cDAB
A
, and check
whether the condition xA2 = 0 is satisfied. If xA2 ̸= 0, the calculation procedure is repeated by using
another initial guess of NA . The bisection method can be effectively used in the iterative calculation
procedure. Let us define a GNU Octave function xaz as

function dxdz = xaz(z,x,Na,Dab,C)


dxdz = -(1-x)*Na/Dab/C;
endfunction

Copy the above script and save the file as xaz.m. The script usexaz.m employs the built-in function
ode45 to solve the differential equation and determines NA by using the bisection method.

% usexaz.m
clc ; clear all ;
Dab = 1.991e-5;
T = 328.5;
R = 8.314;
P = 99.4;

3
Figure 3: Illustration of the bisection method

Pa0 = 68.4;
z2 = 0.238;
z1 = 0;
zv = [z1 z2];
C = P/(R*T);
x0 = Pa0/P;
critN = 1e-11;
errN = 1;
Na1 = 3.0e-6;
Na2 = 4.0e-6;
while errN > critN
Nam = (Na1+Na2)/2;
[z x1] = ode45(@xaz,zv,x0,Na1,Dab,C);
[z x2] = ode45(@xaz,zv,x0,Na2,Dab,C);
[z xm] = ode45(@xaz,zv,x0,Nam,Dab,C);
if x1(end)*xm(end) < 0
Na2 = Nam;
else
Na1 = Nam;
endif
errN = abs(Na1 - Na2);
endwhile
xblm = x0/(log(1/(1-x0)));
Nanal = Dab*C*x0/((z2-z1)*xblm);
fprintf(’Estimated Nab = %e, xA = %8.6f\n’,Nam, xm(end));
fprintf(’Analytic Nab = %e\n’,Nanal);
plot(z,xm), xlabel(’z(m)’), ylabel(’x_A’), axis tight
Run the script, and the results will be printed.
>> usexaz
Estimated Nab = 3.547508e-06, xA = -0.000002
Analytic Nab = 3.547502e-06

Exercise
1. To understand the script, play with the different value of {Na1, Na2} where abs(Na1 - Na2) =
0.1e-06 !

4
Figure 4: Solutions to the binary diffusion problem

2. Obtain twenty values of {NA , xA2 } from the previous task, and shown them on a table! Plot the
curve to demonstrate how the bisection method works!

3. Compute the rate of evaporation and use fprintf to print the results!
4. Explain how the Runge−Kutta method works in the ode45 built-in function of GNU Octave!
5. Compute twenty values of xA at different z using equation (3), plot the xA -vs-z curve, and compare
your results with that obtained from the usezax.m script (Figure 4)!

You might also like