Vous êtes sur la page 1sur 3

_____________________________________________________________________________________________________________________________________

OTTO-VON-GUERICKE-UNIVERSITT MAGDEBURG
Fakultt fr Elektrotechnik und Informationstechnik
Lehrstuhl fr Automatisierungstechnik / Modellbildung
Prof. Dr.Ing. A. !ienle
_____________________________________________________________________________________________________________________________________
Process Control
Short Matlab introduction
"his is onl# a brief introduction$ #ou can get the full documentation set about MA"LA% at
htt&'//(((.math(orks.com/access/hel&desk/hel&/techdoc/matlab.html. MA"LA%$ short for Matri)
Laborator#$ is a sim&le and fle)ible &rogramming en*ironment for a (ide range of numerical
&roblems. MA"LA% is an inter&reted language. "his im&lies that the source code is not com&iled but
inter&reted on the fl#. "his is both an ad*antage and a disad*antage. MA"LA% allo(s for eas#
numerical calculation and *isuali+ation of the results (ithout the need for ad*anced and time
consuming &rogramming. "he disad*antage is that it can be slo($ es&eciall# (hen bad &rogramming
&ractices are a&&lied.
MATLAB Help
MA"LA% has a great online hel& s#stem accessible using the hel& command. %# t#&ing help in the
command (indo( ,and &ressing Enter$ of course- #ou (ill get an o*er*ie( about the hel& to&ics in
Matlab. .ou can get more information about the *arious to&ics b# t#&ing help <topicname>$ i.e.
help general to get a list about general &ur&ose commands a*ailable in Matlab. If #ou (ant to
kno( more about a s&ecific function then t#&e help <functionname> in order to get a detailed
e)&lanation of the functions &ur&ose. /ften used general &ur&ose commands are whos ,list *ariables
in (orks&ace- $ clear ,deletes all *ariables-$ edit ,o&ens an editor- and cd ,change (orking
director#-. Find out more about this commands b# t#&ing help whos $ help clear $ help edit
and help cd. .ou can get more hel& b# using the 01el& menu0 or tr# the commands helpdesk or
demo.
Matrices, vectors and scalars
MA"LA% uses matrices as a basic *ariable t#&e. 2calars and *ectors are s&ecial cases of matrices
ha*ing si+e 3)3 ,scalar-$ 3)4 ,ro( *ector- or 4)3 ,column *ector-. In MA"LA% there are a fe(
con*entions for entering matrices'
a matri) is entered b# using s5uare brackets$ 6 7
ro( elements are se&arated b# blanks or commas
each ro( is ended b# a semicolon$ 8
E)am&le' 9reating real scalars$ *ectors and matrices
x = 1 ,scalar-
y = [2 4 6 8 10] ,ro( *ector-
z = [2; 4; 6; 8; 10] ,column *ector-
A = [4 3 2 1 0; 1 3 5 7 9] ,: ) ; matri)-
It is also common to enter *ectors$ matrices b# defining its start$ increment and end *alue and
se&arate this three *alues b# colons' y = <start>:<increment>:<end>, i.e. y= 2 : 2 : 10
,ro( *ector-$ A=[4:-1:0;1:2:9] ,matri)-. Information about commands concerning elementar#
matrices and matri) mani&ulation can be found b# t#&ing help elmat, i.e. eye ,defines identit#
matri)-$ length ,gi*es *ector length-.
Indexing matrices and vectors
<i*en a matri) M the element in the ith ro($ =th column is gi*en b# M,i$=-. For a *ector * the ith
element is gi*en b# *,i-. 4ote that the lo(est inde) allo(ed in MA"LA% is 3."he colon o&erator is also
of great hel& (hen accessing s&ecific &arts of matrices and *ectors$ as sho(n belo(.
E)am&le' >sing indices in matrces and *ectors
A(1,2) returns the element in the 3. ro( and second column of A
A(1,:) returns the first ro( of the matri) A
A(:,3) returns the third column of the matri) A
A(2,1:5) returns the first fi*e elements of the second ro(
A(1,end) returns the last column of A
Basic operators and unctions
"#&e help ops to get an o*er*ie( about a*ailable o&erators in Matlab and ho( to use them
,information about inde)ing of matrices and *ectors can also be found there (ith help paren-. If #ou
(ant detailed information about an o&erator t#&e help <operatorname>$ i.e. help mtimes to get
info about the o&erator for matri) multi&lication. "he most o&erators in Matlab are *ectori+ed meaning
that the# can be used both for matrices$ *ectors and scalars. /ften used o&erators are the
element(ise di*ision A./2 $ the element(ise multi&lication A.*2 and the element(ise &o(er A.^2
calculation of a gi*en matri) A (ith the scalar :.
?ith help elfun #ou can get info about elementar# mathematical functions in Matlab ,i.e. sin,
cos, exp ...- and (ith help matfun about matri) functions ,i.e. inv ,com&utes in*erse matri)-$ eig
,com&utes eigen*alues and *ectors-$ det ,determinant--. "he most functions in Matlab are also
*ectori+ed meaning that the# can be used both for matrices$ *ectors and scalars$ i.e. sin(X)
,calculates the sine of the elements of matri) @-.
E)am&les' some mathematical terms ,left- and its Matlab re&resentation ,right-8
)$#$+ are scalars8 M$4$A are real matrices'

z=x
2
4e
2y
+ A )B: C DEe)&,:E #-
A=M
T
N
1
A A MF E in*,4-
Basic plotting
"#&e help graph2d to get an o*er*ie( about commands for :dimensional &lot generation. /ften
used commands in this conte)t are plot ,create :D &lots-$ xlabel ,label )a)is-$ ylabel ,label #
a)is-$ title ,add title- and legend ,add legend-. Find out more about this commands (ith help.
E)am&le' 5uadratic func.'
y=x
2
, x [2;2]

