Sunteți pe pagina 1din 81

MATLAB

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Fundamentals:
An Overview
by

Dr. Amitava Chatterjee


Dr
Electrical Measurement and Instrumentation Laboratory,
Electrical Engineering Department,
J d
Jadavpur
University,
U i
it Kolkata,
K lk t India.
I di
URL: http://www.jaduniv.edu.in/profile.php?uid=322

Short Term Course on


Computational Tools and Open Source Software,
UGC-Academic Staff College, Burdwan University, Burdwan, India.
02/09/2013

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

MATLAB1 A Birds Eye View


Wh t is
What
i MATLAB??

MATLAB is an interactive software package which was developed


to perform numerical calculations on vectors and matrices.
The name MATLAB stands for MATrix LABoratory. MATLAB was
written originally to provide easy access to matrix software
developed by the LINPACK (linear system package)
and EISPACK (Eigen system package) projects matrices.
This software package has been commercially available since 1984
and is now widely accepted as a standard tool at most universities
and industries worldwide.
In recent times, MATLAB has evolved as an excellent tool for
teaching and research.
1MATLAB

is a registered trademark of MathWorks, Inc.

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

MATLAB A Birds Eye View


Wh MATLAB??
Why

MATLAB facilitates easy handling of many of the computations


involved.
In fact, the general feeling is that MATLAB should not be viewed
as another complicated programming language, but more as a
powerful calculator.
MATLAB is an interactive system whose basic data element is
y that does not require
q
dimensioning.
g
an array
It has powerful built-in routines that enable a very wide variety of
computations.
p
applications
pp
are collected in packages
p
g referred to as
Specific
toolbox.
It contains a wide variety of toolboxes which allow it to perform
a wide range of applications from science and engineering. The
toolboxes range from statistics to simulation, to control systems,
to signal processing, to optimization, and so on.

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

MATLAB Fundamentals

The graphical interface to the MATLAB workspace.

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

MATLAB Fundamentals

St
Starting
ti MATLAB

THE COMMAND WINDOW

THE COMMAND HISTORY


THE WORKSPACE

THE CURRENT DIRECTORY

Some Useful Commands to Get Started


>> demo

>> help <command>


>> doc <command>

>> lookfor <command>

MATLAB Fundamentals

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Simple
p Arithmetic Operations
p
Use it as a calculator:
>> 4.23*6.8 + 25/6

You can enter 345 as:


>> 3^45

You can enter a complex number 4 + 6i in MATLAB as:


>> 4 + 6i or >> 4 + 6*i

Write on MATLAB command:


>> x = 4 + 5*7

Change the previous command:


>> x = 4 + 5*7;

Whatisthedifference??

Th iissue th
Then
the command:
d
>> disp(x)

MATLAB Fundamentals

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Vector and Matrix Operations


p

Create a Row Vector (one dimensional array):


>> ur = [[5 7 9 5 18 5];
]

Verify the size of the Row Vector:


>> size(ur)

Create a Column Vector (one dimensional array):


>> uc = [12; 8; 21; 6; 35; 67];

Verify the size of the Column Vector:


>> size(uc)

Perform Matrix Multiplication:


>> um1 = ur*uc

or >> um2 = uc*ur

V if th
Verify
the size
i off the
th Matrix:
M ti
>> size(um1) or >> size(um2)

MATLAB Fundamentals

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Vector and Matrix Operations


Create a 5-by-3 Matrix:

>> m1 = [5 7 9; 5 18 5; 6 23 56; 54 6 70; 21 4 65]

Extract only the first row of Matrix m1:


>> row1 = m1(1,:)

Extract only the second column of Matrix m1:


>> col2 = m1(:,2)

Extract a submatrix of m1 comprising elements from 2nd and


3rd rows and 1st and 2nd columns only:
>> subm1 = m1(2:3,1:2)

Transpose m1:
>> m1t = m1

Create a 5-by-5 Matrix comprising all zeros or all ones:


>> xz = zeros(5,5)

