Vous êtes sur la page 1sur 32

Introduction to MATLAB

Ashraf AL-Rimawi MATLAB 12-12-2008


What Is MATLAB

• MATLAB = MATrix LABoratory


• Programming language for technical computation,
engineering problems, science problems, systems
simulation and more
• Toolboxes - prepared sets of integrated tools for
work in a specific area (Control, PDE, Symbolic ...)
• High capability for data visualization

Ashraf AL-Rimawi MATLAB 12-12-2008


MATLAB Desktop
Run
New M-file Get Help Enter Command
SIMULINK
Launch Pad

Use previously run commands View workspace or


current directory

Ashraf AL-Rimawi MATLAB 12-12-2008


Using Help

Two ways of getting help


• Online help, supply you with extensive capability of
contents, search and index. Try the help icon ...

• Help on specific command, help command


– >> help elfun % elementary math functions
– >> help sqrt % square root
– >> help elmat % elementary matrices and matrix
manipulation

Ashraf AL-Rimawi MATLAB 12-12-2008


Some Useful Functions

• whos: List current variables


• clear: Clear variables and functions from memory
• cd: Change current working directory
• dir: List files in directory
• echo: Echo commands in M-files
• format: Set output format

• Use help to find out more about these functions

Ashraf AL-Rimawi MATLAB 12-12-2008


Calculation at the command line
MATLAB as a calculator Assigning Variables Semicolon
>>
>>-5/(4.8+5.32)^2
-5/(4.8+5.32)^2 >>
>>aa==2;2; suppresses
ans
ans== >>
>>bb==5;5;
screen output
-0.0488
-0.0488 >>
>>a^b
a^b
>>
>>(3+4i)*(3-4i)
(3+4i)*(3-4i) ans
ans== Results is
ans
ans== 32
32
assigned to
25
25 >>
>>xx==5/2*pi;
5/2*pi;
“ans” if name
>> is not specified
>>cos(pi/2)
cos(pi/2) >>
>>yy==sin(x);
sin(x);
ans
ans== yy ==
6.1232e-017
6.1232e-017 11
>>
>>exp(cos(0.3))
exp(cos(0.3)) >>
>>zz==asin(y);
asin(y);
Parentheses
ans
ans== zz== for function
2.5995 1.5708 input
2.5995 1.5708

Check Workspace for the stored variables ?


Ashraf AL-Rimawi MATLAB 12-12-2008
Matrices

Ashraf AL-Rimawi MATLAB 12-12-2008


Entering Matrices

• Enter Matrix by:


– Enter an explicit list of elements
– Load matrices from external data files
– Create matrices with functions in M-files

• You have to follow a few basic conventions


– Separate the elements of a row with blanks
– Use a semicolon, ; ,to indicate the end of each row
– Surround the entire list of elements with square brackets, [ ]

Ashraf AL-Rimawi MATLAB 12-12-2008


Entering Matrices Explicit List
cont’
A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
MATLAB displays Try the following commands
16 33 22 13
16 13 >>mul
>> mul==A*A
A*A % %A^2
A^2
>>sqr
>> sqr ==A.*A
A.*A% %A.^2
A.^2
55 10
10 11
11 88
>>sum(A)
sum(A)
>>
99 66 77 12
12 >>BB==A’
>> A’
>>CC==inv(A)
>> inv(A)
44 15
15 14
14 11

Ashraf AL-Rimawi MATLAB 12-12-2008


Entering Matrices
Loading Data from a file
• Data can be loaded into MATLAB from a text file:
Simply type load myfile.txt or myfile.dat. Then the
data will be stored in a matrix called file.
• You can access the matrix from the workspace or
– >> whos % shows the matrix file properties
– >> myfile % data will be displayed
• NOTE: The directory MATLAB looks in for data files
is by default the directory you were in when you
started the program. To look elsewhere, you must
specify the directory!

Ashraf AL-Rimawi MATLAB 12-12-2008


Entering Matrices
Loading Data from a file cont’
• Example
Open note pad, enter some numbers separated by
blank then save as file.txt or .dat
10 15
10 15 20
20 33

22 100
100 55 44
11 22 33 44

• Go to MATLAB type: load file.txt


– Type whos, the matrix and its size will be displayed

Ashraf AL-Rimawi MATLAB 12-12-2008


Matrix Representation in
MATLAB

Ashraf AL-Rimawi MATLAB 12-12-2008


Numerical Array Concatenation
>>aa==[1
>> [12;2;334]
4]

