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

조회 수: 3 (최근 30일)
Alex Wheeler
Alex Wheeler 2021년 3월 18일
답변: Prathamesh 2025년 4월 30일
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

답변 (1개)

Prathamesh
Prathamesh 2025년 4월 30일
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”:

카테고리

Help CenterFile Exchange에서 Predictive Maintenance Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by