If...elseif Statement Not Pulling Variables

1 次查看(过去 30 天)
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
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 个评论
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 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by