How to use Nested Function properly and if/else with user input

3 views (last 30 days)
Hey all
Need some help with this script/function. When I enter values after being prompted , it keeps spitting out the same answers for p2,v2,v3, t3,v3,p3 etc... as if it's only remembering the initial stored answers and not updating everytime i vary the values entered. Perhaps it's not calling my nested functions properly but i cannot figure it out.
As you can see with the code, im trying to calculate p2,v2,T2 , P3,v3,T3, P4,v4,T4 all from the user providing those 5 intial inputs for the function. Depending on whether they select Otto or Diesel, P3 , V3, T3 will be different but the rest remain the same.
% Here is the function, i was trying to play with if statements so that depending on if the user picks otto or diesel, T3, P3, and v3 will be determined differently but it's not working.
function [P2,T2,v2] = simplefunction(P1,T1,Ve,r,k)
P2 = P1*r^k;
T2 = T1*r^(k-1);
v2=Ve/r;
function [P3,T3,v3] = nestedfxone(P2,T2,v2,r,k)
%if answer == 3
T3= (Qin+(Cv*T2))/(Cv) %State 3 for Otto
P3=P2*(T3/T2)
v3=v2
end
%elseif answer ==4
%T3= T2+ (Qin/Cp) %State 3 for Diesel
%P3=P2
%v3= 1 +(Qin/T2)
% end
function [P4,T4,v4]= nestedfxtwo(P3,T3,v3,r,k)
P4=P3*r^k
T4=T3*r^(k-1)
v4=v3/r
end
end
% Here is the script
answer = questdlg('Select Otto or Diesel', 'Cycles','Otto','Diesel','Diesel');
switch answer
case 'Otto'
disp([answer ' you chose otto'])
prompt= 'Enter A value of P1 ';
P1= input(prompt)
prompt1= 'Enter a value of T1 ';
T1=input(prompt1)
prompt7= 'Enter Engine Displace Ve'
Ve=input(prompt7)
prompt2= 'Enter a value of Qin ';
Qin=input(prompt2)
prompt3= ' Enter a value for compression ratio ';
r= input(prompt3)
prompt4= ' Enter a value for the specific heat Cp ';
Cp= input(prompt4)
prompt9= ' Enter a value for the specific heat Cv ';
Cv= input(prompt9)
k=Cp/Cv
prompt5= 'Enter the RPM '
N=input(prompt5)
[P2,T2,v2] = simplefunction (P1,T1,Ve,r,k); % Get state two variables and nested functions for state 3 and 4
%display state 2 values
disp('The Value P2 at State 2 is:')
disp(P2)
disp('The Value T2 at State 2 is:')
disp(T2)
disp('The Value v2 at State 2 is:')
disp(v2)
%display state 3 values
disp('The Value P3 at State 3 is:')
disp(P3)
disp('The Value T3 at State 3 is:')
disp(T3)
disp('The Value v3 at State 3 is:')
disp(v3)
%display state 4 values
disp('The Value P4 at State 4 is:')
disp(P4)
disp('The Value T4 at State 4 is:')
disp(T4)
disp('The Value v4 at State 4 is:')
disp(v4)
case 'Diesel'
disp([answer ' you chose diesel'])
prompt= 'Enter A value of P1 ';
P1= input(prompt)
prompt1= 'Enter a value of T1 ';
T1=input(prompt1)
prompt7= 'Enter Engine Displace Ve '
Ve=input(prompt7)
prompt2= 'Enter a value of Qin ';
Qin=input(prompt2)
prompt3= ' Enter a value for compression ratio ';
r= input(prompt3)
prompt4= ' Enter a value for the specific heat Cp ';
Cp= input(prompt4)
prompt9= ' Enter a value for the specific heat Cv ';
Cv= input(prompt9)
k=Cp/Cv
prompt5= 'Enter the RPM '
N=input(prompt5)
[P2,T2,v2] = simplefunction (P1,T1,Ve,r,k); %Attempting to get T3,P3,v3, T4,P4,v4 just from calling this function once and hopefully executing nested functions
%display state 2 values
disp('The Value P2 at State 2 is:')
disp(P2)
disp('The Value T2 at State 2 is:')
disp(T2)
disp('The Value v2 at State 2 is:')
disp(v2)
%display state 3 values
disp('The Value P3 at State 3 is:')
disp(P3)
disp('The Value T3 at State 3 is:')
disp(T3)
disp('The Value v3 at State 3 is:')
disp(v3)
%display state 4 values
disp('The Value P4 at State 4 is:')
disp(P4)
disp('The Value T4 at State 4 is:')
disp(T4)
disp('The Value v4 at State 4 is:')
disp(v4)
end
any help is greatly appreciated thank you
  2 Comments
