How to use the output of one function as an input of another function

23 Ansichten (letzte 30 Tage)
Tadele
Tadele am 3 Jan. 2023
Beantwortet: Nehemiae am 8 Mär. 2023
How can I assign the output of one function as an input for the second and use the output of the second function as an input for the third function and so on to run an algorithm using these outputs. Below are some of my functions that I want to use the output of one function as input for the other. Can some one help, please? If the file is too much can you show with simpler examples?
The full version of matlab script I have tried is found in the attachement.
1.ODE
lets say we have the the script below with variables x(1),x(2), x(3), x(4), x(5) which I need the end result of these variables to use for the second equation:
The fist equation is
function dxdz = MS_AA_imp(z,x,xp)
TempK = 25 + 273.15; % Temp in Kelvin
Faraday = 96485.336; % Faraday constant [C/mol]
R_id = 8.314; % R constant [J/(K mol)]
VM_1 = 1.8e-5; % AA molar volume of lysine [m3/mol] ---converted to m3--which was 18 cm3/mol
VM_2 = 1.57e-5; % Na molar volume [m3/mol] * Calculated from rS
VM_3 = 4.47e-6; % Cl molar volume [m3/mol] * calcualted form rS
VM_4 = 1.8e-5; % Water molar volume [m3/mol]
D_24 = 1.33E-9; % D24 value under diluted conditions [m^2/s]
D_34 = 2.03E-9; % D34 value under diluted conditions [m^2/s]
%x_5 = x(5); %psi.
V=0.000255;
h=7;
hydration= x(1)/((x(1))*(1-x(1)*h)*(R_id*TempK)); % new hydration formula
CT = x(1) / VM_1 + x(2)/ VM_2 + x(3) / VM_3 + (1-sum(x)) / VM_4;
Z=[-1 1 -1];
D_14 =10e-9 *(0.665-0.393*x(1))/(1+x(1)* (h/(1-x(1)* h))); % new D_14 formula
IStr = 0.5* (Z(2)^2 * x(2)/(VM_2 *1000) + Z(3)^2 * x(3)/(VM_3 *1000));
D_23 = ((D_24 + D_34)/2 * IStr ^(0.55) / (abs((Z(2)) * (Z(3)))^(2.3)));
x_p=[0.001 0.002 0.0001];
x(5)=1-x(1)+x(2)+x(3);
J_1 = x_p(1) * V;
J_2 = x_p(2) * V;
J_3 = x_p(3) * V;
J_4 = (1-sum(x_p)) * V;
%equations to be solved(psi changed with xp(4)
dxdz = [hydration * xp(1) + x(1) * Z(1) * Faraday / (R_id .* TempK)*xp(4) - (J_4 * x(1) - J_1* x(4))/(D_14 * CT);
xp(2) + x(2) * Z(2) * Faraday*xp(5) / ( R_id * TempK) - (J_3 * x(2) - J_2 * x(3))/ D_23 - (J_4 * x(2) - J_2 * x(4))/ (D_24 *CT);
xp(3) + x(3) * Z(3) * Faraday*xp(5) / ( R_id * TempK) - (J_2 * x(3) - J_3 * x(2))/ D_23 - (J_4 * x(3) - J_3 * x(4))/ (D_34* CT); %D_32V replaced with D_
x(1) + x(2) + x(3) + x(4) - 1;
Z(1) * x(1) + Z(2)* x(2) + Z(3) * x(3)];
end
calling ode15i for the above function (the calling function looks like)
y0 = [0.001 0.002 0.003 0.994 0];
yp0 = [0; 0; 0; 0; 0];
[y0,yp0] = decic(@MS_AA_imp,0,y0,[0 0 0 0],yp0,[]);
% Solve the system of DAEs using |ode15i|.
tspan = [0 1.02e-3];
[z,y] = ode15i(@MS_AA_imp,tspan,y0,yp0);
x_end = y(end,:)
the result of the above called ode15i function appears as
x_end =
-0.0000 0.0027 0.0027 0.9946 -0.0021
To run the algorithm, I want to put this output as an input for the second function shown below so that it will be inserted every time the input/initial value is changed. The second function looks like as shown below.(Here I want to feed the output of the above function (x_end) to be the input for partition equation for the variables C_surf )
2. Partition
function F=partition(C_entrance_MS,C_surf)
k=C_entrance_MS(1);
l=C_entrance_MS(2);
m=C_entrance_MS(3);
X_mem=0.5;
Z_AA=-1;
Z_Na=1;
Z_Cl=-1;
%C_surf(1)= 0.01;C_surf(2)=0.05;C_surf(3)=0.08;
%C_surf(1:3)= [0.01 0.05 0.08]; %this line can also be used instead of the
%line above
phi_1=0.5;
phi_2=0.4;
phi_3=0.3;
F(1)= k + X_mem/Z_AA +Z_Na/Z_AA*phi_2*C_surf(2)*(k/(phi_1*C_surf(1)))^(Z_Na/Z_AA) +Z_Cl/Z_AA*phi_3*C_surf(3)*(k/(phi_1*C_surf(1)))^(Z_Cl/Z_AA);
F(2)= -l + phi_2*C_surf(2)*(k/(phi_1*C_surf(1))^(Z_Na/Z_AA));
F(3)= -m + phi_3*C_surf(3)*(k/(phi_1*C_surf(1))^(Z_Cl/Z_AA));
F(4)= k*Z_AA + Z_Na*l + Z_Cl*m + X_mem;
end
To solve for the C_entrance, I used the following Solver function which is shown below
z=[0.00001 0.00001 0.00001]; %where z is the intial guess for the solver
C_entrance=fsolve(@partition,z)
On the third place I want to substitute the result of the above equation (C_entrance) to the variable of the function below (NernstPlanck_Vol.) i.e variable C. which can be used to calculate the value of Co, which can also be used as an input for the other function. Nernstplanck_vol is given below
3. NernstPlanck_Vol
function dydz = NernstPlanck_Vol(Co,C)
xo_1 = Co(1);
xo_2 = Co(2);
xo_3 = Co(3);
TempK = 25 + 273.15; % Temp in Kelvin
Faraday = 96485.336; % Faraday constant [C/mol]
R_id = 8.314; % R constant [J/(K mol)]
VM_1 = 1.8e-5; % AA molar volume of lysine [m3/mol] ---converted to m3--which was 18 cm3/mol
VM_2 = 1.57e-5; % Na molar volume [m3/mol] * Calculated from rS
VM_3 = 4.47e-6; % Cl molar volume [m3/mol] * calcualted form rS
VM_4 = 1.8e-5; % Water molar volume [m3/mol]
r_AA = 3.48e-9;
r_Na = 102e-12; % ion size
r_Cl = 181e-12; % ion size
Kc(1)=0.00000245;
Kc(2)=0.00000345;
Kc(3)=0.000004245;
Z(1)=-1;
Z(2)=1;
Z(3)=-1;
C(1:3)=[0.009 0.0001 0.0009];
x_1 = C(1);
x_2 = C(2);
x_3 = C(3);
x_ave=(Co+C)/2;
p_7= 0.6e-3; %Viscosity of pure water at 45C (Pa.s)(engineering toolbox.com)--denoted as p_7
deltaP=2; %pressure
D_im=1.2e-9; %diffusivitiy not yet worked out for all the solutes
Le = 1.65e-4; % effective membrane thickness
eq4 = (x_ave(1)*Z(1)*(r_AA^2*deltaP/(8*p_7*Le)*(Kc(1)-1)*R_id*TempK)/D_im - x_ave(1)*Z(1)*VM_1*deltaP/Le)/(Faraday*Z(1)^2*x_ave(1)+Z(2)^2*x_ave(2)+ Z(3)^2*x_ave(3)) +...
(x_ave(2)*Z(2)*(r_Na^2*deltaP/(8*p_7*Le)*(Kc(2)-1)*R_id*TempK)/D_im - x_ave(2)*Z(2)*VM_2*deltaP/Le)/(Faraday*Z(1)^2*x_ave(1)+Z(2)^2*x_ave(2)+ Z(3)^2*x_ave(3)) + ...
(x_ave(3)*Z(3)*(r_Cl^2*deltaP/(8*p_7*Le)*(Kc(3)-1)*R_id*TempK)/D_im - x_ave(3)*Z(3)*VM_3*deltaP/Le)/(Faraday*Z(1)^2*x_ave(1)+Z(2)^2*x_ave(2)+ Z(3)^2*x_ave(3));
dydz(1) =x_1 - xo_1+ (x_ave(1)*(r_AA^2*deltaP/(8*p_7*Le)*(Kc(1)-1)*R_id*TempK)/D_im - x_ave(1)*VM_1*deltaP/Le - x_ave(1)*Z(1).*eq4)*Le; %assigning Elctric potential with number x(7)1
dydz(2) =x_2 - xo_2+ (x_ave(2)*(r_Na^2*deltaP/(8*p_7*Le)*(Kc(2)-1)*R_id*TempK)/D_im - x_ave(2)*VM_2*deltaP/Le - x_ave(2)*Z(2).*eq4)*Le; %assigning Elctric potential with number x(7)1
dydz(3) =x_3 - xo_3+ (x_ave(3)*(r_Cl^2*deltaP/(8*p_7*Le)*(Kc(3)-1)*R_id*TempK)/D_im - x_ave(3)*VM_2*deltaP/Le - x_ave(3)*Z(3).*eq4)*Le; %assigning Elctric potential with number x(7)1
end
The solver used to solve Co from the above equation to see if the function is operating looks like as below
z=[0.00001 0.00001 0.00001]; %where z is the intial guess for the solver
Co=fsolve(@NernstPlanck_Vol,z)
The result on the command window looks like
Co =
1.0e-03 *
0.9896 0.1000 0.0000
Again, I want to use the output of this solver (Co) as an input for the following function to be used by the variable C_end to calculate the Cp_cal. The function looks like as shown below.
Cp_calc = zeros(1,3);
Cp_calc(1) = -Z(2)/Z(1)*1/phi_2*C_end(2)*(C_end(1)/(phi_1*Cp_calc(1)))^(-Z(2)/Z(1))...
-Z(3)/Z(1)*1/phi_3*C_end(3)*(C_end(1)/(phi_1*C_end(1)))^(-Z(3)/Z(1));
Cp_calc(2)=1/phi_2*C_end(2)*(C_end(1)/(phi_1*Cp_calc(1))^(-Z(2)/Z(1)));
Cp_calc(3)=1/phi_3*C_end(3)*(C_end(1)/(phi_1*Cp_calc(1))^(-Z(3)/Z(1))); %partionig equation 3
Cp_calc(1)= Cp_calc(2)*Z(2)/Z(1) + Cp_calc(3)*Z(3)/Z(1); % electroneutrality inside the permeate --rearraged
Cp_calc = Cp_calc .* [VM_1 VM_2 VM_3]; % Convert to volume fraction
Cp_calc(Cp_calc <0) = 0;
sol = ((Cp_calc(1:3) - Cp_guess(1:3)).^2);
The final algorithm will be used to converge the result sol that will be the difference of calculated and guess value, i.e Cp_calc and Cp_guess respectively.
The code that I tried to connect all the above equations and insert the output of one function as an input for the next function is shown below on the part find_CP_MSNP

Antworten (1)

Nehemiae
Nehemiae am 8 Mär. 2023
Hello,
In order to pass the parameters (that are the outputs of one function – e.g.MS_AA_imp”) to the function passed to the “fsolve” function (e.g. “partition” function), anonymous functions are used. Here a function handle can be used to bundle the required function and its parameters before sending to the “fsolve” function. A similar pattern, as shown below, can be repeated for the “NernstPlanck_Vol.
C_surf = ysol_MS(end, :);
funcHandle = @(funcVar) partition(funcVar, C_entrance_MS, C_surf); % Create function handle to the anonymous function
z = [0.00001 0.00001 0.00001];
C_entrance_MS = fsolve(funcHandle, z); % Pass function handle to fsolve
The documentation on “fsolve” (https://www.mathworks.com/help/optim/ug/fsolve.html), anonymous functions (https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html), passing parameters in anonymous functions (https://www.mathworks.com/help/optim/ug/passing-extra-parameters.html) can help in understanding the above code.

Community Treasure Hunt

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

Start Hunting!

Translated by