Difference between revisions of "Software tutorial/Scripts and functions"
m (Created page with "== Scripts == * Scripts are a series of commands, saved in a <tt>.m</tt> or <tt>.py</tt> file. * When you run a script, it is as if you typed each line, in sequence, into MATLAB...") |
m (→Scripts) |
||
Line 24: | Line 24: | ||
f = 0.1; % [m^3/s] | f = 0.1; % [m^3/s] | ||
T = 341; % [K] | T = 341; % [K] | ||
energy = rho * Cp * f * T; % [J] | |||
disp(['The energy | disp(['The energy is = ', num2str(energy/1E6), ' MJ/s']) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 31: | Line 31: | ||
<syntaxhighlight lang="text"> | <syntaxhighlight lang="text"> | ||
>> example | >> example | ||
The energy | The energy is = 171.0456 MJ/s | ||
</syntaxhighlight> | </syntaxhighlight> | ||
'''Note''': you do not type the <tt>>></tt> characters - they are used in these tutorials to indicate that you should type the rest of the line in the MATLAB command window. | |||
A simpler way to run your script is to click the "Play" button, or use the shortcut key displayed under the "Debug" menu: usually F7 on Windows computers. | |||
| width="50%" valign="top" | | | width="50%" valign="top" | | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="python"> | ||
rho = 1200 # [kg/m^3] | rho = 1200 # [kg/m^3] | ||
Cp = 4180 # [J/(kg.K)] | Cp = 4180 # [J/(kg.K)] | ||
f = 0.1 # [m^3/s] | f = 0.1 # [m^3/s] | ||
T = 341 # [K] | T = 341 # [K] | ||
energy = rho * Cp * f * T # [J] | |||
print('The energy | print('The energy is = %f MJ/s' % (energy/1E6)) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
From the Python command prompt: | From the Python command prompt: | ||
<syntaxhighlight lang="text"> | <syntaxhighlight lang="text"> | ||
>> execfile('example.py') | >>> execfile('example.py') | ||
The energy | The energy is = 171.045600 MJ/s | ||
</syntaxhighlight> | </syntaxhighlight> | ||
'''Note''': you do not type the <tt>>>></tt> characters - they are used in these tutorials to indicate that you should type the rest of the line in the Python command window. | |||
A simpler way to run your script is to click the "Play" button in Python(x,y), or use the F9 shortcut key. | |||
|} | |} | ||
==Functions== | |||
What if we needed to calculate the energy in at a different inlet temperature, '''<tt>T</tt>''' and at a different flow rate, '''<tt>f</tt>'''? We could edit our script and change the values of '''<tt>T</tt>''' and '''<tt>f</tt>''' and rerun it. | |||
A better alternative is to write a function, which will accept these values and return the energy. '''A simple rule to remember:''' whenever you write code that is similar to code you have previously written - you should stop - and rather write a function. | |||
{| class="wikitable" | |||
|- | |||
! MATLAB: <tt>energy_rate.m</tt> | |||
! Python: <tt>energy_rate.py</tt> | |||
|- | |||
| width="50%" valign="top" | | |||
<syntaxhighlight lang="matlab"> | <syntaxhighlight lang="matlab"> | ||
function | function energy = energy_rate(f, T) | ||
% | % Returns the energy rate into/out of the system. | ||
% Units assumed: f = [m^3/s], T = [K]. | |||
rho = 1200; % [kg/m^3] | |||
Cp = 4180; % [J/(kg.K)] | |||
energy = rho * Cp * f * T; % [J] | |||
</syntaxhighlight> | </syntaxhighlight> | ||
*The output variable here is | * MATLAB functions must create a variable with the same name as the variable that will be returned, in this case: '''<tt>energy</tt>'''. | ||
*The name of the function is | |||
| width="50%" valign="top" | | |||
* | |||
<syntaxhighlight lang="python"> | |||
def energy_rate(f, T): | |||
""" | |||
Returns the energy rate into/out of the system. | |||
Units assumed: f = [m^3/s], T = [K]. | |||
""" | |||
rho = 1200 # [kg/m^3] | |||
Cp = 4180 # [J/(kg.K)] | |||
return rho * Cp * f * T # [J] | |||
</syntaxhighlight> | |||
* The code inside a Python function must be indented to indicate that it is part of the function. The variable that will be returned to the user need not be created - just use the '''<tt>return</tt>''' statement. | |||
|} | |||
* The output variable here is '''<tt>energy</tt>''' and the input variables are '''<tt>f</tt>''' and '''<tt>T</tt>''' | |||
* The name of the function is '''<tt>energy_rate</tt>''' | |||
* In MATLAB only: the function must be in a <tt>.m</tt> file with the same name as the function. | |||
* In MATLAB only: file names must not have any spaces and cannot use <tt>-</tt> (minus) or other mathematical symbols in their name. | |||
== Combining functions and scripts: building up your code == | |||
<syntaxhighlight lang="matlab"> | <syntaxhighlight lang="matlab"> | ||
D = distance(2.0,3.5) | D = distance(2.0,3.5) |
Revision as of 14:42, 19 September 2010
Scripts
- Scripts are a series of commands, saved in a .m or .py file.
- When you run a script, it is as if you typed each line, in sequence, into MATLAB or Python.
- It is good programming practice, and is required for this course, that all all your programs should run from a script.
- It prevents you retyping in the commands if you need to change a variable's value
- You must use scripts if you need to debug your program
- Is helpful when you come back weeks or months later to figure out what you did (like revising for exams)
- Can be shared with your colleagues for collaborative development.
- All variables resulting from calculations performed in the script are available to you in the command window after the script is finished
A simple example of a script:
MATLAB: example.m | Python: example.py |
---|---|
rho = 1200; % [kg/m^3]
Cp = 4180; % [J/(kg.K)]
f = 0.1; % [m^3/s]
T = 341; % [K]
energy = rho * Cp * f * T; % [J]
disp(['The energy is = ', num2str(energy/1E6), ' MJ/s'])
Now at the MATLAB command prompt you can type: >> example
The energy is = 171.0456 MJ/s
Note: you do not type the >> characters - they are used in these tutorials to indicate that you should type the rest of the line in the MATLAB command window. A simpler way to run your script is to click the "Play" button, or use the shortcut key displayed under the "Debug" menu: usually F7 on Windows computers. |
rho = 1200 # [kg/m^3]
Cp = 4180 # [J/(kg.K)]
f = 0.1 # [m^3/s]
T = 341 # [K]
energy = rho * Cp * f * T # [J]
print('The energy is = %f MJ/s' % (energy/1E6))
From the Python command prompt: >>> execfile('example.py')
The energy is = 171.045600 MJ/s
Note: you do not type the >>> characters - they are used in these tutorials to indicate that you should type the rest of the line in the Python command window. A simpler way to run your script is to click the "Play" button in Python(x,y), or use the F9 shortcut key. |
Functions
What if we needed to calculate the energy in at a different inlet temperature, T and at a different flow rate, f? We could edit our script and change the values of T and f and rerun it.
A better alternative is to write a function, which will accept these values and return the energy. A simple rule to remember: whenever you write code that is similar to code you have previously written - you should stop - and rather write a function.
MATLAB: energy_rate.m | Python: energy_rate.py |
---|---|
function energy = energy_rate(f, T)
% Returns the energy rate into/out of the system.
% Units assumed: f = [m^3/s], T = [K].
rho = 1200; % [kg/m^3]
Cp = 4180; % [J/(kg.K)]
energy = rho * Cp * f * T; % [J]
|
def energy_rate(f, T):
"""
Returns the energy rate into/out of the system.
Units assumed: f = [m^3/s], T = [K].
"""
rho = 1200 # [kg/m^3]
Cp = 4180 # [J/(kg.K)]
return rho * Cp * f * T # [J]
|
- The output variable here is energy and the input variables are f and T
- The name of the function is energy_rate
- In MATLAB only: the function must be in a .m file with the same name as the function.
- In MATLAB only: file names must not have any spaces and cannot use - (minus) or other mathematical symbols in their name.
Combining functions and scripts: building up your code
D = distance(2.0,3.5)
- The function will calculate the distance between (2.0,3.5) and the origin and put the result in the variable D
- Note that the function allows us to perform the distance calculation without previously assigning values to the variables x and y in the workspace
- A function can assign multiple output variables (consult MATLAB help to find out how)
- Functions can call other functions