Sie sind auf Seite 1von 46

Control Systems

Lab no.1
Introduction to Matlab

What is MATLAB?
MATLAB is a numerical computing environment
and programming language.
Matlab is a commercial MATrix LABoratory
Package and it operates as an interactive
programming environment with the graphical
output.

History
MATLAB was invented in the late 1970s by Cleve
Moler, then chairman of the computer science
department at the University of New Mexico.
The purpose was to use certain software packages
without any need to learn Fortran.
In 1984 MATLAB was rewritten in C language.

In Engineering MATLAB was first used by


Control Systems engineers.
3

Certain Features of MATLAB


It Allows:
Easy matrix manipulation
Plotting of functions and data
Implementation of algorithms
Creation of user interfaces
It can work with the UNIX Platform as well
as with Windows.
In Matlab almost every data object is
assumed to be an array.
4

ENTERING AND RUNNING MATLAB


On a system running Windows double click on the
MATLAB icon to launch and a command window
will appear with the prompt.
You are now in MATLAB. From this point on,
individual MATLAB commands may be given at
the program prompt.

Current
Directory

Command
Window

Command
History
6

Command Window
It is the primary user interface
type commands

Workspace
view program variables
clear to clear all the variables of work space
double click on a variable to see it in the Array
Editor

Command History
view past commands
save a whole session using diary

Current Directory
directory into which generated output files are
written

The Command window is where you


can submit commands to Matlab and
run programs.

The workspace is a convenient way to


look at variables youve created.

LEAVING MATLAB
A MATLAB session may be terminated by
simply typing
>> quit
or by typing
>> exit
at the MATLAB prompt.
Or simply clicking the cross button
10

Matlab statements can be prepared in the Editor


Window and stored in a file called Script file or
M file". (since there should be a name extension
of the form filename.m)

Demonstrations of various Matlab commands can


be explored using demo.
Help command can also be used to explore the
Matlab functions in a more specific way.

11

ONLINE HELP
>> help
[a long list of help topics
follows]
For specific commands:
>> help demo
A demonstration of various MATLAB
functions can be explored with:
>> demo
A performance test of the machine running
MATLAB can be done with:
>> bench

12

VARIABLES
MATLAB has built-in variables like pi, ans etc.
You can learn their values from the
MATLAB interpreter.
>> pi
ans =
3.1416
>> help pi
PI 3.1415926535897....
The variable ans will keep track of the last output
which was not assigned to another variable.
13

Variable Assignment : The equality sign is


used to assign values to variables:
>> x = 3
x =
3
y =
9
Variables in MATLAB are case sensitive.
Hence, the variables "x" and "y" are distinct
from "X and "Y.
14

MATLAB variable name must be one word.


Underscore_ must be used if variable
name exceeds one word.
Variable name can contain up to 63
characters while beyond 63rd are ignored
If at any time you forget your variables, you
can use the command who (who my
variables are).
To remove a variable, use command
clear x (variable).
15

Semicolon
Output can be suppressed by appending a
semicolon to the command lines.
>> x = 3;
>> y = x^2;
>> y
y =
9

16

Operators (arithmetic)
+ addition

- subtraction
* multiplication
/ division (left and right)
^ power
17

MATLAB Special Variables


ans
pi
eps
inf
NaN
i and j
realmin
realmax

Default variable name for results


Value of
Smallest incremental number
Infinity
Not a number
e.g. 0/0
i = j = square root of -1
The smallest usable positive real number
The largest usable positive real number
18

Other MATLAB symbols


>>
...
,
%
;
:

prompt
continue statement on next line
separate statements and data
start comment which ends at end of line
(1) suppress output
(2) used as a row separator in a matrix
specify range
19

VARIABLE ARITHMETIC
MATLAB uses some fairly standard
notation. More than one command may be
entered on a single line, if they are
separated by commas.
>> 2+3;
>> 3*4, 4^2;
Order of Precedence
Powers are performed before division and
multiplication, which are done before
subtraction and addition
20

Example
>> 2+3*4^2;
generates ans = 50. That is:
2+3*4^2 ==> 2 + 3*4^2 <==
exponent has the highest
precedence
==> 2 + 3*16 <== then
multiplication operator
==> 2 + 48 <== then addition
operator
==> 50
21

BUILT-IN MATHEMATICAL FUNCTIONS


There are more than 300 built in functions in
MATLAB out of which some mathematical
functions are:
sin ;sin(pi) = 0.0
cos ;cos(pi) = 1.0
tan ;tan(pi/4) = 1.0
Arcsine; asin(pi/2)= 1.0
Arccosine; acos(pi/2)= 0.0
arctangent; atan(pi/4)= 1.0
Exponential; exp(1.0) = 2.7183
natural logarithm(ln); log(2.7183)
= 1.0
Common logarithm (base10)
log10(100.0) = 2.0
22

An example of built-in mathematical function is


taking a square root of any no and assign the result
to the variable. e.g.
a=sqrt(2) or b=sin(pi/2) or c=log(30)
Lets verify that sin(90)^2+cos(90)^2=1

23

