Matrix dimensions must agree

1 次查看(过去 30 天)
Hi,
i try to write this little program to have a simplified analysis for the mass of pressurizing Gas in two case: isothermal and isentropic. I used many prompt to insert the inputs and, as shown down, an if cicle. However it works just when i write 'isotermico' as input for the prompt4. If i write 'isoentropico' occurs and error:
Matrix dimensions must agree.
Error in
analisi_semplificata_massa_gas_pressurizzante
(line 52)
if Caso == 'isotermico'
I understand that the problem is the differnt length of the words 'isotermico' and 'isoentropico', in fact if i write 'isoentropi' it works, but i'd like to know how to write it correctly without this ignorant ploy.
prompt4 = 'Caso: '; % INSERIRE CASO (Es.: isotermico, isoentropico);
Caso = input(prompt4,'s');
prompt5 = 'Inserire pressione serbatoio gas: '
P_0 = input(prompt5)
prompt6 = 'Inserire pressione finale del propellente nel serbatoio: '
P_p = input(prompt6)
prompt7 = 'Inserire valore della temperatura nel serbatoio: '
T_0 = input(prompt7);
if Caso == 'isotermico'
V_0 = (V_p*P_p)/(P_0-P_p)
elseif Caso == 'isoentropico'
prompt8 = 'Inserire valore di k: '
k = input(prompt8);
V_0 = V_p/(((P_0/P_p)^(1/k))-1)
else
display('Inserire funzione nello script')
end
m_0 = P_0*V_0/(R*T_0)
Summing:
I need to know how to write correctly the condition of the if cicle without any errors occurring due to the different length of the two text array 'isotermico' and 'isoentropic'.

采纳的回答

Jon
Jon 2020-10-26
编辑:Jon 2020-10-26
Use strcmp for example
if strcmp(Caso,'isotermico')
A switch case expression can also be used nicely for this
switch Caso
case 'isotermico'
V_0 = (V_p*P_p)/(P_0-P_p)
case 'isentropico'
prompt8 = 'Inserire valore di k: '
k = input(prompt8);
V_0 = V_p/(((P_0/P_p)^(1/k))-1)
otherwise
display('Inserire funzione nello script')
end
end
  2 个评论
Sergio Gagliano
Sergio Gagliano 2020-10-26
tryed, now it works and both two method are good, thanks! Just an other question related to the function switch: is there a way to come back at the prompt with wich i choose if execute the 'isotermico' or 'isoentropico' without giving again the other prompt? I mean, given the first prompts that are indipendent, how can i switch between the two cases ('isotermico' and 'isoentropico') just giving only the prompt4? i'd like to switch between the two cases without insert all the inputs every time and run them just when i need it.
(I looked around to find an answer but probably i badly formalized the question)
prompt = 'Inserire massa propellente: ';
prompt2 = 'Inserire densità propellente: ';
prompt3 = 'Inserire PM gas pressurizzante: ';
m_p = input(prompt); % MASSA PROPELLENTE;
RHO_p = input(prompt2);
PM_gas = input(prompt3);
R_0 = 8.314462618; % COSTANTE UNIVERSALE DEI GAS; [J/(mol*K)]
R = R_0/PM_gas;
V_p = m_p/RHO_p;
% %% CALCOLO DEL VOLUME E DELLA MASSA DI GAS
prompt4 = 'Caso: '; % INSERIRE CASO (Es.: isotermico, isoentropico);
Caso = input(prompt4,'s');
prompt5 = 'Inserire pressione serbatoio gas: '
P_0 = input(prompt5)
prompt6 = 'Inserire pressione finale del propellente nel serbatoio: '
P_p = input(prompt6)
prompt7 = 'Inserire valore della temperatura nel serbatoio: '
T_0 = input(prompt7);
if strcmp(Caso,'isotermico')
V_0 = (V_p*P_p)/(P_0-P_p)
elseif strcmp(Caso,'isoentropico')
prompt8 = 'Inserire valore di k: '
k = input(prompt8);
V_0 = V_p/(((P_0/P_p)^(1/k))-1)
else
display('Inserire funzione nello script')
end
Jon
Jon 2020-10-27
I think the best way to do this would be to use the inputdlg command. Type doc inputdlg on the command line to get documentation for this.
Another way to do it would be to use the exist function to see if the variables were defined. So for example
if ~exist('m_p')
m_p = input(prompt); % MASSA PROPELLENTE;
end
or test them all together
% check if all inputs are assigned
inputsAssigned = [exist('m_p') exist('RHO_p') exist('PM_gas') exist('R') exist('V_p')]
if ~all(inputsAssigned)
% some inputs are not assigned, reassign them
m_p = input(prompt); % MASSA PROPELLENTE;
RHO_p = input(prompt2);
PM_gas = input(prompt3);
R_0 = 8.314462618; % COSTANTE UNIVERSALE DEI GAS; [J/(mol*K)]
R = R_0/PM_gas;
V_p = m_p/RHO_p;
end
Of course this means that if you want to assign new values you must first clear the existing ones from your workspace before running the script. You can do this by typing clear on the command line.

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by