aa==
11 22
33 44
>>cat_a
>> cat_a==[a[a 2*a;
2*a;3*a
3*a 4*a;
4*a;5*a
5*a 6*a]
6*a]
cat_a==
cat_a
11 22 22 44
33 44 66 88
33 66 44 88
99 12
12 12 12 1616 4*a
55 10
10 66 12 12
15 20
15 20 18 18 2424

Ashraf AL-Rimawi MATLAB 12-12-2008


Deleting Rows and Columns

>>AA==[1
>> [1559;4
9;4332.5;
2.5;0.1
0.110
103i+1]
3i+1]
AA==
1.0000
1.0000 5.0000
5.0000 9.0000
9.0000
4.0000
4.0000 3.0000
3.0000 2.5000
2.5000
0.1000
0.1000 10.0000
10.0000 1.0000++3.0000i
1.0000 3.0000i
>>A(:,2)=[]
>> A(:,2)=[]
Deleting Col. 2
A
A= =
1.0000
1.0000 9.0000
9.0000
4.0000
4.0000 2.5000
2.5000
0.1000
0.1000 1.0000++3.0000i
1.0000 3.0000i
>>A(2,2)=[]
>> A(2,2)=[]
??? Indexed
??? Indexedempty
emptymatrix
matrixassignment
assignmentisisnot
notallowed.
allowed.

Ashraf AL-Rimawi MATLAB 12-12-2008


Array Indexing

Ashraf AL-Rimawi MATLAB 12-12-2008


Matrix Manipulation Functions

• zeros: Create an array of all zeros


• ones : Create an array of all ones
• eye: Identity Matrix
• rand: Uniformly distributed random numbers
• diag: Diagonal matrices and diagonal of a matrix
• size: Return array dimensions
• fliplr: Flip matrices left-right
• flipud: Flip matrices up and down
• repmat: Replicate and tile a matrix

Ashraf AL-Rimawi MATLAB 12-12-2008


Matrix Manipulation Functions
Cont’
• transpose (’): Transpose matrix
• rot90: rotate matrix 90
• tril: Lower triangular part of a matrix
• triu: Upper triangular part of a matrix
• cross: Vector cross product
• dot: Vector dot product
• det: Matrix determinant
• inv: Matrix inverse
• eig: Evaluate eigen values and eigenvectors
• rank: Rank of matrix
Try these commands, if you stuck use help

Ashraf AL-Rimawi MATLAB 12-12-2008


Operators

Operator Function
Operator Function

++ Addition
Addition
** or
or .*.* Multiplication
Multiplication
// or
or ././ Division
Division
^^ or
or .^.^ Power (x^2
Power (x^2 == x*x)
x*x)
‘‘ Transpose
Transpose

Ashraf AL-Rimawi MATLAB 12-12-2008


Operators Cont’

In order to get element by element multiplication,


division or exponents, use the .*, ./,.^ operators.
>> a = [1 2 3; 1 2 3];
>> b = [4 5 6; 4 5 6];
>> a.*b;
% each element in the Mat. a is multiplied by the
corresponding element in Mat. b

Ashraf AL-Rimawi MATLAB 12-12-2008


Graphics

Ashraf AL-Rimawi MATLAB 12-12-2008


Creating a Plot

The plot function has different forms, depending


on the input arguments. If y is a vector, plot(y)
produces a linear graph of the elements of y
versus the index of the elements of y. If you
specify two vectors as arguments, plot(x,y)
produces a graph of y versus x.

Ashraf AL-Rimawi MATLAB 12-12-2008


Creating a Plot Cont’

>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y) 1

It will produce this figure 0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7

Ashraf AL-Rimawi MATLAB 12-12-2008


Creating a Plot Cont’

Now label the axes, add a title and show the grid
>>xlabel('x = 0:2\pi')
Plot of the Sine Function
>>ylabel('Sine of x') 1

>>title('Plot of the Sine Function','FontSize',12)


0.8

>>grid
0.6

0.4

0.2
Sine of x

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7
x = 0:2

Ashraf AL-Rimawi MATLAB 12-12-2008


Multiple Plots in One Graph

>> y = sin(x) % x defined from the previous slide


>> y2 = sin(x-.25);
>>y3 = sin(x-.5);
>>plot(x,y,x,y2,x,y3)
The legend command provides an easy way to identify
the individual plots.
>> legend('sin(x)','sin(x - 0.25)','sin(x - 0.5)');

Ashraf AL-Rimawi MATLAB 12-12-2008


1
sin(x)
0.8 sin(x - 0.25)
sin(x - 0.5)
0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7

Ashraf AL-Rimawi MATLAB 12-12-2008


Specifying Line Styles and Colors

Colors Symbols Lines


