Transcript FUNCTION

Programming with Matlab
Day 3: Functions
Solving problems: Functions
Function: Code that performs a specific task
INPUT
(vector)
FUNCTION
(Suma)
OUTPUT
(number)
Difference between functions and scripts
Scripts
Command Window
Command Window
>> vector=[1 2 3 4];
>> suma
>>
Editor
>> vector=[1 2 3 4];
>> n_elems=length(vector);
>> s=0;
>> for c_elems=1:n_elems
s=s+vector(c_elems);
end
>>
n_elems=length(vector);
s=0;
for c_elems=1:n_elems
s=s+vector(c_elems);
end
=
• The script uses previously defined variables (for example, vector).
• All variables defined in the script (i.e. n_elems, s, c_elems) remain in the
workspace after the script is executed.
Difference between functions and scripts
Functions
Command Window
Command Window
>> vector=[1 2 3 4];
>> s=suma(vector);
>>
Editor
>> vector=[1 2 3 4];
>> n_elems=length(vec);
>> s=0;
>> for c_elems=1:n_elems
s=s+vec(c_elems);
end
>>
function s=suma(vec)
n_elems=length(vec);
s=0;
for c_elems=1:n_elems
s=s+vec(c_elems);
end
≠
Difference between functions and scripts
Functions
Command Window
Workspace
>> vec=[1 2 3 4];
>> s=suma(vec);
>>
Editor
vec
function s=suma(vector)
n_elems=length(vector);
s=0;
for c_elems=1:n_elems
s=s+vector(c_elems);
end
vec
Function suma
•Create variables
•Delete variables
•Change variables
• A function can only access those workspace variables
that are given to it as input arguments.
• A function only adds to the workspace those variables
specified as output arguments.
All the
mess
remains
inside
s
Workspace
vec
s
Function definition
Function's name.
BUT: This name has
no effect.
The filename is the true name
of the function (the one we will
use to call and execute it).
function
It is better that
both names
match.
Editor - C:\Directorio\function_name.m
function [output1,output2,…] = function_name (input1,input2,…)
Code
output1=0;
for c=1:10
output1=output1+input1(c);
end
…
output2=‘Pues sí’;
Output arguments
• From zero to many
• Between square brackets []
• Separated by commas
Input arguments
• From zero to many
• Between parenthesis ()
• Separated by commas
The values of output variables (output1, output2…) when the program
finishes, are sent to the workspace.
Use of functions
Editor - C:\Directorio\sumadorl.m
function s = sumadorl (sumando1,sumando2)
s = sumando1 + sumando2;
Command Window
>> vector=[1 2 3 4];
>> suma=sumadorl(vector,10)
suma =
11
>>
12
13
14
Use of functions
Editor - C:\Directorio\sumadorl.m
function s = sumadorl (sumando1,sumando2)
s = sumando1 + sumando2;
Command Window
>> vector=[1 2 3 4];
>> suma=sumadorl (vector,10)
Variable names do not need to
match inside and outside the
function (but they may match).
suma =
11
>>
12
13
14
Input arguments may be either variables or
just numbers/vectors typed directly.
Local Variables
Editor - C:\Directorio\sumadorl.m
function s = sumadorl(sumando1,sumando2)
s = sumando1 + sumando2;
Local variables: Used inside
a function. Each function
has its own Workspace, and
they never get mixed.
Command Window
>> vector=[1 2 3 4];
>> suma=sumadorl(vector,10)
Base Workspace: Used from
Command Window.
suma =
11
>>
12
13
14
There may be variables with the same name in
different workspaces, but they are completely
different variables.
Turns out, we have been using
functions all the time
Command Window
>> s = sin(2);
Output
Input
Logical operators AND, OR, NOT
•
•
•
•
AND: &
OR: |
NOT: ~
They may be nested.
Examples:
(a==b & c==d) | e>f
a==b & (c==d | e>f)
a==b & ~(c>d)
Efficiency: Preallocating variables
>> a(1)=5;
>> a(2)=3;
>> a(3)=7;
Reserve space for one number
Reserve space for another number
Three space
requests
Reserve space for another number
Reserve space for three numbers
>> a=zeros(1,3);
>> a(1)=5;
>> a(2)=3; Space was already reserved
>> a(3)=7;
One space
request
MUCH FASTER
Specially important in loops
If you do not know the final length from the beginning, preallocate
plenty of space and cut the vector in the end