Please, help me fix the first code. why is 1st code not working but the 2nd code is?

Ca0 = 8.24;
Cb0 = 0.824;
K = 6.98*10^17;
C0 = Ca0 + Cb0;
K1 = 0.00083287;
K2 = 0.2172;
K4 = 1.341;
K5 = 1.60;
r1 = 0.00522;
Ca = (Cb0*((Ca0/Cb0) - X));
F = @(X)((K1*C0*(Ca - ((3*X*Cb0*X*Cb0)/(K*Cb0*(1 - X)))))/(((K1*3*X*Cb0*X*Cb0)/(K*Cb0*(1 - X))) + (K2*Cb0*(1 - X)) + (K5*X*Cb0) + (K4*K5*3*X*Cb0*X*Cb0) + 1)) - r1
roots = fsolve(F,0)
----------------------------------------------------------------------------------------------------------------------------
The code above returns error message: Undefined function or variable 'X'.
But the code below runs successfully after replacing value of Ca in F.
-----------------------------------------------------------------------------------------------------------------------------
Ca0 = 8.24;
Cb0 = 0.824;
K = 6.98*10^17;
C0 = Ca0 + Cb0;
K1 = 0.00083287;
K2 = 0.2172;
K4 = 1.341;
K5 = 1.60;
r1 = 0.00522;
F = @(X)((K1*C0*((Cb0*((Ca0/Cb0) - X)) - ((3*X*Cb0*X*Cb0)/(K*Cb0*(1 - X)))))/(((K1*3*X*Cb0*X*Cb0)/(K*Cb0*(1 - X))) + (K2*Cb0*(1 - X)) + (K5*X*Cb0) + (K4*K5*3*X*Cb0*X*Cb0) + 1)) - r1
roots = fsolve(F,0)
In addition, how can I put the 1st format (that is, where Ca is defined separately and then Ca placed in F) in a function and solve

Réponses (1)

Since ‘Ca’ is actually a function of ‘X’ it has to be coded as such —
Ca = @(X) (Cb0*((Ca0/Cb0) - X));
Ca0 = 8.24;
Cb0 = 0.824;
K = 6.98*10^17;
C0 = Ca0 + Cb0;
K1 = 0.00083287;
K2 = 0.2172;
K4 = 1.341;
K5 = 1.60;
r1 = 0.00522;
Ca = @(X) (Cb0*((Ca0/Cb0) - X));
F = @(X)((K1*C0*(Ca(X) - ((3*X*Cb0*X*Cb0)/(K*Cb0*(1 - X)))))/(((K1*3*X*Cb0*X*Cb0)/(K*Cb0*(1 - X))) + (K2*Cb0*(1 - X)) + (K5*X*Cb0) + (K4*K5*3*X*Cb0*X*Cb0) + 1)) - r1
F = function_handle with value:
@(X)((K1*C0*(Ca(X)-((3*X*Cb0*X*Cb0)/(K*Cb0*(1-X)))))/(((K1*3*X*Cb0*X*Cb0)/(K*Cb0*(1-X)))+(K2*Cb0*(1-X))+(K5*X*Cb0)+(K4*K5*3*X*Cb0*X*Cb0)+1))-r1
roots = fsolve(F,0)
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
roots = 1.3226
And with that change, it works!
.

2 commentaires

My pleasurre!
If my Answer helped you solve your problem, please Accept it!
.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by