Title: Programming in Matlab
1Programming in Matlab
2Outline
- The Matlab programming environment
- The Search Path
- startup file example
- m-file scripts and functions
- Flow Control statements
- Function functions
- A simulink example
3The programming environment
- The working directory is controlled by
- gtgt dir
- gtgt cd catalogue
- gtgt pwd
- The matlab search path defines where matlab
searches for m-files - gtgt path
- gtgt addpath
- gtgt pathtool
- gtgt which function
4The programming environment
- Executable m-files are located in the current
working directory or somewhere on the search
path. - The function/variable duality causes Matlab to
search for an identifier according to the scheme - 1. variable in current workspace
- 2. built-in variable
- 3. built-in m-file
- 4. m-file in current directory
- 5. m-file on search path
5The programming language
- Script-files contain Matlab lines just as you
type them inside Matlab
fact.m
YFACT(N) Factorial n!12...n y prod(1n)
Tip Start with a comment line To type the file
(script of function), do gtgt type fact
6The programming language
- Function m-files have local variables and always
start with function
fact.m
function yfact(n) YFACT(N) Factorial
n!12...n y prod(1n)
The first consecutive comment rows are displayed
by the command gtgt help fact
7Flow control - selection
- The if-elseif-else construction
if ltrelational expressiongt ltcommandsgt elseif
ltrel. expr.gt ltcommandsgt else ltrel. expr.gt
ltcommandsgt end
8Flow control - selection
- Relational operators , , gt, lt, (and),
(or), (not), xor. - Logical tests isempty, any, all
if isempty(who) disp(Empty workspace) else
who end
9Flow control - selection
- The if-elseif-else construction
fact.m
function yfact(n) YFACT(N) Factorial
n!12...n if nargin lt 1 error(no input
assigned) elseif n lt 0 error(input must be
non-negative) elseif abs(n-round(n)) gt eps
error(input must be integer) end y prod(1n)
10Flow control - repetition
for kltvectorgt ltstatementsgt end
The commands are executed repeatedly and the
variable k is given each value in the loop-vector
11Flow control - repetition
- Display table of factorials
for k112 disp(num2str(k), ,...
num2str(prod(1k))) end
12Flow control - repetition
- Display table of factorials
function yfact(n) YFACT(N) Factorial
n!12...n y1 for k2n y yn end
13Repetition Animation demo
- Construct a script m-file that saves the movie,
frame by frame, in the variable M
logomovie.m
logo dtheta 10 for k 1360/dtheta M(k)
getframe(gcf) camorbit(dtheta,0) end
Note Prior to Matlab 5.3 the movie needs to be
initated by the moviein command
14Animation demo
15Flow Control - Cond. Repetition
while ltcond. statementgt ltstatementsgt end
The commands are executed repeatedly as long as
the conditional statement evaluates to true
16Flow Control - Cond. Repetition
- What is the largest factorial Matlab can compute?
Compare the implementations
k1 While prod(1k)Inf, kk1 end k
for k1100000 if prod(1k)Inf, k, return,
end end
17Flow Control - Cond. Repetition
- Newtons method for solving nonlinear equations
- Solve the equation
18Flow Control - Cond. Repetition
newton.m
function x,n newton(x0,tol,nmax) X,N
NEWTON(X0,TOL,MAXIT) x x0 n 0 while (n lt
nmax) (abs(x-x0)gttoln0) n n 1 x0
x x x - (exp(-x)-sin(x))/...
(-exp(-x)-cos(x)) end
- gtgt x,nnewton(0,1e-3,10)
19Function functions
- General purpose functions take other m-files as
input, e.g. solution of ODEs. - Several methods are provided in Matlab for
solving ordinary differential equations - gtgt help ode45
- a Runge-Kutta method of medium order for
non-stiff problems
20Example differential equation
General ODE This example is model of an
electronic circuit with a nonlinear vacuum tube,
called van den Pol equation.
21Example differential equation
- Construct an m-file function for the right-hand
side of
fvdp.m
function dxdt fvdp(t,x) dxdt fvdp(t,x)
van der Pol equation dxdt x(2,) ...
(1-x(1,).2).x(2,)-x(1,)
Solve the ODE in the time span 0lttlt10 gtgt t,x
ode45(fvdp,0,10,20) gtgt plot(t,x) gtgt
plot(x(,1),x(,2))
22Customize startup
- Create a regular text file with extension .m,
e.g. with the commando - gtgt edit
startup.m
My startup file addpath(C\MATLABR11\work\proje
ct) disp(Welcome to Matlab)
Save this m-file somewhere on the search path, it
will execute automatically when Matlab is launched
23Summary
- User added m-files must be stored in the current
directory or on the search path - The functions have local variables while scripts
operate on the global workspace - Flow control is handled by if-statements,
for-loops and while-loops - General purpose functions take other functions as
input argument