L08 14Feb11
X
MATLAB Writing Your Own Functions
3-minute exercise
3-minute exercise
Function syntax
Function input and output
Functions “pass by value”
Functions “pass by value”
Function input and output
Function input and output
Function input and output parameters
Local variables
Use of MATLAB functions
Functions with multiple returns
Debugging MATLAB functions
00:00
/
00:00
CC
MATLAB
Writing
Your
Own
Functions
3-minute
exercise
%
whatsitdo1.m
Nt=300;
xmax=10;
x=linspace(0,xmax,Nt);
y=0.95+0.05*sin(2*pi*x);
%
plot
for
it=1:2:Nt
plot(x(1:it),y(1:it),...
x(it),y(it),'ro');
axis([0
xmax
0
1.2]);
grid
on
drawnow
end
EG112
2
What
does
plot
look
like?
3-minute
exercise
%
whatsitdo2.m
Nt=300;
xmax=10;
x=linspace(0,xmax,Nt);
y=0.95+0.05*sin(2*pi*x);
%
plot
tailLength=35;
for
it=1:2:Nt
itail=max([1,it-tailLength]);
plot(x(itail:it),y(itail:it),...
x(it),y(it),'ro');
axis([0
xmax
0
1.2]);
grid
on
drawnow
end
EG112
3
What
does
plot
look
like?
Function
syntax
mypoly.m
function
y=mypoly(x)
a=1;
b=2;
c=3;
y=a*x.^2+b*x+c;
EG112
4
Defining
a
function:
First
line
must
start
with
the
word
“function”
and
the
declaration.
File
name
must
be
the
same
as
the
function
name.
Function
must
calculate
value
to
be
returned.
Function
can
be
used
like
other
MATLAB
function.
z=mypoly(1.2);
Function
input
and
output
EG112
5
mypoly.m
function
y=mypoly(x)
a=1;
b=2;
c=3;
y=a*x.^2+b*x+c;
name
input
return
variable
function
must
assign
a
value
to
the
return
Functions
“pass
by
value”
>>
a=101;
>>
z=2;
>>
f=mypoly(z);
>>
disp(f)
11
>>
disp(z)
2
>>
disp(a)
101
EG112
6
mypoly.m
function
y=mypoly(x)
a=1;
b=2;
c=3;
y=a*x.^2+b*x+c;
The
value
stored
in
the
external
variable
z
is
passed
to
the
function
by
storing
it
in
the
local
variable
named
x.
Similarly
the
value
stored
in
the
local
variable
y
is
copied
to
the
external
variable
f.
Functions
“pass
by
value”
>>
a=101;
>>
z=2;
>>
f=mypoly(z);
>>
disp(f)
11
>>
disp(z)
2
>>
disp(a)
101
EG112
6
mypoly.m
function
y=mypoly(x)
a=1;
b=2;
c=3;
y=a*x.^2+b*x+c;
The
value
stored
in
the
external
variable
z
is
passed
to
the
function
by
storing
it
in
the
local
variable
named
x.
Similarly
the
value
stored
in
the
local
variable
y
is
copied
to
the
external
variable
f.
Function
input
and
output
EG112
7
a=101;
z=2;
f=mypoly(z);
function
y=mypoly(x)
2
2
z
x
1
a
2
b
3
c
11
y
f
11
101
a
function
y=mypoly(x)
a=1;
b=2;
c=3;
y=a*x.^2+b*x+c;
local
variables:
x,
a,
b,
c,
and
y
Function
input
and
output
EG112
7
a=101;
z=2;
f=mypoly(z);
function
y=mypoly(x)
2
2
z
x
1
a
2
b
3
c
11
y
f
11
101
a
function
y=mypoly(x)
a=1;
b=2;
c=3;
y=a*x.^2+b*x+c;
local
variables:
x,
a,
b,
c,
and
y
Function
input
and
output
parameters
Input
parameters
being
“passed
by
value,”
means
that
the
function
cannot
alter
the
value
of
a
variable
in
the
calling
statement.
The
function
can
only
directly
alter
the
value
of
variables
through
its
output.
(A
function
can
do
other
things,
e.g.,
make
plots,
sounds,
etc.)
EG112
8
a=101;
z=2;
f=mypoly(z);
Local
variables
Variables
created
inside
the
function
are
local—not
accessible
outside
the
function.
Local
variables
can
have
the
same
name
as
variables
outside
the
function
without
conflict.
This
enables
one
to
write
a
function
without
concern
about
variables
named
outside
the
function.
Each
function
has
its
own
private
workspace.
Local
variables
are
temporary.
They
are
created
when
the
function
runs
and
disappear
afterwards.
EG112
9
Use
of
MATLAB
functions
Use
functions
to:
Extend
the
language
by
adding
new
functions
and
commands.
Break
down
large
programs
into
simpler
tasks.
Develop
a
reusable
library.
Function
Prime
Directive:
Each
function
should
do
one
thing
and
do
it
well.
EG112
10
Functions
with
multiple
returns
Syntax:
[var1,
var2,
…]=funcName(x1,
x2,
x3,…);
After
var1,
other
returned
variables
are
optional,
but
they
must
occur
in
the
prescribed
order.
Each
input
or
output
variable
can
be
any
MATLAB
data
structure:
double,
string,
vector,
matrix,
other
Specify
in
header
comments
what
each
input
and
output
represent.
>>help
myfun
prints
header
comments
EG112
11
Debugging
MATLAB
functions
Cannot
run
function
without
argument
by
pressing
green
save-and-run
button.
Often
helpful
to
have
a
test
main
program
Set
run
configuration
file
(next
to
Save-and-run)
EG112
12