- 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).
How to use Nested Function properly and if/else with user input
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Stephen23
el 9 de Feb. de 2019
Editada: Stephen23
el 9 de Feb. de 2019
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
el 9 de Feb. de 2019
Editada: Sergio Alaimo
el 9 de Feb. de 2019
Respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!