Difference between revisions of "Software tutorial/Scripts and functions"

From Process Model Formulation and Solution: 3E4
Jump to navigation Jump to search
Line 67: Line 67:
|-
|-
! MATLAB: <tt>energy_rate.m</tt>
! MATLAB: <tt>energy_rate.m</tt>
! Python: <tt>energy_rate.py</tt>
! Python <no file required>
|-
|-
| width="50%" valign="top" |
| width="50%" valign="top" |

Revision as of 15:05, 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 <no file required>
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]
  • MATLAB functions must create a variable with the same name as the variable that will be returned, in this case: energy.
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 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 return statement.

Note: It is good programming practice to start every function with a comment. The comment should be at least one line that describe what the function will do. Additional comments on the inputs are usually helpful.

In the above examples the name of the function is energy_rate and the input variables are f and T

  • 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.
  • In MATLAB only: the output variable is energy

Combining functions and scripts: building up your code

Functions, like scripts, can also be called from the command line.

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