MATLAB Prog
MATLAB Prog
http://www.s3.kth.se/control/kurser/2E1215/
Today’s Lecture
Matlab programming
Programming environment and search path
M-file scripts and functions
Flow control statements
Function functions
Programming tricks and tips
Programming in Matlab 3
Matlab environment
Matlab construction
Core functionality as compiled C-code, m-files
Additional functionality in toolboxes (m-files)
Script files
factscript.m
factscript.m
%FACTSCRIPT
%FACTSCRIPT –– Compute
Compute n-factorial,
n-factorial, n!=1*2*...*n
n!=1*2*...*n
yy == prod(1:n);
prod(1:n);
Functions
[output_arguments]= function_name(input_arguments)
% Comment lines
<function body> factfun.m
factfun.m
function
function [z]=factfun(n)
[z]=factfun(n)
%% FACTFUN
FACTFUN –– Compute
Compute factorial
factorial
%% Z=FACTFUN(N)
Z=FACTFUN(N)
zz == prod(1:n);
prod(1:n);
>> y=factfun(10);
Programming in Matlab 9
Functions
Take inputs, generate outputs, have internal variables
Solve general problem for arbitrary parameters
Scripts
Operate on global workspace
Document work, design experiment or test
Solve a very specific problem once
Exam: all problems will require you to write functions
facttest.m
facttest.m
%% FACTTEST
FACTTEST –– Test
Test factfun
factfun
N=50;
N=50;
y=factfun(N);
y=factfun(N);
Programming in Matlab 10
if <logical expression>
<commands>
elseif <logical expression>
if
if height>170
height>170
<commands>
disp(’tall’)
disp(’tall’)
else
elseif
elseif height<150
height<150
<commands>
disp(’small’)
disp(’small’)
end
else
else
disp(’average’)
disp(’average’)
end
end
Programming in Matlab 11
Logical expressions
for index=<vector>
<statements>
end
The <statements> are executed repeatedly.
At each iteration, the variable index is assigned
a new value from <vector>.
for
for k=1:12
k=1:12
kfac=prod(1:k);
kfac=prod(1:k);
disp([num2str(k),’
disp([num2str(k),’
‘,num2str(kfac)])
‘,num2str(kfac)])
end
end
Programming in Matlab 13
for
for k=1:n
k=1:n
kfac=prod(1:k);
kfac=prod(1:k);
disp([num2str(k),’
disp([num2str(k),’ ’,num2str(kfac)])
’,num2str(kfac)])
y(k)=kfac;
y(k)=kfac;
end;
end;
Programming in Matlab 14
logomovie.m
logomovie.m
%% logomovie
logomovie –– make
make movie
movie of
of 360
360 degree
degree logo
logo
tour
tour logo;
logo;
no_frames=40;
no_frames=40;
dtheta=360/no_frames;
dtheta=360/no_frames;
for
for frame
frame == 1:no_frames,
1:no_frames,
camorbit(dtheta,0)
camorbit(dtheta,0)
M(frame)
M(frame) == getframe(gcf);
getframe(gcf);
end
end
%% now
now display
display captured
captured movie
movie
movie(gcf,M);
movie(gcf,M);
Programming in Matlab 15
while-loops
while <logical expression>
<statements>
end
<statements> are executed repeatedly as long as the
<logical expression> evaluates to true
k=1;
k=1;
while
while prod(1:k)~=Inf,
prod(1:k)~=Inf,
k=k+1;
k=k+1;
end
end
disp([‘Largest
disp([‘Largest factorial
factorial in
in Matlab:’,num2str(k-1)]);
Matlab:’,num2str(k-1)]);
Programming in Matlab 16
newton.m
newton.m
function
function [x,n]
[x,n] == newton(x0,tol,maxit)
newton(x0,tol,maxit)
%% NEWTON
NEWTON –– Newton’s
Newton’s method
method for
for solving
solving equations
equations
%% [x,n]
[x,n] == NEWTON(x0,tol,maxit)
NEWTON(x0,tol,maxit)
xx == x0;
x0; nn == 0;
0; done=0;
done=0;
while
while ~done,
~done,
nn == nn ++ 1;
1;
x_new
x_new == xx -- (exp(-x)-sin(x))/(-exp(-x)-
(exp(-x)-sin(x))/(-exp(-x)-
cos(x));
cos(x));
done=(n>=maxit)
done=(n>=maxit) || (( abs(x_new-x)<tol
abs(x_new-x)<tol );
);
x=x_new;
x=x_new;
end
end
>> [x,n]=newton(0,1e-3,10)
Programming in Matlab 18
Function functions
myfun.m
myfun.m
function
function [f,f_prime]
[f,f_prime] == myfun(x)
myfun(x)
%% MYFUN–
MYFUN– Evaluate
Evaluate f(x)
f(x) == exp(x)-sin(x)
exp(x)-sin(x)
%% and
and its
its first
first derivative
derivative
%% [f,f_prime]
[f,f_prime] == myfun(x)
myfun(x)
f=exp(-x)-sin(x);
f=exp(-x)-sin(x);
f_prime=-exp(-x)-cos(x);
f_prime=-exp(-x)-cos(x);
Programming in Matlab 19
Function functions
myodefun.m
myodefun.m
function
function x_dot
x_dot == myodefun(t,x)
myodefun(t,x)
%% MYODEFUN
MYODEFUN –– Define
Define RHS
RHS of
of ODE
ODE
x_dot(1,1)=x(2);
x_dot(1,1)=x(2);
x_dot(2,1)=-x(1)+0.1*(1-x(1)^2)*x(2);
x_dot(2,1)=-x(1)+0.1*(1-x(1)^2)*x(2);
Summary