Programming with Matlab Day 3 Functions Solving problems

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 >> vector=[1 2 3 4]; >> Difference between functions and scripts Scripts Command Window >> vector=[1 2 3 4]; >>](http://slidetodoc.com/presentation_image_h2/c6286d28756e622212c3f9d7ccf145ec/image-3.jpg)
Difference between functions and scripts Scripts Command Window >> vector=[1 2 3 4]; >> suma >> Editor 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]; >> 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 >> vector=[1 2 3 4]; >> Difference between functions and scripts Functions Command Window >> vector=[1 2 3 4]; >>](http://slidetodoc.com/presentation_image_h2/c6286d28756e622212c3f9d7ccf145ec/image-4.jpg)
Difference between functions and scripts Functions Command Window >> vector=[1 2 3 4]; >> s=suma(vector); >> Editor function s=suma(vec) 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]; >> 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]; Difference between functions and scripts Functions Command Window Workspace >> vec=[1 2 3 4];](http://slidetodoc.com/presentation_image_h2/c6286d28756e622212c3f9d7ccf145ec/image-5.jpg)
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: Directoriofunction_name. m function [output 1, output 2, …] = function_name (input 1, input 2, …) Code output 1=0; for c=1: 10 output 1=output 1+input 1(c); end … output 2=‘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 (output 1, output 2…) when the program finishes, are sent to the workspace.

Use of functions Editor - C: Directoriosumadorl. m function s = sumadorl (sumando 1, sumando 2) s = sumando 1 + sumando 2; Command Window >> vector=[1 2 3 4]; >> suma=sumadorl(vector, 10) suma = 11 >> 12 13 14

Use of functions Editor - C: Directoriosumadorl. m function s = sumadorl (sumando 1, sumando 2) s = sumando 1 + sumando 2; Command Window >> vector=[1 2 3 4]; >> suma=sumadorl (vector, 10) suma = 11 >> 12 13 14 Variable names do not need to match inside and outside the function (but they match). Input arguments may be either variables or just numbers/vectors typed directly.

Local Variables Editor - C: Directoriosumadorl. m function s = sumadorl(sumando 1, sumando 2) s = sumando 1 + sumando 2; 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
- Slides: 12