为什么我的Matlab函数脚本不拾取我在主脚本中描述的变量?

3ks5zfa0  于 2023-04-21  发布在  Matlab
关注(0)|答案(1)|浏览(136)

有人可以帮助我修复我的代码吗?还有,为什么当我调用我在主脚本中定义的变量时,我的函数脚本中会出现错误?我已经定义了kprime,但当我运行函数脚本时,我没有得到任何响应。
主脚本:

%% Define constants
p.k=0.02; %lb.mol/atm.lbmcat.h @ 260°C
p.T=260; % Isothermal termperature in °C
p.P0=10; % Initial feed pressure (atm)
p.void=0.45; % bed void fraction
p.rho=120; % catalyst particle density(lbm/ft^3)
p.ttubes=1000; % Total tubes ( 10x100)
p.Dcatalyst=0.25/12; % diameter of catalyst particles(ft)
p.Dtubes=1.5/12; % diameter of tubes
p.ya0=0.24; % mole fraction Ethylene

%% Define parameters
Pa0=p.P0*p.ya0; % Partial pressure (atm)
Ac=pi*((p.Dcatalyst^2)/2); % cross-sectional area of particle
epsilon=p.ya0*-0.5; % Epsilon from stochiometry where delta = (1-0.5-1)=-0.5
kprime=p.k*Pa0*0.5^(2/3); %lb.mol/h.lbmcat

%% Define Weight region of interest
N=p.ttubes*Ac *(p.rho*(1-p.void)); % Final weight (lbm)
W= linspace (0,N,100); % Weight of catalyst across tube length (lbm)

%% Parameter inital conditions evaluation per tube
Fa0=(2.4*(10^-4))*3600; % Initial flowrate of A (lb mol/h) 
Fb0=((1.2*(10^-4))*3600);% Initial flowrate of B (lb mol/h)
Fi0=((1.2*(10^-4))*3600)*(0.79/0.21);% Initial flowrate of Inerts (N2) (lb mol/h)
F0= [Fa0;Fb0;Fi0]; % Vector of initial conditions
mA0=Fa0*28; % mass flowrate of A (lb/h)
mB0=Fb0*32; %mass flowrate of B (lb/h)
mI0=Fi0*28; %mass flowrate of I (lb/h)
mT=mA0+mB0+mI0; % total mass flowrate(lb/h)
G=mT/Ac; % Superficial mass velocity (lb/hft^2)
v=0.0673;   % superficial velocity (lbm/ft.h) (Tabulated data for air @260°C, 10atm)
rho0=0.413; %gas density (lb/ft^3) (Tabulated data for air @260°C, 10atm)
Beta=((G*(1-p.void))/((4.17*10^8)*rho0*p.Dcatalyst*(p.void^3))*((150*(1-p.void)*v )/p.Dcatalyst)+1.75*G ); % Beta value (atm/ft) Check eq
alpha=(2*Beta)/(Ac*(1-p.void)*p.rho*p.P0);% alpha value (/lbcat)

%% Define weight varying variables 
u.X=@(W) Conversion(W) % Conversion change with weight 
u.y=@(W) Pressure(W); % Pressure drop with variable weight
f=(1+epsilon*u.X(W))/y; % Ratio of volumetric flowrate down reactor
raprime=kprime*(1-u.X(W))/(1+epsilon*u.X(W)) *y; % Reaction rate with variable weight

function dXdW = Conversion(W, X)

dXdW= (kprime/Fa0)*((1-X)/(1+epsilon*X))*y ;

function dydW= Pressure(W,y)
dydW  =-(alpha)*(1+epsilon*X)/(2*y);
mf98qq94

mf98qq941#

一般来说,函数都有自己独立的工作区。()函数只看到传递给它的内容,即W和X。()函数只能看到W和y。您需要将这些其他变量传递到函数中,或者通过使用嵌套函数使它们对函数可见。例如,你可以把你的外部脚本变成一个函数,使其他函数嵌套:

function my_function % add this line

%% Define constants
p.k=0.02; %lb.mol/atm.lbmcat.h @ 260°C
    :
function dXdW = Conversion(W, X)
dXdW= (kprime/Fa0)*((1-X)/(1+epsilon*X))*y ;
end
function dydW= Pressure(W,y)
dydW  =-(alpha)*(1+epsilon*X)/(2*y);
end

end % add this line

现在最后两个函数将看到调用函数中的所有变量。你得到的其他错误是因为你没有用正确数量的输入参数调用最后两个函数。

相关问题