MATLAB: How to get the user to select an equation out of a list of equations

if statement

Hello everyone: I am writing to ask for help. I want to create a script on matlab that allows the user to select an equation out of 4 possible equations that they could use to calculate the pressure of a gas. The data that I have loaded in to the script will then be used in the equation I have selected. I tried using the 'if' fuction but I'm not sure that this is the right function to use. The following is what I have tried to do with the 'if' function.
% To allow the user to pick the equation they would like to use
fprintf('IG = Ideal Gas; VDW = Van der Waals; SRK = Saove-Reidlich-Kwong; PR = Peng-Robinson\n');
eos = input('Please enter the number for the corresponding equation of state you would like to use: \n');
if eos == IG
v = 0.0008*vc :0.00005: vc; t = Tc; p = R*t./v;
elseif eos == VDW
a = 27 * R^2 * Tc^2/(64*Pc);b = R * Tc/(8*Pc);v = 0.0008*vc :0.00005: vc;t = Tc; p = R*t./(v-b)-a./v.^2;
elseif eos == SRK
ac = 0.42748*R^2*Tc^2/Pc; b = 0.08664*R*Tc/Pc; v = 0.0008*vc :0.00005: vc;m = 0.48+1.574*w-0.176*w^2; a(T) = ac*(1+m*(1-sqrt(T))^2; p = R*t./(v-b)-a(T)/(v.(v+b));
else eos == PR
ac = 0.45724*R^2*Tc^2/Pc; b = 0.07780*R*Tc/Pc;v = 0.0008*vc :0.00005: vc; m = 0.37464+1.542264*w-0.269*w^2; a(T) = ac*(1+m*(1-sqrt(T))^2; p = R*t./(v-b)-a(T)/(v.(v+b)+b.(v-b));
end
Please help! Thank you in advance!

Best Answer

input('Str') replies a number. So use input('Str', 's') to get a char vector instead.
eos == IG compares elementwise with the variable called IG . You want to compare the strings: strcmpi(eos, 'IG') or nice with switch/case:
fprintf(['IG = Ideal Gas\n', ...
'VDW = Van der Waals\n', ...
'SRK = Saove-Reidlich-Kwong\n', ...
'PR = Peng-Robinson\n']);
eos = input('Please enter the key: ', 's');
switch upper(eos)
case 'IG'
...
end
Related Question