0% found this document useful (0 votes)
5 views

F13 Lecture 7 Functions

MATLAB Functions

Uploaded by

gitanxtreme
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

F13 Lecture 7 Functions

MATLAB Functions

Uploaded by

gitanxtreme
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

ENGR 108

Introduction to Computing for


Engineers and Scientists
LECTURE 7: FUNCTIONS, INPUT AND OUTPUT

1
Objectives

After this lecture, students will be able to:


◦ Write custom MATLAB functions
◦ Write one-line “anonymous functions”
◦ Use “function functions” to analyze and plot functions

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.

function energy = kineticenergy(mass, vel)


energy = 0.5*mass.*vel.^2;
end

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.

The anonymous function is stored as a variable, and not a function


file. It will be erased when MATLAB is closed, unless it is saved into a
MAT file.

10
Anonymous functions
An anonymous function can have multiple inputs:

>> srss = @(x,y) sqrt(x.^2+y.^2);


>> srss(3,4)
ans =
5
>> srss(5,12)
ans =
13

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

Some functions take other functions as their input. These functions


use handles to specify the function that will be used.
Assuming that f is the handle for the function f(x),
◦ fzero(f, x0) finds where the function associated with the handle f
crosses the x-axis near the initial guess of x0
◦ fminbnd(f, x1, x2) finds the minimum (smallest value) of f(x) for
values of x between x1 and x2
◦ fplot(f, [xmin, xmax]) plots f(x) for xmin < x < xmax.

13
Next week

• Getting input from users


• Formatting output with fprintf
• Comparing data and making decisions

14

You might also like