0% found this document useful (0 votes)
9 views3 pages

Step Input CSTR Matlab Code

This MATLAB code simulates the axial dispersion model for a Continuous Stirred Tank Reactor (CSTR) using both finite difference and analytical methods. It calculates the step response and residence time distribution (RTD) based on user-defined parameters such as reactor volume, diameter, flow rates, and dispersion number. The results are visualized through plots comparing the numerical and analytical solutions for the outlet concentration and RTD.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Step Input CSTR Matlab Code

This MATLAB code simulates the axial dispersion model for a Continuous Stirred Tank Reactor (CSTR) using both finite difference and analytical methods. It calculates the step response and residence time distribution (RTD) based on user-defined parameters such as reactor volume, diameter, flow rates, and dispersion number. The results are visualized through plots comparing the numerical and analytical solutions for the outlet concentration and RTD.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

% MATLAB Code for Axial Dispersion Model with Step Input in CSTR

clear all; close all; clc;


v = input('volume of reactor (L) = ');
L = input('CSTR diameter (cm) = ');
q = input ('Only Tracer flow rate (LPH)= ');
qt = input ('Total flow rate after tracer input (LPH)= ');
u = qt*1000/(60*3.14*(L^2));
tau = v*60/qt;
DN = input ('Dispersion number = ');
D = DN*u*L;
Pe = 1/DN;
C0 = input ('Step input concentration (M)= ');
tmax = 350;
nt = 500;
nx = 50;
dx = L/(nx);
dt = tmax/(tau*nt);
alpha = DN*dt/dx^2;
beta = dt/dx;
if alpha>0.4 || beta >1
warning ('Numerical scheme may be unstable, consider reducing dt or
increasing dx');
end

x = linspace(0, L, nx)';
t = linspace(0, tmax, nt);
C = zeros(nx, nt);
C(:, 1) = 0;
C(1, :) = C0;
for j = 1:nt-1
for i = 2:nx-1
C(i, j+1) = C(i, j) + alpha * (C(i+1, j) - 2*C(i, j) + C(i-1, j)) -
beta * (C(i, j) - C(i-1, j));
end
C(nx, j+1) = C(nx-1, j+1);
end
F_n = C(nx,:)/C0;
E_n = gradient (F_n,dt);
E_n = E_n/trapz(t,E_n);
tm_n = trapz(t, t.*E_n);
sigma2_n = trapz(t, (t-tm_n).^2.*E_n);

figure(1)

subplot(2,1,1)
plot(t, F_n, 'b-', 'LineWidth', 2);
title('Step Response at CSTR Outlet (Finite diffrence solution');
xlabel('Time (min)');
ylabel('F-curve -->');
legend('F-curve');
grid on
subplot(2,1,2)
plot(t, E_n, 'r--', 'LineWidth', 1.5);
title(['RTD Response (Step input) of CSTR Outlet (Finite diffrence
solution)\newlineMean residence time = ',...
num2str(tm_n,3), ' s, Variance (num) = ', num2str(sigma2_n,4),' s^2']);
xlabel('Time (min)');
ylabel('E-curve -->');
legend('E-curve');
grid on

Ca = zeros(nx, nt);
for j = 1:nt
for i = 1:nx
if t(j) > 0
term1 = 0.5 * erfc((x(i) - u*t(j))/(2*sqrt(D*t(j))));
term2 = 0.5 * exp(u*x(i)/D) * erfc((x(i) +
u*t(j))/(2*sqrt(D*t(j))));
Ca(i, j) = C0 * (term1 + term2);
end
end
end
F_a = Ca(nx,:)/C0;
E_a = gradient (F_a,dt);
E_a = E_a/trapz(t,E_a);
tm_a = trapz(t, t.*E_a);
sigma2_a = trapz(t, (t-tm_a).^2.*E_a);

figure(2);
subplot(2,1,1)
plot(t, F_a, 'b-', 'LineWidth', 2);
title('Step Response at CSTR Outlet (Analytical Solution)');
xlabel('Time (min)');
ylabel('F-crve -->');
legend('F-curve');
grid on
subplot(2,1,2)
plot(t, E_a, 'r--', 'LineWidth', 1.5);
title(['RTD Response (Step input) of CSTR Outlet (Analytical
Solution)\newlineMean residence time = ',...
num2str(tm_a,3), ' s, Variance (analytic) = ', num2str(sigma2_a,4),'
s^2']);
xlabel('Time (min)');
ylabel('E-curve -->');
legend('E-curve');
grid on

fprintf('Average fluid velocity (u) = %.2f cm/min\n', u);


fprintf('Theoratical avg residence time = %.2f s\n', tau);
fprintf('Avg residence time (Finite diff.) = %.2f s\n', tm_n);
fprintf('Avg residence time (Analytical) = %.2f s\n', tm_a)
fprintf('Dispersion coefficient, D = %.2f\n', D);
fprintf('Peclet number (Pe = uL/D) = %.2f\n', Pe);

figure(3);
plot(t, F_n, 'b-', 'LineWidth', 2);
hold on;
plot(t, F_a, 'r--', 'LineWidth', 1.5);
title('Step Response at CSTR Outlet \newlineComparison b/w Solutions of
Nemerical method and Analytical metod');
xlabel('Time (min)');
ylabel('F -->');
legend('Numerical solution Response', 'Analytical solution response');
grid on;

figure(4);
plot(t, E_n, 'b-', 'LineWidth', 2);
hold on;
plot(t, E_a, 'r--', 'LineWidth', 1.5);
title('RTD of CSTR for step input \newlineComparison b/w RTDs of Nemerical
method and Analytical metod');
xlabel('Time (min)');
ylabel('E -->');
legend('Numerical solution RTD', 'Analytical solution RTD');
grid on;

You might also like