Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Functions
Learning Outcome
Introduction
In Matlab programming, we need to reuse some
parts of the codes in many situations.
In last lecture we learnt for and while loops, which
enabled us to repeat some code blocks
There is another mechanism called function in
Matlab, which enables us to package some code
segments into a block and give the block a name.
Whenever we want to carry out the operations in the
block, we just need to call the block name and pass
some necessary parameters to it.
Introduction
Advantages of using functions:
Code segments in functions can be re-used in
other Matlab programs
Code segments in functions can be tested and
verified separately
Introduction
Previously we have learnt and used a number of
Matlab functions without realising it.
Matlab functions
min, max, sin, exp, rand
For Maths operation
For data plotting
For input/output
For string processing
sprintf, int2num
List of return
values
Function
name
List of input
values
1
P
1
1
1 1
5
3 6 10
4 10
1
2
1
3
1
4
Pi, j Pi 1, j Pi, j 1
Script example
%
%
%
%
%
Function example
function PascTria = fPascal( np );
% --- fPascal --%
% PascTria = fPascal( np );
% This function computes Pascals triangle of a given
% dimension, <np>.
% ----------------------------------------------------------disp(['Pascal''s triangle of dimension ' num2str(np)]);
Z = zeros(np,np);
% Initialise Z as a matrix of zeros.
Z(1,:)=ones(1,np); % Set all entries in row 1 equal to 1.
Z(:,1)=ones(np,1); % Set all entries in column 1 equal to 1.
for irow=2:(np-1) % Step through rows 2 up to (np-1)
for jcol=2:(np+1-irow)
% Columns 2 up to (np+1-irow)
Z(irow,jcol)= Z(irow-1,jcol) + Z(irow,jcol-1);
end
end
PascTria = Z;
Functions
>> fPascal
>>
>>
>>
>>
<RET>
n=7; Z = 11;
fPascal(n)
Z
PascTria
Notice how the input argument (n) does not need to have
the same name as that within the function (np) in effect,
np is a copy of the variable n which will be used internally
within the function.
The output matrix has not been assigned to any variable so
it is automatically assigned to ans. Therefore Z is still 11
and PascTria is undefined.
Functions
W1 H 1 1 W2 H 2 H1 2
A
2
2
3
3
W13 H1 W2 3 H 2
H1
H2
W1 H1 W2 H 2
I xx W1 H 1 yC
yC
I yy
W2 H 2 H 1
2
2
12 12
12 12
A W1 H1 W2 H 2
2
yC
2
Help on functions
<RET>
<RET>
Global variables
%
%
%
%
--- main0 --This script shows that information can be passed into and out of
functions through <global> variables.
-----------------------------------------------------------------------
global x;
% Declare the variable x to be global.
x = 11.5;
% Assign a value to the global variable.
y = 1.5*pi-11.5;
z = func_xy( y );
% The value of <y> is passed into the function
disp([' The value of z is : ' num2str(z) ]);
disp([' The value of x is : ' num2str(x) ]);
10
Global variables
x started off as 11.5, then was both accessed and
modified in the workspace from the function
func_xy, becoming 12.5 even though x was not
an input or output variable.
Note that global variables do have the same name
within the functions that access them and in the
workspace.
Global variables
11
Global variables
Tips in using global variables:
1. who global gives a list of all the known global
variables
2. clear global deletes all global variables
3. Assigning a value to a variable and subsequently
declaring it to be a global variable leads to very
unpredictable results always declare it to be global
first and then assign a value.
People sometimes think it is useful to use global variables
rather than long lists of input and output variables. In these
cases, structures are a better option. Structures are
essentially variables that contain other variables, and are
beyond the scope of this course.
function y = triv_func( a, b, x );
% --- triv_func --% y = triv_func( a, b, x );
% Illustration of writing a function to ensure it can act on arrays.
% ----------------------------------------------------------------------y = cos( a*x + b*(x.^2) );
Notice the use of the dot in x.^2 (in case x is a vector), but no dot in
a*x or b*(x because we expect both a and b to be scalars.
12
Example
10
0 <
For a function =
, plot
0
< 1
f(x) against x in the range 0,1 , for different
values of [0.2, 0.4, 0.6].
Example
%
%
%
%
--- Main program --Calls functions fx to calculate y againest x in the range [0,1]
for different Xa values and plot the results.
----------------------------------------------------------------
13
Closing Remarks
14