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!