- ode15s: https://www.mathworks.com/help/matlab/ref/ode15s.html
- function: https://www.mathworks.com/help/matlab/ref/function.html
Trying to plot variables as a function of time with a coupled ode
1 view (last 30 days)
Show older comments
I have to use 5 ODEs (Ca, Cb, Cs, P, T) all coupled togeher to Plot the reactor temperature, the head space pressure, and the concentrations of A, B, S, D as function of time. I am not familiar with odes and the recommended ode to use is ode15s because of stiffness. There are two reactions for this process: A + B โ C + ยฝ D (gas) S โ 3 D (gas) + misc liquids and solids
I am trying to use these equations to make this coupled ode
r1๐ด = โ๐๐ด๐ถ๐ด๐ถB
๐2๐ = โ๐๐๐ถs
Assuming that the volume of the batch does not change, the mole balances for the reaction liquid yield: ๐๐ถ๐ด/๐๐ก = ๐1๐ด = โ๐๐ด๐ถ๐ด๐ถ๐ต , d๐ถ๐ต/๐๐ก = ๐1๐ต = ๐1๐ด = โ๐๐ด๐ถ๐ด๐ถ๐ต , ๐๐ถ๐/๐๐ก = ๐2๐ = โ๐๐๐ถS
๐๐/d๐ก = (๐น๐ท โ ๐น๐ฃ๐๐๐ก) ๐
๐/๐H
I believe to calculate temperature as a function of time I can use one of these equations:
k(๐) = ๐0๐๐ฅ๐ (โ ๐ธ๐ /RT)
๐๐/d๐ก = (๐0(๐1๐ดโ๐ป๐๐ฅ๐,1 + ๐2๐โ๐ป๐๐ฅ๐,2) โ ๐๐ด(๐ โ ๐๐ )) / โ ๐๐๐ถ๐,i
I am struggling to be able to couple all these odes together to be able to calculate all these different variables as a function of time. I would be very appreciative if someone could help me understand how to do this and help with the code. If to help you need any extra data just ask and i'll give it if available.
Thanks
0 Comments
Answers (1)
Abhishek Chakram
on 16 Nov 2023
Edited: Abhishek Chakram
on 16 Nov 2023
Hi Tom Goodland,
It appears to me that you are facing difficulty in plotting variables as a function of time with a coupled ode. One of the ways of achieving this will be using "ode15s" and passing a custom function to it. Here is a sample code for the same:
y0 = [10; 10; 10; 1; 300]; % Initial values of CA, CB, CS, P, T
tspan = [0 100]; % Time span from 0 to 100 seconds
options = odeset('RelTol',1e-6,'AbsTol',1e-9); % Set the solver options
[t,y] = ode15s(@myODEs,tspan,y0,options); % Solve the ODEs
plot(t,y(:,1),'r',t,y(:,2),'g',t,y(:,3),'b') % Plot CA, CB, and CS vs time
xlabel('Time (s)')
ylabel('Concentration (mol/L)')
legend('CA','CB','CS')
figure % Create a new figure
plot(t,y(:,4),'k') % Plot P vs time
xlabel('Time (s)')
ylabel('Pressure (Pa)')
figure % Create a new figure
plot(t,y(:,5),'m') % Plot T vs time
xlabel('Time (s)')
ylabel('Temperature (K)')
function dydt = myODEs(t,y)
% Define the parameters and constants
kA = 0.005;
kS = 0.1;
S = 100;
P = 0;
E = 10;
C = 0;
R = 8.314;
V0 = 1;
VH = 1; % Assume some value for the head space volume
FD = 0.01; % Assume some value for the molar flow rate of D
Fvent = 0.02; % Assume some value for the venting flow rate
U = 0.5; % Assume some value for the overall heat transfer coefficient
A = 1; % Assume some value for the heat transfer area
Ta = 300; % Assume some value for the ambient temperature
Cp = 1; % Assume some value for the heat capacity of the liquid
dHrxn1 = -100; % Assume some value for the enthalpy change of reaction 1
dHrxn2 = -200; % Assume some value for the enthalpy change of reaction 2
k0 = 1; % Assume some value for the pre-exponential factor
Ea = 10000; % Assume some value for the activation energy
% Define the reaction rates
r1A = -kA*y(1)*y(2); % y(1) is CA, y(2) is CB
r2S = -kS*y(3); % y(3) is CS
% Define the ODEs
dydt = zeros(5,1); % Initialize the output vector
dydt(1) = r1A; % dCA/dt
dydt(2) = r1A; % dCB/dt
dydt(3) = r2S; % dCS/dt
dydt(4) = (FD - Fvent)*R*y(5)/VH; % dP/dt, y(5) is T
dydt(5) = (V0*(r1A*dHrxn1 + r2S*dHrxn2) - U*A*(y(5) - Ta))/(sum(y(1:3))*Cp); % dT/dt
end
Kindly refer to the following documentation to know more about the functions used:
Best Regards,
Abhishek Chakram
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!