y – yellow . – point - – solid line
m – mag o – circle : – dots
c – cyan x – xmark -. – line dot
r – red + – plus - - – dashes
g – green * – star
b – blue s – square
w – white d – diamond
k – black v – triangle down ^ – triangle up < – left
< – right
p – pentagram
h – hexagram

Ashraf AL-Rimawi MATLAB 12-12-2008


Specifying Line Styles and Colors
Cont’ 1

0.8

•• Example
Example 0.6

>>x1
>> x1==0:pi/100:2*pi;
0:pi/100:2*pi; 0.4

0.2

>>x2
>> x2==0:pi/10:2*pi;
0:pi/10:2*pi; 0

>>plot(x1,sin(x1),'r:',x2,sin(x2),'r+');
plot(x1,sin(x1),'r:',x2,sin(x2),'r+');
-0.2

>> -0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7

First Line Second Line 1

0.8

Red, Dots Red, Plus 0.6

0.4

0.2

Plot figure two !! -0.2

-0.4

Use cos, change -0.6

-0.8

the period -1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

Ashraf AL-Rimawi MATLAB 12-12-2008


Adding Plots to an Existing Graph

• This can be done using command hold


•• Example
Example 1

>>x1
>> x1==0:pi/100:2*pi;
0:pi/100:2*pi; 0.8

>>x2
>> x2==0:pi/10:2*pi;
0:pi/10:2*pi; 0.6

>>plot(x1,sin(x1),'r:',x2,sin(x2),'bd')
>> plot(x1,sin(x1),'r:',x2,sin(x2),'bd') 0.4

>>grid
>> grid 0.2

>>x3
>> x3==0:pi/100:2*pi;
0:pi/100:2*pi; 0

-0.2
>>x4
>> x4==0:pi/10:2*pi;
0:pi/10:2*pi;
-0.4
>>hold
>> hold%%current
currentplot
plotheld
held
-0.6
>>plot(x1,cos(x1),'r:',x2,cos(x2),'bd')
>> plot(x1,cos(x1),'r:',x2,cos(x2),'bd') -0.8

>>hold
>> holdoff
off -1
0 1 2 3 4 5 6 7

Holds
Release
thethe
current
plot plot
Ashraf AL-Rimawi MATLAB 12-12-2008
Multiple Plots in One Figure

• The subplot command enables you to display


multiple plots in the same window
Syntax: subplot(rows,cols,index)
subplot(rows,cols,index)

•• Example
Example
>>subplot(2,2,1);plot(x1,cos(x1),'r:',x2,cos(x2),'bd')
>> subplot(2,2,1);plot(x1,cos(x1),'r:',x2,cos(x2),'bd')
>>subplot(2,2,2);plot(x1,sin(x1),'r:',x2,sin(x2),'bd')
>> subplot(2,2,2);plot(x1,sin(x1),'r:',x2,sin(x2),'bd')
>>subplot(2,2,3);plot(x1,cosh(x1),'r:',x2,cosh(x2),'bd')
>> subplot(2,2,3);plot(x1,cosh(x1),'r:',x2,cosh(x2),'bd')
>>subplot(2,2,4);plot(x1,sinh(x1),'r:',x2,sinh(x2),'bd')
>> subplot(2,2,4);plot(x1,sinh(x1),'r:',x2,sinh(x2),'bd')

Try This !!

Ashraf AL-Rimawi MATLAB 12-12-2008


Multiple Plots in One Figure
Cont’
subplot(2,2,1);plot
subplot(2,2,1); plot......

subplot(2,2,2);plot
subplot(2,2,2); plot......

subplot(2,2,3);plot
subplot(2,2,3); plot......

subplot(2,2,4);plot
subplot(2,2,4); plot......

Ashraf AL-Rimawi MATLAB 12-12-2008


Saving A figure

File > export > choose the suitable format JPEG, TIFF, bmp ...

Ashraf AL-Rimawi MATLAB 12-12-2008


Printing a figure with the command
print
The command print can be used to save your figure in
many formats in a file.
Syntax: print
print-device
-device-options
-optionsfilename
filename

Encapsulatedcolor
Encapsulated color
•• Examples
Examples Postscriptstiff
Postscripts tiff
print-depsc
print -depsc-tiff
-tifffig.eps
fig.eps imagein
image infig.eps
fig.eps
print-dtiff
print -dtiff-r200
-r200fig.tiff
fig.tiff
Savethe
Save thefigure
figureasas
Tiffwith
Tiff withresolution
resolution
200dpi
200 dpiin
infig.tiff
fig.tiff
Use help to see other types, Try One !!
Ashraf AL-Rimawi MATLAB 12-12-2008

Vous aimerez peut-être aussi