Programming with Matlab Day 3: Functions
Solving problems: Functions Function: Code that performs a specific task FUNCTION (Suma) INPUT (vector) OUTPUT (number)
Difference between functions and scripts Command Window >> vector=[1 2 3 4]; >> n_elems=length(vector); >> s=0; >> for c_elems=1:n_elems s=s+vector(c_elems); end >> Command Window = >> vector=[1 2 3 4]; >> suma >> n_elems=length(vector); s=0; for c_elems=1:n_elems s=s+vector(c_elems); end Editor 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 Command Window >> vector=[1 2 3 4]; >> n_elems=length(vec); >> s=0; >> for c_elems=1:n_elems s=s+vec(c_elems); end >> Command Window ≠ >> vector=[1 2 3 4]; >> s=suma(vector); >> function s=suma(vec) n_elems=length(vec); s=0; for c_elems=1:n_elems s=s+vec(c_elems); end Editor
Difference between functions and scripts vec Workspace Command Window >> vec=[1 2 3 4]; >> s=suma(vec); >> vec function s=suma(vector) n_elems=length(vector); s=0; for c_elems=1:n_elems s=s+vector(c_elems); end Editor Function suma Create variables Delete variables Change variables All the mess remains inside s 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. vec s Workspace
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). It is better that both names match. function function [output1,output2,…] = function_name (input1,input2,…) output1=0; for c=1:10 output1=output1+input1(c); end output2=‘Pues sí’; Editor - C:\Directorio\function_name.m … Output arguments From zero to many Between square brackets [] Separated by commas Code 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; Editor - C:\Directorio\sumadorl.m >> vector=[1 2 3 4]; >> suma=sumadorl(vector,10) suma = 11 12 13 14 >> Command Window
Use of functions Editor - C:\Directorio\sumadorl.m function s = sumadorl (sumando1,sumando2) s = sumando1 + sumando2; Editor - C:\Directorio\sumadorl.m Variable names do not need to match inside and outside the function (but they may match). >> vector=[1 2 3 4]; >> suma=sumadorl (vector,10) suma = 11 12 13 14 >> Command Window Input arguments may be either variables or just numbers/vectors typed directly.
Local Variables function s = sumadorl(sumando1,sumando2) s = sumando1 + sumando2; Editor - C:\Directorio\sumadorl.m Local variables: Used inside a function. Each function has its own Workspace, and they never get mixed. >> vector=[1 2 3 4]; >> suma=sumadorl(vector,10) suma = 11 12 13 14 >> Command Window Base Workspace: Used from Command Window. 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 >> s = sin(2); Command Window Input Output
Logical operators 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 Three space requests Reserve space for another number Reserve space for another number >> a=zeros(1,3); >> a(1)=5; >> a(2)=3; >> a(3)=7; Reserve space for three numbers One space request MUCH FASTER Space was already reserved 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