Multiple Inputs, Multiple Outputs

1 次查看(过去 30 天)
I have the following code:
f=@rate;
Y0 = [40; 125; 100; 250; 1500; 300; 0; 2; 0.5; 30; 8; 10; 10];
function output=rate(Y0)
%Y0 = [SI; SS; XI ; XS; XB_H ; XB_A ; XP; SO; SNO ; SNH ;SND ; XND ; S_ALK ];
%Y0 = [40; 125; 100; 250; 1500; 300; 0; 2; 0.5; 30; 8; 10; 10];
%j=Process
%i= component
%SI=40;
%SS=125;
%XI=100;
%XS=250;
%XB_H=1500;
%XB_A=300;
%XP=0;
%SO=2;
%SNO=0.5;
%SNH=30;
%SND=8;
%XND=10;
%S_ALK=10;
%%Process rate expression constants at 20C
mu_H=6; %1/day
mu_A=0.8; %1/day
KS=20; %gCOD/m^3
KNH=1; %gNH2-N/m^3
K_OH=0.2; %gO2/m^3
K_OA=0.4; %gO2/m^3
KNO=0.5; %gNO3-N/m^3
ng=0.8; %dimensionless
bH=0.62; %1/day
bA=0.2; %1/day
ka=0.08; %m^3*COD(g*day)^-1
kH=3; %g slowly biodegradable COD (g cell COD*day)^-1
KX=0.03; %g slowly biodegradable COD(g cell COD)^-1
nH=0.4; %dimensionless
%%Constants for coefficients at 20C
YH=0.67; %g cell COD formed (g N oxidized)^-1
iXB=0.086; %g N(g COD)^-1) in biomass
iXP=0.06; %g N(g COD)^-1 in endogenous mass
YA=0.24; %g cell COD formed (g N oxidized)^-1
fp=0.08; %dimensionless
P1= (mu_H)*(SS/(KS+SS))*(SO/(K_OH+SO))*(XB_H);
P2= (mu_H)*(SS/(KS+SS))*(K_OH/(K_OH+SO))*(SNO/(KNO+SNO))*(ng*XB_H);
P3= (mu_A)*(SNH/(KNH+SNH))*(SO/(K_OA+SO))*(XB_A);
P4= (bH)*(XB_H);
P5= (bA)*(XB_A);
P6= (ka)*(SND)*(XB_H);
P7= (kH)*((XS/XB_H)/((KX)+(XS/XB_H)))*((SO/(K_OH+SO))+(nH)*(K_OH/(K_OH+SO))*(SNO/(KNO+SNO)))*(XB_H);
P8= P7*(XND/XS);
%r_SI=0;
%r_SS=((-1/YH)*P1)+((-1/YH)*P2)+(1*P7);
%r_XI=0;
%r_XS=((1-fp)*P4)+((1-fp)*P5)+(-1*P7);
%r_XB_H=(1*P1)+(1*P2)+(-1*P4);
%r_XB_A=(1*P3)+(-1*P5);
%r_XP=((fp)*(P4))+((fp)*(P5));
%r_SO=((-((1-YH)/(YH)))*P1)+((-((4.57-YA)/(YA)))*P3);
%r_SNO=((-((1-YH)/(2.86*YH)))*P2)+((1/YA)*P3);
%r_SNH=((-1*iXB)*(P1))+((-1*iXB)*(P2))+((-iXB-(1/YA))*P3)+(P6);
%r_SND=(-1*P6)+(P8);
%r_XND=((iXB-(fp*iXP))*(P4))+((iXB-(fp*iXP))*(P5))+(-1*P8);
%r_S_ALK=(((-iXB/14))*(P1))+((((1-YH)/(14*(2.86*YH)))-(iXB/14))*(P2))+((((-iXB/14))-(1/(7*YA)))*(P3))+((1/14)*(P6));
output=[0;
((-1/YH)*P1)+((-1/YH)*P2)+(1*P7);
0;
((1-fp)*P4)+((1-fp)*P5)+(-1*P7);
(1*P1)+(1*P2)+(-1*P4);
(1*P3)+(-1*P5);
((fp)*(P4))+((fp)*(P5));
((-((1-YH)/(YH)))*P1)+((-((4.57-YA)/(YA)))*P3);
((-((1-YH)/(2.86*YH)))*P2)+((1/YA)*P3);
((-1*iXB)*(P1))+((-1*iXB)*(P2))+((-iXB-(1/YA))*P3)+(P6);
(-1*P6)+(P8);
((iXB-(fp*iXP))*(P4))+((iXB-(fp*iXP))*(P5))+(-1*P8);
((-(iXB/14))*P1)+(((1-YH)/(14*(2.86*YH))-(iXB/14))*P2)+(((-(iXB/14))-(1/7*YA))*P3)+((1/14)*P6)];
end
All I want to do is input any column vector of 13 numerical values, have each of them be inputted in their respective equation, and I want MATLAB to tell me what the output values are. How do I do this? Thanks in advance.

采纳的回答

Mehmed Saad
Mehmed Saad 2020-4-24
编辑:Mehmed Saad 2020-4-24
This is not a good coding approach
SI=Y0(1);
SS=Y0(2);
XI=Y0(3);
XS=Y0(4);
XB_H=Y0(5);
XB_A=Y0(6);
XP=Y0(7);
SO=Y0(8);
SNO=Y0(9);
SNH=Y0(10);
SND=Y0(11);
XND=Y0(12);
S_ALK=Y0(13);
or you can insert this in the start of function
tx={'SI'; 'SS'; 'XI' ; 'XS'; 'XB_H' ; 'XB_A' ; 'XP'; 'SO'; 'SNO' ; 'SNH' ;'SND' ; 'XND' ; 'S_ALK'};
for i=1:length(Y0)
feval(@()assignin('caller',tx{i},Y0(i)));
end
  5 个评论
Ameer Hamza
Ameer Hamza 2020-4-24
"good coding approach is to directly use variable Y"
Not always. Good coding approach is the one that makes your code easy to understand and debug. For example, If the OP is trying to implement a system of mathematical equations. It can be much more important to use consistent notation, then using an array. For example, consider following two codes. I am sure the first code is much more readable and easy to debug as compared to the second code.
Using the individual variable name:
eq1 = alpha + beta^3 + gamma/2 + eta;
eq2 = log(gamma) + sqrt(eta) + sin(alpha);
eq3 = exp(eta) - beta;
Using array:
eq1 = y(1) + y(2)^3 + y(3)/2 + y(4);
eq2 = log(y(3)) + sqrt(y(4)) + sin(y(1));
eq3 = exp(y(4)) - y(2);

请先登录,再进行评论。

更多回答(1 个)

Ameer Hamza
Ameer Hamza 2020-4-24
Create a file named rate.m and save the code of function in that file. The code of the function includes the lines between
function output=rate(Y0)
% function code
end
inside the function, uncomment these lines and write them like this
SI=Y0(1);
SS=Y0(2);
XI=Y0(3);
XS=Y0(4);
XB_H=Y0(5);
XB_A=Y0(6);
XP=Y0(7);
SO=Y0(8);
SNO=Y0(9);
SNH=Y0(10);
SND=Y0(11);
XND=Y0(12);
S_ALK=Y0(13);
Then call the function like this
Y0 = [40; 125; 100; 250; 1500; 300; 0; 2; 0.5; 30; 8; 10; 10];
out = rate(Y0);
  1 个评论
JR
JR 2020-4-24
Thank you so much. Your way worked as well as the individual above.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by