Programming in Matlab - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Programming in Matlab

Description:

Executable m-files are located in the current working ... Create a regular text file with extension .m, e.g. with the commando edit % My startup file ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 24
Provided by: nic47
Category:

less

Transcript and Presenter's Notes

Title: Programming in Matlab


1
Programming in Matlab
  • TSRT01, Lecture 2

2
Outline
  • The Matlab programming environment
  • The Search Path
  • startup file example
  • m-file scripts and functions
  • Flow Control statements
  • Function functions
  • A simulink example

3
The 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

4
The 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

5
The 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
6
The 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
7
Flow control - selection
  • The if-elseif-else construction

if ltrelational expressiongt ltcommandsgt elseif
ltrel. expr.gt ltcommandsgt else ltrel. expr.gt
ltcommandsgt end
8
Flow control - selection
  • Relational operators , , gt, lt, (and),
    (or), (not), xor.
  • Logical tests isempty, any, all

if isempty(who) disp(Empty workspace) else
who end
9
Flow 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)
10
Flow control - repetition
  • for-loops

for kltvectorgt ltstatementsgt end
The commands are executed repeatedly and the
variable k is given each value in the loop-vector
11
Flow control - repetition
  • Display table of factorials

for k112 disp(num2str(k), ,...
num2str(prod(1k))) end
12
Flow control - repetition
  • Display table of factorials

function yfact(n) YFACT(N) Factorial
n!12...n y1 for k2n y yn end
13
Repetition 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
14
Animation demo
  • gtgt movie(gcf,M)

15
Flow Control - Cond. Repetition
  • while-loops

while ltcond. statementgt ltstatementsgt end
The commands are executed repeatedly as long as
the conditional statement evaluates to true
16
Flow 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
17
Flow Control - Cond. Repetition
  • Newtons method for solving nonlinear equations
  • Solve the equation

18
Flow 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)

19
Function 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

20
Example differential equation
General ODE This example is model of an
electronic circuit with a nonlinear vacuum tube,
called van den Pol equation.
21
Example 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))
22
Customize 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
23
Summary
  • 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
Write a Comment
User Comments (0)
About PowerShow.com