Please, help me fix the first code. why is 1st code not working but the 2nd code is?
Show older comments
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
Answers (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
roots = fsolve(F,0)
And with that change, it works!
.
2 Comments
Adedamola Adedoyin
on 7 Nov 2021
Star Strider
on 7 Nov 2021
My pleasurre!
If my Answer helped you solve your problem, please Accept it!
.
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!