x = -2:0.1:2;
y = x.^2;
figure;
plot(x,y,'k');
xlabel('x');
ylabel('y');
title('simple plot');
Gesult'
/ther useful basic &lotting commands are grid on/off ,adds/remo*es a rectangular grid to #our
&lot-$ hold on/off ,&lot se*eral gra&hs in one &lot-. 4ote that ; at the end of each line is used to
su&&ress Matlab out&ut.
Basic Programming
.ou can enter #our Matlab commands in t(o different (a#s' one b# one in the Matlab command
(indo( or #ou can &ut them in files$ socalled Mfiles. Mfiles are created (ith a te)t editor ,note'
edit o&ens a te)t editor and a ne( Mfile- and can then be called b# t#&ing its name in the Matlab
command (indo(. "here are t(o kinds of Mfiles'
2cri&ts$ (hich do not acce&t in&ut arguments and (ork (ith ,global- *ariables
in the Matlab (orks&ace.
Functions$ (hich acce&t in&ut arguments and return out&ut arguments and (ork (ith
their o(n ,local- *ariables
In the follo(ing is an e)am&le of the 5uadratic function &lotting both as a scri&t and as a function.

E)am&le' scri&t file content ,left- and function file content ,right-
% quadratic function plotting
x = -2:0.1:2;
y = x.^2;
figure;
plot(x,y,'k');
xlabel('x');
ylabel('y');
title('simple plot');
function y = plotqfunc(x)
% PLOTQFUNC plots quadratic function
z = x.^2;
y = z;
figure;
plot(x,y,'k');
xlabel('x');
ylabel('y');
title('simple plot');
"he scri&t o&erates (ith *ariables ,x$y- in the Matlab (orks&ace. "he commands in the scri&t file are
e)ecuted b# t#&ing the scri&tname in the Matlab command (indo(.
"he function has one in&ut argument ,x- and one out&ut argument ,y-. "he in&ut argument must be
declared in the Matlab (orks&ace ,i.e. (ith x=-2:0.1:2;- before the function can be called. "he
function is called b# t#&ing its name and the in&ut argument in the Matlab command (indo($ i.e.
y=plotqfunc(x). 4ote that the file name and the function name ha*e to be the same. 4ote further
that z is not *isible in the Matlab (orks&ace ,it is a local *ariable$ declared inside the function- and
that % is used to insert comments in the Mfiles.
%efore #ou can e)ecute the scri&t and the functions files the# ha*e to be in the current (orking
director# of Matlab. >se the command pwd to sho( the current (orking director# and cd to change it.
>se distinct names for #our scri&t and functions files in order to a*oid conflicts (ith alread# e)isting
Matlab functions$ i.e. use #our name as &art of the file name.
"here are se*eral constructs (hich can be used in Mfiles to control the &rogram flo('
if ,conditional branching-$
switch ,conditional branching-$
for ,determined loo&-$
while ,undetermined loo&-$
continue ,b#&ass loo& iteration-$
break ,lea*e loo&-.
.ou can get more information about them using the hel& command. "#&e help lang to get more
information about &rogramming in Matlab.
Matlab includes man# demonstration &rograms that highlight *arious features and functions. "#&e
help demos in the command (indo( to get an o*er*ie(.
References:
[1] !!":##$$$.%&!$'r(s.c'%#"r')*c!s#%&!+&,#,''(s.-s"

Vous aimerez peut-être aussi