Problem with if statements
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
David Hill
2022년 11월 21일
e=["H","Li","Be","B","C","N","O","F","Na","Mg","Al","Si","P","S","Cl","K","Ca",...
"Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br"];
l=[2.20;0.98;1.57;2.04;2.55;3.04;3.44;3.98;0.93;1.31;1.61;1.90;2.19;2.58;3.16;...
0.82;1;1.36;1.54;1.63;1.66;1.55;1.83;1.88;1.91;1.90;1.65;1.81;2.01;2.18;...
2.55;2.96];
FirstElement = find(ismember(e,string(input('What is the first element in the compound?','s'))));
SecondElement = find(ismember(e,string(input('What is the second element in the compound?','s'))));
Polarity = abs(l(SecondElement) - l(FirstElement));
if Polarity > 0.4
disp('Polar')
else
disp('Non Polar')
end
댓글 수: 2
David Hill
2022년 11월 28일
ismember is producing a logical array the same size as e. You could also do e=="Cr" which produces the same logical array. abs is just the absolute value of the subtraction. input will produce a character array but we need to compare string to string. string just converts the character array into a string.
e=["H","Li","Be","B","C","N","O","F","Na","Mg","Al","Si","P","S","Cl","K","Ca",...
"Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br"];
ismember(e,"Cr")
e=="Cr"
추가 답변 (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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!