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'.
0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Red 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!