F13 Lecture 7 Functions
F13 Lecture 7 Functions
1
Objectives
2
Custom functions
Every function we’ve used so far in this class is built into MATLAB.
However, we can write our own functions that work the same way.
Say we want to make a function that takes the input value and
multiplies it by 2. This function will be called twice.
3
Writing a function
We start by making a new function file (File > New > Function).
The first line of the function file is the function definition:
function [out1, out2, ...] = funcname(in1, in2, ...)
• The first word is always “function”.
• The second part is the list of output variables, where the output from
the function will be stored.
• The third part is the name of the function. The function name must
meet all the requirements of variable names (letters, numbers and
underscores; has to start with a letter).
• The fourth part is the list of input variables, the name(s) MATLAB will
give to the inputs given to the function.
4
How our function works
The first line defines the
function. When someone uses
this function, MATLAB puts the
input into the variable x and will
function t = twice(x)
output whatever is in t.
% TWICE doubles a number
% TWICE(X) returns 2*X. The second line does a
t = 2*x; calculation with x and stores the
end result in t.
The last line tells MATLAB that
the function is over, and to return
the contents of t.
5
Saving and using our function
>> twice(1)
ans =
2
Save the function file with the
>> A = 5; twice(A)
same name as the function so
ans =
that MATLAB can find it. So, we 10
save the twice function as the >> I = eye(2)
file twice.m. I =
Now we can use our new 1 0
0 1
function in scripts and the
>> twice(I)
Command Window. ans =
2 0
0 2
6
Function scope
All the variables in a function >> twice(5)
are local to the function. They ans =
exist only in a special workspace
belonging to the function, called 10
its scope. >> t
A function cannot access or ??? Undefined function or
overwrite variables outside its variable ‘t'.
scope, and a script can’t access
MATLAB can’t see the contents of
variables used inside a function. t, because it is local to twice.
7
Functions with more
than one input
1
Here is a function that will calculate the kinetic energy 𝐾𝐸 = 𝑚𝑣 2 of
2
an object, given its mass and its velocity. Notice that this function takes
two inputs and uses them both to calculate the output.
8
Functions with no output
Not every function needs to have output variables.
Here is a function that will make a plot of sin 𝑥 for 𝑎 < 𝑥 < 𝑏. It takes
two inputs, but doesn’t have any variables to output.
function sinplot(a,b)
% SINPLOT produces a graph of the sine function
% SINPLOT(a,b) graphs sin(x) over the interval [a, b].
x = linspace(a, b, 100);
y = sin(x);
plot(x,y)
end
Anonymous functions
For simple one-line functions, MATLAB provides a shortcut way of
defining a function called anonymous functions. Here is the twice
function rewritten as an anonymous function:
twice = @(x) 2*x;
◦ twice is the handle of the function.
◦ @ tells MATLAB that this will be an anonymous function.
◦ x is the name of the input variable.
◦ The function will output 2*x.
10
Anonymous functions
An anonymous function can have multiple inputs:
11
Function handles
Function handles can be used for regular functions, both built-in ones
and ones you write yourself.
For example, to make a handle t to the twice function:
t = @twice;
So why do we care about function handles?
12
Function functions
13
Next week
14