Stephen23
Stephen23 on 9 Feb 2019
Edited: Stephen23 on 9 Feb 2019
@Sergio Alaimo: your nested functions do not make much sense, in particular:
  • Neither of the nested functions nestedfxone or nestedfxtwo are called anywhere, so they are completely superfluous.
  • None of the variables defined within the nested functions exist in their parent workspace/s, so there is absolutely no point in defining them as nested functions (they might as well be local functions).
It is not clear to me what you are trying to achieve, so I would not hazard to guess how this could be fixed.
In any case you should redesign your code to get rid of those numbered variables (use simpler arrays and indexing instead). Numbered variables are a sign that you are doing something wrong.
Sergio Alaimo
Sergio Alaimo on 9 Feb 2019
Edited: Sergio Alaimo on 9 Feb 2019
hey there, i figured something was horribly wrong so I redid it and it does what was originally intended though i feel like it could be greatly simplified. Perhaps more code in the function file and less in the script because as it stands , i barely even need the function and the professor requires one. what are your thoughts?
%Script
answer = questdlg('Select Otto or Diesel', 'Cycles','Otto','Diesel','Diesel');
switch answer
case 'Otto'
disp([answer ' you chose otto'])
prompt= 'Enter A value of P1 in kPa ';
P1= input(prompt)
prompt1= 'Enter a value of T1 in Kelvin ';
T1=input(prompt1)
prompt7= 'Enter Engine Displace Ve in Litres'
Ve=input(prompt7)
prompt2= 'Enter a value of Qin in kJ/kg ';
Qin=input(prompt2)
prompt3= ' Enter a value for compression ratio ';
r= input(prompt3)
prompt4= ' Enter a value for the specific heat Cp in kJ/kg ';
Cp= input(prompt4)
prompt9= ' Enter a value for the specific heat Cv kJ/kg ';
Cv= input(prompt9)
k=Cp/Cv
prompt5= 'Enter the RPM '
N=input(prompt5)
[P2,T2,v2] = simplefunction (P1,T1,Ve,r,k);
T3= (Qin+(Cv*T2))/(Cv) %State 3 for Otto
P3=P2*(T3/T2)
v3=v2
P4=P3*r^k
T4=T3*r^(k-1)
v4=v3/r
stroke=4
Wnet= (-Cv *(T2-T1))- Cv*(T4-T3)
Power = (Wnet*N)/(stroke)
Torque = (Power)/(2*pi*N)
% DIESEL
case 'Diesel'
disp([answer ' you chose diesel'])
prompt= 'Enter A value of P1 in kPa ';
P1= input(prompt)
prompt1= 'Enter a value of T1 in Kelvin ';
T1=input(prompt1)
prompt7= 'Enter Engine Displacement Ve in litres '
Ve=input(prompt7)
prompt2= 'Enter a value of Qin in kJ/kg ';
Qin=input(prompt2)
prompt3= ' Enter a value for compression ratio ';
r= input(prompt3)
prompt4= ' Enter a value for the specific heat Cp in kJ/kg ';
Cp= input(prompt4)
prompt9= ' Enter a value for the specific heat Cv in kJ/kg ';
Cv= input(prompt9)
k=Cp/Cv
prompt5= 'Enter the RPM '
N=input(prompt5)
[P2,T2,v2] = simplefunction (P1,T1,Ve,r,k); % Get state two variables
T3= T2+ (Qin/Cp) %State 3 for Diesel
P3=P2
v3= 1 +(Qin/T2)
P4=P3*r^k
T4=T3*r^(k-1)
v4=v3/r
stroke=4 % assume 4 stroke for diesel
Wnet= (-Cv *(T2-T1))- Cv*(T4-T3)
Power = (Wnet*N)/(stroke)
Torque = (Power)/(2*pi*N)
end
%Function
function [P2,T2,v2] = simplefunction(P1,T1,Ve,r,k)
P2 = P1* (r^k)
T2 = T1*r^(k-1)
v2=Ve/r
end

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!