-
Notifications
You must be signed in to change notification settings - Fork 2
/
Robertson_kinetics.m
84 lines (67 loc) · 1.82 KB
/
Robertson_kinetics.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function Robertson_kinetics
%% Batch reactor with Robertson's reaction system
%
% You'll learn:
% +: How to solve transient problems in ODE formulation
% +: The differences between explicit and implicit solvers
% +: How to pass the jacobian matrix to the integrator
%
%% The problem
%
% Differential equations:
% dy1/dt = -k1y1 + k2y2y3
% dy2/dt = k1y1 - k2y2y3 -k3y2^2
% dy3/dt = k3y2^2;
%
% Initial conditions
% t = 0 ... y1 = 1
% y2 = 0
% y3 = 0
%
% ============================================================
% Author: [email protected]
% homepage: github.com/asanet
% Date: 2018-07-05
% Matlab version: R2018a
% Contact me for help/personal classes!
%% Problem setting
addpath('AuxFunctions')
% The kinetic constants
k1 = 0.04;
k2 = 1e4;
k3 = 3e7;
% Time interval and initial conditions
tspan = [0 1e8];
y0 = [1 0 0]';
% Pass the analytic jacobian matrix to the odesolver
opt = odeset('Jacobian',@jacobian);
% Solve the model (what if you use a explicit integrator such as ode45?)
tic
[t,y] = ode15s(@model,tspan,y0,opt);
toc
% Plot the data
figured;
h = semilogx(t,y(:,1),t,y(:,2)*1e4,t,y(:,3));
set(h,'LineWidth',1.5);
ylabel('Molar fraction')
xlabel('Time')
axis([t(2) t(end) 0 1])
legend({'y_1','y_2\times 10^4','y_3'})
% The model
function dy = model(~,y)
dy(1,1) = -k1*y(1) + k2*y(2)*y(3);
dy(2,1) = k1*y(1) - k2*y(2)*y(3) - k3*y(2)^2;
dy(3,1) = k3*y(2)^2;
end
function J = jacobian(~,y)
J(1,1) = -k1;
J(1,2) = k2*y(3);
J(1,3) = k2*y(2);
J(2,1) = k1;
J(2,2) = -k2*y(3) - 2*k3*y(2);
J(2,3) = -k2*y(2);
J(3,1) = 0;
J(3,2) = 2*k3*y(2);
J(3,3) = 0;
end
end