Problem with if statements
古いコメントを表示
Hey!
Whenever I run this script, the answer polar is always displayed even though the answer should be nonpolar. Any help would be apprecitated. Thank you.
H = 2.20;
Li = 0.98;
Be = 1.57;
B = 2.04;
C = 2.55;
N = 3.04;
O = 3.44;
F = 3.98;
Na = 0.93;
Mg = 1.31;
Al = 1.61;
Si = 1.90;
P = 2.19;
S = 2.58;
Cl = 3.16;
K = 0.82;
Ca = 1;
Se = 1.36;
Ti = 1.54;
V = 1.63;
Cr = 1.66;
Mn = 1.55;
Fe = 1.83;
Co = 1.88;
Ni = 1.91;
Cu = 1.90;
Zn = 1.65;
Ga = 1.81;
Ge = 2.01;
As = 2.18;
Se = 2.55;
Br = 2.96;
FirstElement = input('What is the first element in the compound?');
SecondElement = input('What is the second element in the compound?');
Polarity = SecondElement - FirstElement;
while Polarity == SecondElement - FirstElement;
if FirstElement == Li, SecondElement == H;
Polarity = H - Li;
end
if FirstElement == Be, SecondElement == H;
Polarity = H - Be;
end
if FirstElement == B, SecondElement == H;
Polarity = H - B;
end
if FirstElement == B, SecondElement == H;
Polarity = H - B;
end
if FirstElement == B, SecondElement == C;
Polarity = C - B;
end
if FirstElement == B, SecondElement == N;
Polarity = N - B;
end
if FirstElement == B, SecondElement == O;
Polarity = O - B;
end
if FirstElement == B, SecondElement == F;
Polarity = F - B;
end
end
disp(ans(Polarity))
if Polarity > 0.4;
disp('Polar')
elseif disp('Non Polar')
end
採用された回答
その他の回答 (1 件)
Steven Lord
2022 年 11 月 21 日
編集済み: Steven Lord
2022 年 11 月 21 日
Don't define a large number of variables whose names match the symbols for the elements and use a large collection of if statements. Instead I recommend using either a struct array with dynamic field names or a table with named rows.
polarityValues = struct(H = 2.20, Li = 0.98, Be = 1.57, B = 2.04)
Or if you have too many fields to set this way, you could define them with lines like:
polarityValues.C = 2.55
Now if we want to retrieve the polarityValues for two elements:
firstElement = 'Li';
secondElement = 'Be';
polarity(1) = polarityValues.(firstElement);
polarity(2) = polarityValues.(secondElement)
For a table-based approach:
elementSymbols = ["H"; "Li"; "Be"; "B"; "C"];
polarityValues = [2.2; 0.98; 1.57; 2.04; 2.55];
t = table(polarityValues, 'VariableNames', "Polarity", 'RowNames', elementSymbols)
To retrieve values:
polarity = t{'Li', 'Polarity'}
polarity = t{{firstElement, secondElement}, 'Polarity'}
This neatly avoids the problem where you don't have an if statement for a particular combination of elements, in which case the polarity variable will be defined as the difference between the symbols of the elements due to your line "Polarity = SecondElement - FirstElement;"
polarity = firstElement - secondElement
L is 10 characters later in the alphabet than B and i is 4 characters later in the alphabet than e. Because in this case polarity is a vector, the body of that last if statement in your code will be executed if all the elements of the vector are greater than 0.4, and in this case they are.
カテゴリ
ヘルプ センター および File Exchange で Lengths and Angles についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!