Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Matlab

Similar presentations


Presentation on theme: "Programming with Matlab"— Presentation transcript:

1 Programming with Matlab
Day 3: Functions

2 Solving problems: Functions
Function: Code that performs a specific task FUNCTION (Suma) INPUT (vector) OUTPUT (number)

3 Difference between functions and scripts
Command Window >> vector=[ ]; >> n_elems=length(vector); >> s=0; >> for c_elems=1:n_elems s=s+vector(c_elems); end >> Command Window = >> vector=[ ]; >> 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.

4 Difference between functions and scripts
Command Window >> vector=[ ]; >> n_elems=length(vec); >> s=0; >> for c_elems=1:n_elems s=s+vec(c_elems); end >> Command Window >> vector=[ ]; >> 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

5 Difference between functions and scripts
vec Workspace Command Window >> vec=[ ]; >> 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

6 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.

7 Use of functions Editor - C:\Directorio\sumadorl.m
function s = sumadorl (sumando1,sumando2) s = sumando1 + sumando2; Editor - C:\Directorio\sumadorl.m >> vector=[ ]; >> suma=sumadorl(vector,10) suma = >> Command Window

8 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=[ ]; >> suma=sumadorl (vector,10) suma = >> Command Window Input arguments may be either variables or just numbers/vectors typed directly.

9 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=[ ]; >> suma=sumadorl(vector,10) suma = >> 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.

10 Turns out, we have been using functions all the time
>> s = sin(2); Command Window Input Output

11 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)

12 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


Download ppt "Programming with Matlab"

Similar presentations


Ads by Google