How do I get my code to read CA3 and so on?

2 Ansichten (letzte 30 Tage)
Alex Wheeler
Alex Wheeler am 18 Mär. 2021
Beantwortet: Prathamesh am 30 Apr. 2025
I have the following code entered so far, but it refuses to read past the if statement. How do I get the code to recognize my else statement?
V = 4 %m^3, Volume;
Vo = 0.04 %m^3/s;
T = V/Vo %s, Time constant;
k = 0.01 %m^3/mol/s, Rate constant;
CA0 = input('The value of the inlet concentration (CA0) is: ');
CA1 = ((1 + 2 * T * k * CA0)- sqrt(1 + 4 * T * k * CA0)) / (2 * T * k);
CA2 = ((1 + 2 * T * k * CA1)- sqrt(1 + 4 * T * k * CA1)) / (2 * T * k);
CA3 = ((1 + 2 * T * k * CA2)- sqrt(1 + 4 * T * k * CA2)) / (2 * T * k);
CA4 = ((1 + 2 * T * k * CA3)- sqrt(1 + 4 * T * k * CA3)) / (2 * T * k);
CA5 = ((1 + 2 * T * k * CA4)- sqrt(1 + 4 * T * k * CA4)) / (2 * T * k);
p = ['The value of CA1 is: ',num2str(CA1)];
disp(p);
if (10 < CA1);
disp('The final desired concentration has not been reached, moving to 2nd CSTR');
p = ['The value of CA2 is: ',num2str(CA2)];
disp(p);
else (10 < CA2);
disp('The final desired concentration has not been reached, moving to 3rd CSTR');
p = ['The value of CA3 is: ',num2str(CA3)];
disp(p)
end

Antworten (1)

Prathamesh
Prathamesh am 30 Apr. 2025
I understand that the code you are executing is unable to run the “else” part.
The issue with the code is in the syntax of the “if-else” statement. In MATLAB, the “else” statement doesn't take a condition. It's a catch-all for when the “if” condition is false. To check another condition, you can use use “elseif”.
Here is an implementation of the same, in the given code snippet:
V = 4; %m^3, Volume
Vo = 0.04; %m^3/s
T = V/Vo; %s, Time constant
k = 0.01; %m^3/mol/s, Rate constant
CA0 = input('The value of the inlet concentration (CA0) is: ');
CA1 = ((1 + 2 * T * k * CA0)- sqrt(1 + 4 * T * k * CA0)) / (2 * T * k);
CA2 = ((1 + 2 * T * k * CA1)- sqrt(1 + 4 * T * k * CA1)) / (2 * T * k);
CA3 = ((1 + 2 * T * k * CA2)- sqrt(1 + 4 * T * k * CA2)) / (2 * T * k);
CA4 = ((1 + 2 * T * k * CA3)- sqrt(1 + 4 * T * k * CA3)) / (2 * T * k);
CA5 = ((1 + 2 * T * k * CA4)- sqrt(1 + 4 * T * k * CA4)) / (2 * T * k);
p = ['The value of CA1 is: ',num2str(CA1)];
disp(p);
if (10 < CA1)
disp('The final desired concentration has not been reached, moving to 2nd CSTR');
p = ['The value of CA2 is: ',num2str(CA2)];
disp(p);
elseif (10 < CA2)
disp('The final desired concentration has not been reached, moving to 3rd CSTR');
p = ['The value of CA3 is: ',num2str(CA3)];
disp(p);
end
For additional information, refer the following MathWorks documentation on “if, elseif, else”:

Kategorien

Mehr zu Predictive Maintenance Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by