ODE45 code won't run

조회 수: 11 (최근 30일)
Najeem Afolabi
Najeem Afolabi 2020년 12월 25일
댓글: Najeem Afolabi 2020년 12월 27일
So I have this complex reaction model that I made but it just won't run. It doesn't say that there is an error necessarily, it just tries to run for a really long time. I eventually stop the the attempt and it takes me to the ode45 function script and points at a line. This is the function and code that I'm using.
clear all; close all;
clc
%Initial Concentrations
CA0 = 0.5; %mol/L
CB0 = 0.1; %mol/L
Qoa = 0.000025; %L/s
Qob = 0.000025; %L/s
Q1 = Qob + Qoa;
CAii1 = Qoa*CA0/Q1; CB1 = Qob*CB0/Q1; CAiiB = 0; CAiB = 0; CAi = 0; CC1 = 0; CD1 = 0;
initCon = [CAii1, CB1, CAiiB, CAi, CAiB, CC1, CD1];
V = 10*10^-3;
k2 = 0.01;
%Residence Time
tau = V/Q1;
%Volume
tauRange = [0:0.2:tau]; %s
%ODE SOLVER
[tauRange, C1] = ode45(@diffpfr, tauRange, initCon, []);
%PLOT PFR
figure (1)
plot(tauRange, C1,'Linewidth',1)
function dcdv = diffpfr(tau, C)
a = 2; b = 0.25; c = 1; d = 0.25;
k1 = 1; k1_ = 1; k2_ = 0.25;k2 = 0.25;
%Rate Equations
rAii = - a * C(1)*C(2) + b * C(3);
rB = - a* C(1)* C(2) + b * C(3) - c * C(4) * C(2) + d * C(7);
rAiiB = -k1 * C(4) * C(5) - k2 * C(4) * C(6) + a * C(1)*C(2) - b * C(3);
rAi = k1 * C(4) * C(5) + k2 * C(4) * C(6) - c * C(4) * C(2) + d * C(7);
rAiB = c * C(4) * C(2) - d * C(7) - k1_ * C(7) - k2_ * C(7);
rC = k1 * C(3) + k1_ * C(7);
rD = k2 * C(3) + k2_ * C(7);
%differential equations
dcdv = [ rAii;rB; rAiiB; rAi; rAiB; rC; rD];
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 12월 25일
Experiment with using ode23s() as perhaps your ode is stiff.
Najeem Afolabi
Najeem Afolabi 2020년 12월 27일
Yeah, I tried that too but still no luck unfortunately.

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 12월 27일
편집: Cris LaPierre 2020년 12월 27일
There's something with your tau. Right now it is 200. When I make it <=30 I get it to run very quickly.
If instead I use ode15s, it works as is.
%Initial Concentrations
CA0 = 0.5; %mol/L
CB0 = 0.1; %mol/L
Qoa = 0.000025; %L/s
Qob = 0.000025; %L/s
Q1 = Qob + Qoa;
CAii1 = Qoa*CA0/Q1;
CB1 = Qob*CB0/Q1;
CAiiB = 0;
CAiB = 0;
CAi = 0;
CC1 = 0;
CD1 = 0;
initCon = [CAii1, CB1, CAiiB, CAi, CAiB, CC1, CD1];
V = 10*10^-3;
k2 = 0.01;
%Residence Time
tau = 200;%V/Q1;
%Volume
tauRange = [0 tau]; %s
%ODE SOLVER ### Switch to ode15s
[tauRange, C1] = ode15s(@diffpfr, tauRange, initCon);
%PLOT PFR
plot(tauRange, C1,'Linewidth',1)
function dcdv = diffpfr(tau, C)
a = 2; b = 0.25; c = 1; d = 0.25;
k1 = 1; k1_ = 1; k2_ = 0.25;k2 = 0.25;
%Rate Equations
rAii = - a*C(1)*C(2) + b*C(3);
rB = - a*C(1)*C(2) + b*C(3) - c*C(4)*C(2) + d*C(7);
rAiiB = -k1*C(4)*C(5) - k2*C(4)*C(6) + a*C(1)*C(2) - b*C(3);
rAi = k1*C(4)*C(5) + k2*C(4)*C(6) - c*C(4)*C(2) + d*C(7);
rAiB = c*C(4)*C(2) - d*C(7) - k1_*C(7) - k2_*C(7);
rC = k1*C(3) + k1_*C(7);
rD = k2*C(3) + k2_*C(7);
%differential equations
dcdv = [ rAii; rB; rAiiB; rAi; rAiB; rC; rD];
end
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2020년 12월 27일
For more guidance on choosing a solver, see the Basic Solver Selection setion of the Choose and ODE Solver page.
Najeem Afolabi
Najeem Afolabi 2020년 12월 27일
Ahh I see. Thank you very much. I'll apply this now.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by