Simplyfing code by removing from script and adding to function

1 view (last 30 days)
Hello all, I have a script that seems like it has too much clutter in it and a function that has a couple of lines. I feel like some of the lines in script could be removed and put as a function.
In a nut shell the job of this program is to calculate the values of variables P2, P3, P4 from the value of P1 that user inputs. This is the same for T1 and v1. Any help or direction is appreciated. Thanks
%SCRIPT$
answer = questdlg('Select Otto or Diesel', 'Cycles','Otto','Diesel','Diesel');
switch answer
%OTTO
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 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 kJ/kg ';
Cv= input(prompt9)
k=Cp/Cv
prompt5= 'Enter the RPM '
N=input(prompt5)
[P2,T2,v2] = Assignment3Functionunsimplified (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 % Assuming 4 stroke
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] = Assignment3Functionunsimplified (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] = Assignment3Functionunsimplified(P1,T1,Ve,r,k)
P2 = P1* (r^k)
T2 = T1*r^(k-1)
v2=Ve/r
end
  1 Comment
Preethi
Preethi on 11 Feb 2019
hi,
You can have these code lines into the function , then you need not send the inputs while calling function
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 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 kJ/kg ';
Cv= input(prompt9)
k=Cp/Cv
prompt5= 'Enter the RPM '
N=input(prompt5)
Inside the function you can have a switch statement, for the calculations. The final output from function will be Wnet, Torque, Power

Sign in to comment.

Answers (1)

Adam
Adam on 11 Feb 2019
Edited: Adam on 11 Feb 2019
This code is the same in both cases so should definitely go inside a function, though you may wish to pass stroke in as an input argument too if you want greater flexibility rather than declaring a constant inside a function:
P4=P3*r^k
T4=T3*r^(k-1)
v4=v3/r
stroke=4 % Assuming 4 stroke
Wnet= (-Cv *(T2-T1))- Cv*(T4-T3)
Power = (Wnet*N)/(stroke)
Torque = (Power)/(2*pi*N)
Ideally you would put all your maths into functions, one for the Diesel case and one for the Otto case, both of which could then call a common function containing the above and pass the relevant arguments.
Also your input requests appear to be the same in both cases so they can also be factored out into a single function that you call in both cases and which will return P1, T1, Ve, Qin, r, Cp and Cv
It may not matter much in this case, but since you are wanting to restructure your code, which is a good sign, some of the things you should be thinking of are:
  • Put common code into a function and call it from the two (or more) places where it is common.
  • Create functions that make intuitive sense from a code-readability perspective - ideally your top level script or function should read like a string of easy to understand instructions with well named functions, with the details hidden inside those functions.
  • Don't mix together methods of getting input with calculations that take place on that input - in your case you ask the user for inputs using the input function, but ideally the functions you create to calculate the results should just these returned values as inputs so that they can be provided by any method you wish, e.g. hard-coded, extracted from a GUI, etc.
  • Understand what are your desired outputs and what are just intermediate variables - in your case it seems that Wnet, Power and Torque are the desired outputs so these should be the outputs of a function, as well as your P2, P3, P4. Leave everything else inside the function's scope so that your top level code is tidy. e.g. T4, v4 don't need to be exposed if they are just used for intermediate calculations.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!