# Problem over switch function

5 ビュー (過去 30 日間)
Joel 2014 年 9 月 22 日
コメント済み: Steven Lord 2020 年 1 月 9 日
So this is what I have so far.
and here is the problem:
I've made the switches, and when I go to HELP SWITCH, it shows me how to do the switch, but I don't really understand how to integrate it within the problem. I was trying to figure out if there was a way to input the element needed, and then it would add the values of a and b, and then I could execute the equation, but I can't really figure out how to go about doing that (if I'm right).
##### 1 件のコメント表示非表示 なし
Joel 2014 年 9 月 22 日
So this is what I have so far now. I'm trying to use an input function so you can choose what element you want, and apparently I'm doing something wrong... or maybe I'm using an input function in the wrong way, but every time I type one of my elements it says it doesn't have a value.
T = 300 ; V = 20; R = .08206;
myelement = input('My element: ');
switch (myelement)
case 'he'
a= .0341 ; b = .0237;
case 'hydrogheen'
a = .244 ; b = .0266;
case 'oxygen'
a = 1.36 ; b = .0318;
case 'chlorine'
a = 6.49 ; b = .0562;
case 'carbondioxide'
a = 3.59 ; b = .0427;
end
P = (R*T)/ (V - b) - a / V.^2;

サインインしてコメントする。

### 採用された回答

Adam 2014 年 9 月 22 日

Your code works, but you have to input the element as:
'he'
(with the quotation marks), not just
he
which Matlab tries to evaluate as a function or variable rather than a string.
If you want to get fancy you can also use
validatestring
to ensure a sensible input string (this also allows you to input, for example just 'o' if one of your defined valid strings is 'oxygen' and it will convert 'o' to 'oxygen' on output, though, of course, just 'h' would be ambiguous.
By the way, your spelling of 'Hydrogen' is a little off too which may cause a failure if the user types it in correctly at the command line.

サインインしてコメントする。

### その他の回答 (2 件)

Guillaume 2014 年 9 月 22 日
Please don't post images of code when you could just copy and paste the code.
Your switch syntax is correct. However, for it to be useful, you need to put it before the equation not after.
If you'd copy-pasted the code, I would have copied it below and rearranged it for you. Can't do that with a picture.
##### 1 件のコメント表示非表示 なし
Joel 2014 年 9 月 22 日
So this is what I have so far now. I'm trying to use an input function so you can choose what element you want, and apparently I'm doing something wrong... or maybe I'm using an input function in the wrong way, but every time I type one of my elements it says it doesn't have a value.
T = 300 ; V = 20; R = .08206;
myelement = input('My element: ');
switch (myelement)
case 'he'
a= .0341 ; b = .0237;
case 'hydrogheen'
a = .244 ; b = .0266;
case 'oxygen'
a = 1.36 ; b = .0318;
case 'chlorine'
a = 6.49 ; b = .0562;
case 'carbondioxide'
a = 3.59 ; b = .0427;
end
P = (R*T)/ (V - b) - a / V.^2;

サインインしてコメントする。

Julia 2014 年 9 月 22 日

Hi,
you have to write a function:
function P = VanDerWaals(T,V,e)
% switch-case
% computation of P
Since R is the same for every gas you can enter the value directly in the equation.
##### 2 件のコメント表示非表示 1 件の古いコメント
Steven Lord 2020 年 1 月 9 日
That suggests you called f_serial with only one input. If you did, when MATLAB tries to evaluate the switch expression str is undefined. MATLAB can see that str was defined to be an input argument to your function and so gives you a (hopefully) more informative error, that you called your function with fewer inputs than your function requires.
Call your function with the two inputs that it requires.

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by