If...elseif Statement Not Pulling Variables

Please I need help. I am trying to run the following arguments for three different cases using the if..elseif statement but it is popping up errors. I am trying to pull the values for the variables a, b and c for a subsequent calculation, depending on what statement is true from the three cases below.
if (E<0.01) and (F<L1) or (E>0.01) and (F<L2)
then
a = 0.98; b = 0.4846; c = 0.0868;
elseif (0.01<E<0.4) and (L3<F<L1) or (E>0.4) and (L3<F<L4)
then
a = 0.845; b = 0.5351; c = 0.0173;
elseif (E<0.4) and (F>L1) or (E>0.4) and (F>L4)
then
a = 1.065; b = 0.5824; c = 0.0609;
end
Thank you

댓글 수: 1

Jan
Jan 2015년 4월 6일
I've formatted your code, do improve the readability. Please apply a proper code formatting by your own - thanks!

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2015년 4월 6일
편집: Stephen23 2015년 4월 6일

2 개 추천

That syntax is not MATLAB syntax at all: MATLAB if does not use a then statement, and while and and or do exist as functions, they cannot be used like that (only as functions, as the documentation clearly shows). It is also not possible to make two logical comparisons simultaneously, so 0.01<E<0.4 will always be an error.
You need to learn about basic MATLAB syntax and learn to read the documentation (read the links that I gave). These tutorials are a good place to start to learn MATLAB:
Here is a version that works without errors, as long as all of the input values are scalars:
if (E<0.01 && F<L1) || (E>0.01 && F<L2)
a = 0.98;
b = 0.4846;
c = 0.0868;
elseif (0.01<E && E<0.4 && L3<F && F<L1) || (E>0.4 && L3<F && F<L4)
a = 0.845;
b = 0.5351;
c = 0.0173;
elseif (E<0.4 && F>L1) || (E>0.4 && F>L4)
a = 1.065;
b = 0.5824;
c = 0.0609;
end
Note that if none of these conditions are met, then these variables will not be defined! (and ergo an else statement might be a good idea).

댓글 수: 6

Fun Dan
Fun Dan 2015년 4월 6일
Thank you Stephen for your prompt response. I really appreciate it. I am actually new with Matlab and the link you sent is very helpful. I have tried to run the codes you just gave me. However, I am not able to calculate A = (a*C^b)/F^c based on the output from the code.
Stephen23
Stephen23 2015년 4월 6일
편집: Stephen23 2015년 4월 6일
"not able to calculate" is not enough information: are you getting a wrong value, or an error message (in red), or a warning message, or did your laptop catch fire? Unless you tell us exactly what code you are running (with the values) and the complete and exact error messages that you might be getting, then it is impossible to know what might be preventing the calculation. Sorry, but we can't read minds!
Fun Dan
Fun Dan 2015년 4월 6일
% Parameters V= 0.002; D= 0.05021; QL= 0.0033; QG= 0.0033; % Calculation of Correlation Parameters F = 0.102*V^2/D; E = QL/(QL+QG); % Calculation of Non-Dimensional Numbers L1 = 316* E^0.302; L2 = 0.0009252*E^-2.4684; L3 = 0.1*(E)^-1.4516; L4 = 0.5*(E)^-6.738; % Determination of Pressure Regime if (E<0.01 && F<L1) (E>0.01 && F<L2) a = 0.98; b = 0.4846; c = 0.0868; elseif (0.01<E && E<0.4 && L3<F && F<L1) (E>0.4 && L3<F && F<L4) a = 0.845; b = 0.5351; c = 0.0173; elseif (E<0.4 && F>L1) (E>0.4 && F>L4) a = 1.065; b = 0.5824; c = 0.0609; end A=a*E^b/F^c;
The output I got was 'FlowMod' which is the name I used in saving the mfile.
Read this, so we can read your code.
Stephen23
Stephen23 2015년 4월 6일
편집: Stephen23 2015년 4월 6일
@Fun Dan: How are you calling this? When you write that the "output was 'FlowMod'", what is this the output of? Please show us the exact code that you use to call this script, either as text or in screenshot.
And please, as Image Analyst has already asked, format your code so that it is readable. You can use the {} Code button above the textbox to do this.
After I formatted your code correctly and replaced all of the || operators that you had rather mysteriously removed from the code, it worked just fine. I simply placed this in an Mfile called "temp":
% Parameters
V = 0.002;
D = 0.05021;
QL = 0.0033;
QG = 0.0033;
% Calculation of Correlation Parameters
F = 0.102*V^2/D;
E = QL/(QL+QG);
% Calculation of Non-Dimensional Numbers
L1 = 316* E^0.302;
L2 = 0.0009252*E^-2.4684;
L3 = 0.1*(E)^-1.4516;
L4 = 0.5*(E)^-6.738;
% Determination of Pressure Regime
if (E<0.01 && F<L1) || (E>0.01 && F<L2)
a = 0.98;
b = 0.4846;
c = 0.0868;
elseif (0.01<E && E<0.4 && L3<F && F<L1) || (E>0.4 && L3<F && F<L4)
a = 0.845;
b = 0.5351;
c = 0.0173;
elseif (E<0.4 && F>L1) || (E>0.4 && F>L4)
a = 1.065;
b = 0.5824;
c = 0.0609;
end
A = a*E^b/F^c;
And ran the script from the command line like this:
>> temp
and checked the final value too
>> A
A =
1.9372
Fun Dan
Fun Dan 2015년 4월 6일
Thank you Stephen! It worked perfectly now. I got A = 1.9372 as well.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2015년 4월 6일

댓글:

2015년 4월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by