Undefined function or variable using ODE45

I'm having trouble with defining an independent variable in matlab. I am getting the error, 'undefined function or variable 'Y'. Y is the variable I am trying to solve for.
%elementary rate law
FA0=10; %mol/min
W=100; %KG
a=.008; %kg^-1
k1C=2; %L/kg cat/min
K1C=.2; %mol/L
k2D=.4; %L/kg cat/min
k3E=5; %L/mol^2/kg cat/min
kc=1; %L/kg cat/min
v=16.67; %L/min
Fa=Y(1);
Fb=Y(2);
Fc=Y(3);
Fd=Y(4);
Fe=Y(5);
y=Y(6);
CT0=FA0;
FT=Fa+Fb+Fc+Fd+Fe;
Ca=CT0*(Fa/FT)*y;
Cb=CT0*(Fb/FT)*y;
Cc=CT0*(Fc/FT)*y;
Cd=CT0*(Fd/FT)*y;
Ce=CT0*(Fe/FT)*y;
r1C=k1C*(Ca-(Cb*Cc/K1C));
r2D=k2D*Cd;
r3E=k3E*Cd*(Ce^2);
ra=-r1C-r2D;
rb=r1C;
rc=r1C-r3E;
rd=r2D-.5*r3E;
re=r3E;
dFadw=ra;
dFbdw=rb;
dFcdw=rc;
dFddw=rd;
dFedw=re;
dydw=-a*FT/(2*FT0*y);
fun=@(w,Y)[dFadw;dFbdw;dFcdw;dFddw;dFedw,dydw];
wspan=0:1:100;
x0=[10;0;0;0;0;1];
[T,X] = ode45(fun,wspan,x0)

 采纳的回答

I believe this is what you actually intend:
function dy = fun(T,Y)
FT0 = 42;
FA0=10; %mol/min
W=100; %KG
a=.008; %kg^-1
k1C=2; %L/kg cat/min
K1C=.2; %mol/L
k2D=.4; %L/kg cat/min
k3E=5; %L/mol^2/kg cat/min
kc=1; %L/kg cat/min
v=16.67; %L/min
Fa=Y(1);
Fb=Y(2);
Fc=Y(3);
Fd=Y(4);
Fe=Y(5);
y=Y(6);
CT0=FA0;
FT=Fa+Fb+Fc+Fd+Fe;
Ca=CT0*(Fa/FT)*y;
Cb=CT0*(Fb/FT)*y;
Cc=CT0*(Fc/FT)*y;
Cd=CT0*(Fd/FT)*y;
Ce=CT0*(Fe/FT)*y;
r1C=k1C*(Ca-(Cb*Cc/K1C));
r2D=k2D*Cd;
r3E=k3E*Cd*(Ce^2);
ra=-r1C-r2D;
rb=r1C;
rc=r1C-r3E;
rd=r2D-.5*r3E;
re=r3E;
dFadw=ra;
dFbdw=rb;
dFcdw=rc;
dFddw=rd;
dFedw=re;
dydw=-a*FT/(2*FT0*y);
dy = [dFadw;dFbdw;dFcdw;dFddw;dFedw;dydw];
end
wspan=0:1:100;
x0=[10;0;0;0;0;1];
[T,X] = ode45(@fun,wspan,x0);
figure
plot(T,X)
grid
The ‘FT0’ value and assignment are missing, so I created one to test the code. (You may want to substitute a different value.) This runs without error.

3 个评论

Thank you. I've put in the value of .6 for CT0, but now the code takes forever to run. It has been running for 10 min so far. Is this just becuase I have 6 equations with 6 unknowns? Is there any way to optimize this?
Nevermind, I had some incorrect values. Your fix worked, thanks!
As always, my pleasure!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrix Computations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by