or xo = ones(5,5)

Some
Elementary
Commands
and
S
El
t
C
d for
f Matrices
M t i
d Arrays
A

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Vector and Matrix Operations

MATLAB Fundamentals

Matrices and Arrays: Operations and Manipulation

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Vector and Matrix Operations

MATLAB Fundamentals

Matrices and Arrays: Matrix Analysis and Linear Equations

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Vector and Matrix Operations

MATLAB Fundamentals

MATLAB Fundamentals

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Variables and Numbers

MATLAB does not require any type declarations or dimension statements.


When MATLAB encounters a new variable name, it automatically creates
the variable and allocates the appropriate amount of storage.
In MATLAB, X and x are not the same variable.

MATLAB by default displays only 4 decimals in the result of the


calculations, but it actually performs numerical calculations in double
precision, i.e. in 15 digits.
Issue the command: >> xx = 125.6578

Study how the format command changes the display:


>> format long
g

Then issue the next command: >> xx

: Again revert back the display format : >> format short


Then verify the display of the same variable: >> xx

Study the commands, whos and who. Is there any difference ??

MATLAB Fundamentals

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

The Colon Operator


p

The colon (:) is one of the most important MATLAB operators. It occurs in
several different forms.
If you want to create a row vector of integers with unit
spacing, issue the command: >> var1 = 1:10
If you want a similar array with nonunit spacing
spacing, specify the
increment (either positive or negative). Study the commands:
>> var2 = 3:6:33 or >> var3 = 33:-6:3

The spacing can be floating point numbers too:

>> var4 = 5:0.25:15 or >> var5 = 15:-0.25:5 or >> var6 = 0:pi/6:pi

You can delete a row or a column from a matrix using : and


[ ]. You can delete the second column of a matrix as:
>> Xd = m1

>> Xd(:,2) = [ ]

MATLAB Fundamentals
EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Some Useful
f Commands
C

To clear the Command Window, type clc

To abort a MATLAB computation,


p
, type
yp ctrl-c
To continue a line, type . . .

To delete a variable X, type clear X

To delete all variables from the work space, type clear all

To erase any statement from the command line, type ctrl-u

To turn WARNINGS issued by MATLAB on/off,


on/off type warning on/off

MATLAB Fundamentals

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Save and Load Commands

Workspace variables do not persist upon exiting MATLAB. So


one needs to store data for later use using save command. You
can save the entire workspace as a binary MAT file:
>> save myworkspace.mat

Next time when you re-open MATLAB, you can load the entire
workspace
k
previously
i
l stored:
t
d
>> load myworkspace.mat

Save a variable as a MAT file:


>> save var1

Load this variable from the MAT file:


>> load var1

Similarly save a variable in a file in ASCII format and load a file,


already stored in ASCII format, in MATLAB workspace:
>> save var1.dat var1 -ascii and >> load var1.dat

MATLAB Fundamentals

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Plot Commands (two-dimensional)

Plot the graph of e-0.2xsin(x) over the domain 0 to 6.