Matrices
A Matrix is a rectangular array of numbers, every
Matlab variable we define refers to a matrix
[ a single no is a 1 by 1 matrix]
There are many applications of matrices related to
the field of Engineering e.g. families of equations
representing the state of the system( Equilibrium
equations, Energy & momentum conservation
equations). These all can be easily analyzed using
matrices.
Due to the above stated facts MATLAB helps us
in defining the matrices and their solution.
24

Example
Here is an example which shows how a
matrix is defined in a MATLAB:
a=[1 2 4; 5 8 2; 7 4 1] (3 by 3 order)
similarly
b=[4 5; 9 0] (2 by 2 order)
or in other way
c=[2 8 4; 0 2 1] (2 by 3 order).

25

Operations on Matrices
Matrices can be added or subtracted using Matlab
but their order should be same,
try few examples in matlab

In the same way we can multiply two matrices but


the product should obey the law of multiplication
of matrices,
try few examples in matlab

26

Built-in Matrices
There are many built in matrices available in the
Matlab
1) Zeros(3) is a 3x3 Zero matrix
Zeros(2,3) is a 2x3 Zero matrix
2) eye(3) is a 3x3 identity matrix
eye(3x2) is a 3x2 identity matrix

27

3) ones(3) is a 3x3 matrix of 1s


ones(2,3) is a 2x3 matrix of 1s

4) MAGIC Magic square.


MAGIC(N) is an N-by-N matrix constructed from the
integers
equal row, column, and diagonal sums.
Produces valid magic squares for all N > 0 except N
= 2.

28

RAND(N) is an N-by-N matrix with random


entries, chosen from a uniform distribution on the
interval (0.0,1.0).
& many more.. So search and practice yourself

29

Size of the matrix


Size of the matrix can be determined using the
command size(a) or size(b)
The output appears to be
Rows

Columns

30

Transpose of a matrix
Transpose of a matrix is interchanging the
rows and columns
Matlab is used to find the transpose of a
matrix with the help of (apostrophe) like a
or b or c

31

Round Floating Point numbers to Integers

round(a)
fix(a)
ceil(a)
floor(a)

32

a=

0.1000 0.9000 0.5000 0.4500 -0.1000


-0.9000 -0.5000 -0.4500
round(a)
Round towards nearest integer.
ans =
0 1 1 0 0 -1 -1 0
fix(a)
Round towards zero.
ans =
0 0 0 0 0 0 0 0
33

ceil(a)
Round towards plus infinity.
ans =
1 1 1 1 0 0 0

floor(a)
Round towards minus infinity.
ans =
0 0 0 0 -1 -1 -1 -1

34

Indexing Matrices
Indexing using parentheses
>> A(2,3)
Index submatrices using vectors
of row and column indices
>> A([2 3],[1 2])

35

Index complete row or column using


the colon operator
>> A(1,:)
Can also add limit index range
>> A(1:2,:)
>> A([1 2],:)

36

Plotting with MATLAB


MATLAB will plot one vector vs. another. The
first one will be treated as the abscissa (or x)
vector and the second as the ordinate (or y) vector.
The vectors have to be the same length.
MATLAB will also plot a vector vs. its own
index. The index will be treated as the abscissa
vector. Given a vector time and a vector dist
we could say:
>> plot (time, dist)
>> plot (dist)

% plotting versus time


% plotting versus index
37

Plotting with MATLAB


There are commands in MATLAB to "annotate" a
plot to put on axis labels, titles, and legends. For
example:
>> % To put a label on the axes we would use:
>> xlabel ('X-axis label')
>> ylabel ('Y-axis label')
>> % To put a title on the plot, we would use:
>> title ('Title of my plot')
38

Some Useful MATLAB commands

who
whos
help
clear
clear x
clc

List known variables


List known variables plus their size
e.g. >> help sqrt
Help on using sqrt
Clear all variables from work space
Clear variables x and y from work space
Clear the command window

39

Some Useful MATLAB commands

what
dir
ls
pwd

List all m-files in current directory


List all files in current directory
Same as dir
Shows path of current directory

40

A Useless, But Interesting,


MATLAB command

why

In case you ever needed a reason

41

MATLAB Relational Operators


MATLAB supports six relational operators.
Less Than
Less Than or Equal
Greater Than
Greater Than or Equal
Equal To
Not Equal To

<
<=
>
>=
==
~=
42

MATLAB Logical Operators


MATLAB supports three logical operators.

not
and
or

~
&
|

% highest precedence
% equal precedence with or
% equal precedence with and

43

MATLAB Selection Structures


An if - elseif - else structure in MATLAB.
Note that elseif is one word.

if expression1
% is true
% execute these commands
elseif expression2
% is true
% execute these commands
else
% the default
% execute these commands
end
44

MATLAB Repetition Structures


A for loop in MATLAB
for x = 1: 0.5 : 10
% execute these commands
end

for x = array

A while loop in MATLAB while expression


while x <= 10
% execute these commands
end
45

Thank You
&
and you can explore it much more by
practice.

46

Das könnte Ihnen auch gefallen