>> x = 0:pi/20:6*pi;
p ( , p(
)
( ), ),g
>> plot(x,exp(-0.2*x).*sin(x),'r'),grid
>> axis([0 20 -0.4 0.8])
>> xlabel('time') >> ylabel('amplitude')
>> title(
title('Plot
Plot of the Exponentially Damped Sine Function
Function'))
Plot of the Exponentially Damped Sine Function

0.8

0.6

amplitude

0.4

02
0.2

-0.2

-0.4

10
time

12

14

16

18

20

MATLAB Fundamentals

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Plot Commands (two-dimensional)

Plotting multiple data sets (or functions) in one plot.


>> x = 0:pi/20:6*pi;
>> y1 = 5*cos(x);

>> y
y2 = 0.5*sin(x);
( );

>> y3 = 2.5*cos(x);

>> plot(x,y1,'r--',x,y2,'b-',x,y3,'k:')
>> axis([0 18 -5 5])

>> xlabel('0 \leq x \leq 6\pi')

>> ylabel(
ylabel('Cosine
Cosine functions')
functions )

>> legend('5*cos(x)','0.5*sin(x)','2.5*cos(x)')
>> title('Typical example of multiple plots')

MATLAB Fundamentals

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Plot Commands (two-dimensional)

Plotting multiple data sets (or functions) in one plot.


>> x = 0:pi/20:6*pi; >> y1 = 5*cos(x); >> y2 = 0.5*sin(x);
>> y3 = 2.5*cos(x); ..
>> title('Typical example of multiple plots')
Typical example of multiple plots

5*cos(x)
0 5*sin(x)
0.5
sin(x)
2.5*cos(x)

4
3

Co
osine functionss

2
1
0

-1
-2
-3
-4
-5

8
10
0 x 6

12

14

16

18

MATLAB Fundamentals

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Plot Commands (two-dimensional)

Plotting multiple data sets (or functions) using subplot.


>> x = 0:pi/20:6*pi;
>> y1 = 5*cos(x);

>> y2 = 0.5*sin(x);

>> y3 = 2.5*sqrt(x);

>> y4 = 12*exp(-0.5*x);

>> subplot(2,2,1), plot(x,y1,'r--')

>> subplot(2,2,2),
subplot(2 2 2) plot(x,y2,
plot(x y2 'b-')
b )
>> subplot(2,2,3), plot(x,y3,'k:')

>> subplot(2,2,4), plot(x,y4,'m-.')

>> subplot(2,2,1), axis([0 18 -5 5])

>> subplot(2,2,1), xlabel('0 \leq x \leq 6\pi')


p ( , , ), ylabel('Cosine
y
(
function'))
>> subplot(2,2,1),
>> subplot(2,2,1), legend('5*cos(x)')

>> subplot(2,2,1), title('Typical example of a subplot-1')

MATLAB Fundamentals

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Plot Commands (two-dimensional)

Plotting multiple data sets (or functions) using subplot.


>> x = 0:pi/20:6*pi; >> y1 = 5*cos(x); >> y2 = 0.5*sin(x);
>> y3 = 2.5*sqrt(x); >> y4 = 12*exp(-0.5*x);
>> subplot(2,2,1), plot(x,y1,'r--') >> subplot(2,2,2), plot(x,y2,'b-')
Typical example of a subplot-1

Typical example of a subplot-2

10
15
0 x 6
Typical example of a subplot-3
2.5*sqrt(x)

10
0 x 6

15

-0.5

10

0.5*sin(x)

Sine function

5*cos(x)

-5

Square
e root function

0.5

Expone
ential function

Cosine function
C
n

10
15
0 x 6
Typical example of a subplot-4
12*exp(-0.5*x)

10

10
0 x 6

15

MATLAB Fundamentals

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Plot Commands (three-dimensional)


Plot the Mexican Hat.

>> [x y] = meshgrid(-8:0.5:8);
>> r = sqrt(x.^2
q (
+ y.^2)+eps;
y ) p ;
>> z = sin(r)./r;
>> mesh(z)

0.5

-0.5
40

30

40

30

20

20

10

10

MATLAB Fundamentals

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Plot Commands (three-dimensional)


Plot the Mexican Hat.

>> [x y] = meshgrid(-8:0.5:8);
>> r = sqrt(x.^2
q (
+ y.^2)+eps;
y ) p ;
>> z = sin(r)./r;
>> surf(z), shading flat
or

>> figure, surf(z), shading flat

0.5

-0.5
40

30

40

30

20

20

10

10

Programming in MATLAB

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Scripts
p and Functions

A script file is an external file that contains a sequence of MATLAB


statements. Script files are called M-files and they have .m in their
filename extension
extension.
Scripts do not accept input arguments or return output arguments.
They operate on data in MATLAB workspace.
workspace
M-files can also be written in form of functions that can accept input
arguments and return output arguments
arguments. Internal variables remain
local to the function. If you want more than one function to share a
single copy of a variable, simply declare the variable as global in all
the functions
functions.
It is easy to edit a script file if we wish to remove any error(s) and
then the sequence of statements can be executed whenever desired
by typing the <file name> in MATLAB command line in the command
window.

Programming in MATLAB

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

How to Write and Execute Script


p M-files
f ??

Let us consider that we wish to write the Mexican Hat


program as an M-file.
Use the MATLAB editor to create a new file:
File

New

Script (or M-file)

E t th
Enter
the series
i off MATLAB statements:
t t
t
[x y] = meshgrid(-8:0.5:8);
r = sqrt(x.^2 + y.^2)+eps;
z = sin(r)./r;
mesh(z)

Save the file, for example, as mexhat.m.

Run the file, in the command line, by typing:


>> mexhat

When the execution completes, the variables remain in the


workspace. Verify this by typing: >> whos

Programming in MATLAB

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

M-file
f Functions

Let us consider that we wish to write an M-file function that


will calculate squares and cubes of the sum of two numbers.
Use the MATLAB editor to create a new function:
File

New

Function

E t th
Enter
the series
i off MATLAB statements:
t t
t

function [outp1 outp2] = sqre_cube(inp1,inp2)


outp1 = inp1^2 + inp2^2+ 2*inp1*inp2;
outp2 = inp1
inp1^3
3 + inp2
inp2^3
3+3
3*inp1*inp2*(inp1
inp1 inp2 (inp1 + inp2);
end

Save the file as sqre_cube.m.

Run the file, in the command line, by typing:


>> [x y] = sqre_cube(5,4)

R
Run
th
the fil
file again,
i iin th
the command
d li
line, b
by ttyping:
i
>> [x y] = sqre_cube(6,7)

Programming in MATLAB

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Control Flow and Operators


p

MATLAB has several flow control constructs:


The if end structure

The if else end structure

The if elseif else end structure


The for end loop

The while end loop

The break
Th
b
k statement
t t
t ((can b
be used
d tto tterminate
i t a while
hil loop
l
or to exit a for loop).
The continue statement (can be used to exit a for loop to
pass immediately to the next iteration of the loop, skipping
the remaining statements in the loop).
The return
return statement, the switch
switch statement etc.

One can study the syntax by typing the help command (e.g.
help for)

Programming in MATLAB

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Control Flow and Operators


p
IF

Conditionally execute statements.

The general form of the if statement is:


IF expression

statements

ELSEIF expression
statements

ELSE

statements

END
FOR

Repeat statements a specific number of times.

The general form of a for statement is:

FOR variable = expression, statement, ..., statement END

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Control Flow and Operators

Programming in MATLAB

Arithmetic Operators and Special Characters

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Control Flow and Operators


p

Programming in MATLAB

Array Operators

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Control Flow and Operators


p

Programming in MATLAB

Relational and Logical Operators

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Control Flow and Operators


p

Programming in MATLAB

Operator Precedence

Programming in MATLAB

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

fprintf
fp f Statement for
f Displaying
p y g Output
p on the Screen
In MATLAB, the command fprintf can be utilized both for
displaying the output on the screen as well as for writing the
output
t t to
t a file.
fil

A script M-file
M file that executes a sequence of statements and
displays the result on the screen:
x_array
x
array = [5.5
[5 5 8.7
8 7 12
12.4
4 34
34.7
7 67
67.9
9 -3.2
-3 2 6
6.8];
8];
fprintf('\nmaximum value = %8.4f', max(x_array));
fprintf('\nminimum value = %8.4f', min(x_array));
fprintf('\naverage value = %8.4f', mean(x_array));
fprintf('\n');

One can easily


O
il change
h
the
th format
f
t off display
di l
if he/she
h / h wishes
i h
to.

Programming in MATLAB

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

fprintf
fp f Statement for
f Savingg Output
p to a File

One can save the results of some computation to a file in text


format, using the following steps:
Open a file using fopen

Write the output using fprintf


Cl
Close
the
th file
fil using
i
f l
fclose

Change
g the previous
p
script
p M-file as shown below and
execute it in the command prompt:
x_array = [5.5 8.7 12.4 34.7 67.9 -3.2 6.8];
outp = fopen('array.txt','wt');
fprintf(outp,'\nmaximum value = %8.4f', max(x_array));
fprintf(outp,'\nminimum value = %8.4f', min(x_array));
fprintf(outp,'\naverage
fprintf(outp,
\naverage value = %8.4f
%8.4f',, mean(x
mean(x_array));
array));
fprintf(outp,'\n');
fclose(outp);

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Programming in MATLAB

Some Useful Workspace and File Commands

Some Predefined Variables and Math Constants

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Programming in MATLAB

MATLAB/Simulink

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Introduction to Simulinkk

Simulink is a user-friendly environment created in MATLAB


platform, essentially for the purpose of graphical, modelbased simulation of dynamic systems.
The key features of Simulink are:
Graphical editor for building and managing hierarchical block
diagrams
Libraries of predefined blocks for modeling continuous-time
and discrete-time systems
g
with fixed-step
p and variable-step
p ODE
Simulation engine
solvers
Scopes and data displays for viewing simulation results
Project and data management tools for managing model files
and
d data
d t
Model analysis tools for refining model architecture and
increasing simulation speed
MATLAB Function block for importing MATLAB algorithms into
models
.

MATLAB/Simulink

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

How to Start Simulinkk

Type simulink in command prompt.

or

click here
to start
simulink

choose
current
directory

MATLAB/Simulink

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Simulinkk Libraryy Browser

Library
y
Browser
gives
access
to
various standard or
additional
blocks
that can be utilized
to build complicated
models.

MATLAB/Simulink

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Creatingg a New Simulinkk Model

From the Simulink Library Browser Menu, select:


File
New
Model. An empty model opens in the
Simulink Editor.

MATLAB/Simulink

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Creating a New Simulink Model

Edit the blank model by dragging and dropping blocks from the library
browser, adding appropriate sources and sinks, and making suitable
connections to build your own model.

drag and drop from the


Sources in the Library

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Creating a New Simulink Model

MATLAB/Simulink

double click the sine block to


change its parameter values

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Creating a New Simulink Model

MATLAB/Simulink

add another block in your model

source 1

source 2

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Creating a New Simulink Model

MATLAB/Simulink

sink
(oscilloscope)

connector

MUX

The Complete Model

set the configuration parameters before running the simulation

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Creating a New Simulink Model

MATLAB/Simulink

MATLAB/Simulink

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Creating a New Simulink Model


click here to start simulation

total simulation time

autoscale

double click on the


bl k
block
t
to
scope
display
output
waveform

scope
parameters

MATLAB/Simulink

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Saving the Simulink Model

the
simulink
model
created is saved, for
future use, as sim1.mdl
in
your
chosen
directory

Reusing an Existing Simulink Model

double
click
sim1.mdl
to
open
p
this
simulink model

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

MATLAB/Simulink

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Creatingg GUIs
G

In MATLAB, one can use GUIDE, the MATLAB Graphical User


Interface Development Environment to create GUIs.
A GUI is essentially a graphical display which allows a user to
pictorially interact with a program. It comprises the following
basic elements:

Components Push buttons, sliders, radio buttons, check


boxes etc., which enable the user to interact with a
program.

Figure A graphical window which houses all these


components.

Callbacks Code modules that are used to enable user


interaction with the program.

To write a GUI, type in the command prompt:


>> guide

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Creatingg GUIs
G

Graphical User Interfaces (GUIs)

Select this and click OK to create a blank, new GUI.

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

The Layout Editor

The blank GUI is displayed in the Layout Editor, which acts as the
control panel for all of the GUI Tools.

Layout Area

Component
Palette

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

The Layout Editor

Lay out your GUI, by dragging components like Slider, Edit Text, Check
Box, Push Button etc. from the Component Palette into the Layout Area.

Layout Area

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

The Layout Editor

Lay out your GUI, by dragging components like Slider, Edit Text, Check
Box, Push Button etc. from the Component Palette into the Layout Area.

Layout Area

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

The Layout Editor

Lay out your GUI, by dragging components like Slider, Edit Text, Check
Box, Push Button etc. from the Component Palette into the Layout Area.

Layout Area

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

An Example

Design a GUI in MATLAB to determine square root of a number, using


Newtons method. In the GUI, enter the number to be square-rooted, press the
Start button, and the GUI should show the result in an iterative manner.

Right click anywhere in


the Layout Area to reach
the
figures
Property
Inspector.

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

An Example

Click the figure


s
figures
Property
Inspector and set
the figures Name
property
to
Newton.

An Example

Make
the
GUI
Resizable.
Click
Tools
GUI
Options.

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Graphical User Interfaces (GUIs)

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

An Example

Under
Proportional
when the window
is resized, the
GUI components
also get resized.

Place
a
Static Text
control.
Right click
to reach its
Property
Inspector.
p

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

An Example

Graphical User Interfaces (GUIs)

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

An Example

Change
its
String property
to Enter the
number
to
square
root
and
Press
Start. The Tag
of
this
component is
text1.

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

An Example

Add
an
Edit
Text
control. Set
its
String
property to
blank. The
Tag of this
component
is edit1.

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

An Example

Next, activate the figure using Editor in View option and saving it as
Newton.fig.

An Example

This automatically opens the application M-file Newton.m.

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Graphical User Interfaces (GUIs)

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

An Example

Add
another
Static
Text
control. Set its
String property
to Output and
its
HorizontalAlig
nment to left.
The Tag of this
component is
text2.

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

An Example

Add a Push
Button. Set its
String property
to Start. The
Tag
of
this
component is
pushbutton1.

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

An Example

Now, activate the figure again using Editor in View option, to update
Newton.m. Then the pushbutton1_Callback sub function is programmed as:

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

An Example

Save your work and activate the figure again to test it. Then Run the figure.

Graphical User Interfaces (GUIs)


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

An Example

Save your work and activate the figure again to test it. Then Run the figure.

An Example

The GUI Newton.fig shows the following result.

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Graphical User Interfaces (GUIs)

MATLAB in Image Processing


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Useful Images Processing Commands

Read an image using imread command in command line:


>> img1 = imread('pout.tif');

Check the row and column dimensions of the image:


>> [img_rows, img_cols] = size(img1)

Display the image using imshow command in command line:


>> imshow(img1)

pout.tif

MATLAB in Image Processing


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Useful Images Processing Commands

Images are written to disc using the function imwrite:


>> imwrite(img1, 'pout_test.tif');

Obtain an image file details using the command imfinfo:


>> imfinfo pout_test.tif

Create a histogram to view the intensity distribution:


>> figure, imhist(img1)

Histogram
of pout.tif

MATLAB in Image Processing


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Useful Images Processing Commands

Improve the contrast of this image using the function histeq which performs
histogram equalization:
g = histeq(img1);
( g )
>> img2

Display the histogram equalized image in a new figure window:


>> figure, imshow(img2)

Histogram equalized
pout.tif

MATLAB in Image Processing


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Useful Images Processing Commands

View the intensity distribution of the histogram equalized version of pout.tif:


>> figure, imhist(img2)

Histogram of
histogram
equalized
version
of
pout.tif

MATLAB in Image Processing


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Useful Images Processing Commands

Read a new grayscale image moon.tif and display this image:


>> img_moon = imread('moon.tif');
>> imshow(img
imshow(img_moon)
moon)

Use the command imadjust to perform intensity adjustment for moon.tif:


>> img_moon1 = imadjust(img_moon, [0.5 0.75], [0 1], 2);
>> figure,
fi
iimshow(img_moon1)
h (i
1)

moon.tif

Intensity
adjusted
version of
moon.tif

MATLAB in Image Processing


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Useful Images Processing Commands

To perform image segmentation of moon.tif, use function graythresh that


automatically computes an appropriate threshold:
>> thr_intensity = graythresh(img_moon);

Use the command im2bw to convert grayscale moon.tif into a binary image using
thresholding and display the thresholded image:
>> img_moon_bw = im2bw(img_moon,thr_intensity);
>> figure, imshow(img_moon_bw)

moon.tif

Automatic
thresholding
based
segmented
moon.tif

MATLAB in Image Processing


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Useful Images Processing Commands


An example of linear spatial filtering:
>> img_pepper = imread('peppers.png');
>> figure, imshow(img_pepper)

MATLAB in Image Processing


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Useful Images Processing Commands


An example of linear spatial filtering:
>> img_pepper = imread('peppers.png');
>> figure, imshow(img_pepper)
>> img_pepper_gr
= rgb2gray(img_pepper);
i
b2
(i
)
>> figure, imshow(img_pepper_gr)

MATLAB in Image Processing


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Useful Images Processing Commands

An example of linear spatial filtering:


>> img_pepper = imread('peppers.png');
>> figure, imshow(img_pepper)
>> img_pepper_gr
= rgb2gray(img_pepper);
i
b2
(i
)
>> figure, imshow(img_pepper_gr)
>> img_pepper_grn = imnoise(img_pepper_gr,'gaussian',0,0.01);
imshow(img_pepper_grn)
pepper grn)
>> figure, imshow(img

MATLAB in Image Processing


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Useful Images Processing Commands

An example of linear spatial filtering:

>> img_pepper_grn = imnoise(img_pepper_gr,'gaussian',0,0.01);


>> figure,
imshow(img_pepper_grn)
fi
i h (i
)
>> H_mask1 = fspecial('average');
>> H_mask2 = fspecial('gaussian');
img pepper grf1 = imfilter(img_pepper_grn,H_mask1,
imfilter(img pepper grn,H mask1,'replicate');
replicate );
>> img_pepper_grf1
>> img_pepper_grf2 = imfilter(img_pepper_grn,H_mask2,'replicate');
>> figure, imshow(img_pepper_grf1)

MATLAB in Image Processing


EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

Useful Images Processing Commands

An example of linear spatial filtering:

>> img_pepper_grn = imnoise(img_pepper_gr,'gaussian',0,0.01);


>> figure,
imshow(img_pepper_grn)
fi
i h (i
)
>> H_mask1 = fspecial('average');
>> H_mask2 = fspecial('gaussian');
img pepper grf1 = imfilter(img_pepper_grn,H_mask1,
imfilter(img pepper grn,H mask1,'replicate');
replicate );
>> img_pepper_grf1
>> img_pepper_grf2 = imfilter(img_pepper_grn,H_mask2,'replicate');
>> figure, imshow(img_pepper_grf1)
>> figure, imshow(img_pepper_grf2)

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

References
L
i
MATLAB 7,
7 Release14,
R l
14 MATLAB & Si
li k
Learning
Simulink
Student Version, The MathWorks, Inc., 2005.
Ed Overman,
Overman A MATLAB Tutorial,
Tutorial Department of
Mathematics, The Ohio State University, 2012.

David Houcque, Introduction to MATLAB for Engineering


Students, Northwestern University, 2005.
Simulink Getting
g Started Guide,, R2013a,, MATLAB &
Simulink, The MathWorks, Inc., 2013.
MATLAB/Simulink Tutorial, ECEN 2060, Colorado
University, 2008.

R. C. Gonzalez, R. E. Woods, and S. L. Eddins, Digital Image


P
Processing
i
using
i
MATLAB,
MATLAB Pearson
P
Ed
Education,
ti
2005
2005.

EL
JA EC
D TR
AV I A
PU CA MIT
R L E AV
U N A
N G C
IV IN H
ER E A
SI ER TTE
TY IN R
, K G D JE
O E E
LK PA
AT R
A, TM
I N EN
D T
IA

S-ar putea să vă placă și