SlideShare ist ein Scribd-Unternehmen logo
1 von 45
Contents
Built in functions
Getting Started
Vectors and Matrices
Introduction
Simulink
Modeling examples
MATLAB
SIMULINK
M–files : script and functions
3
Command
Window
Workspace /
Current Directory
Command
History
Explore the Matlab
Desktop
 Don’t have to declare type
 Don’t even have to initialise
 Just assign in command window
>>
>> a=12; % variable a is assigned 12
4
Matlab
prompt
assign
operator
suppress
command
output
comment
operator
Matlab
prompt
assign
operator
suppress
command
output
comment
operator
Try the same line without the
semicolon and comments
 View variable contents by simply typing the
variable name at the command prompt
>> a
a =
12
>>
>> a*2
a =
24
>>
5
 The workspace is Matlab’s memory
 Can manipulate variables stored in the
workspace
>> b=10;
>> c=a+b
c =
22
>>
6
 Display contents of workspace
>> whos
Name Size Bytes Class
a 1x1 8 double array
b 1x1 8 double array
c 1x1 8 double array
Grand total is 3 elements using 24 bytes
>>
 Delete variable(s) from workspace
>> clear a b; % delete a and b from workspace
>> whos
>> clear all; % delete all variables from workspace
>> whos
7
 Don’t need to initialise type, or dimensions
>>A = [3 2 1; 5 1 0; 2 1 7]
A =
3 2 1
5 1 0
2 1 7
>>
8
square brackets to define matrices
semicolon for next row in matrix
 Access elements of a matrix
>>A(1,2)
ans=
2
 Remember Matrix(row, column)
 Naming convention Matrix variables start with a capital
letter while vectors or scalar variables start with a
simple letter
9
A =
3 2 1
5 1 0
2 1 7
indices of matrix element(s)
 VERY important operator in Matlab
 Means ‘to’
>> 1:10
ans =
1 2 3 4 5 6 7 8 9 10
>> 1:2:10
ans =
1 3 5 7 9
10
Try the following
>> x=0:pi/12:2*pi;
>> y=sin(x)
>>A(3,2:3)
ans =
1 7
>>A(:,2)
ans =
2
1
1
11
A =
3 2 1
5 1 0
2 1 7
What’ll happen if you type A(:,:) ?
>> A ' % transpose
>> B*A % matrix multiplication
>> B.*A % element by element
multiplication
>> B/A % matrix division
>> B./A % element by element division
>> [B A] % Join matrices (horizontally)
>> [B; A] % Join matrices (vertically)
12
A =
3 2 1
5 1 0
2 1 7
B =
1 3 1
4 9 5
2 7 2
Create matrices A and B and try out the matrix operators in this slide
Enter matrix B
into the Matlab
workspace
 Arithmetic operations – Matrices
Performing operations to every entry in a matrix
Add and subtract
>>> A=[1 2 3;4 5 6;7 8
9]
A =
1 2 3
4 5 6
7 8 9
>>>
>>> A+3
ans =
4 5 6
7 8 9
10 11 12
>>> A-2
ans =
-1 0 1
2 3 4
5 6 7
 Arithmetic operations – Matrices
Performing operations to every entry in a matrix
Multiply and divide
>>> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
>>>
>>> A*2
ans =
2 4 6
8 10 12
14 16 18
>>> A/3
ans =
0.3333 0.6667 1.0000
1.3333 1.6667 2.0000
2.3333 2.6667 3.0000
 Arithmetic operations – Matrices
Performing operations to every entry in a matrix
Power
>>> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
>>>
A^2 = A * A
To square every element in A, use
the element–wise operator .^
>>> A.^2
ans =
1 4 9
16 25 36
49 64 81
>>> A^2
ans =
30 36 42
66 81 96
102 126 150
 Arithmetic operations – Matrices
Performing operations between matrices
>>> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
>>> B=[1 1 1;2 2 2;3 3 3]
B =
1 1 1
2 2 2
3 3 3
A*B 



















3
3
3
2
2
2
1
1
1
9
8
7
6
5
4
3
2
1
A.*B










3
x
9
3
x
8
3
x
7
2
x
6
2
x
5
2
x
4
1
x
3
1
x
2
1
x
1










27
24
21
12
10
8
3
2
1
=
=










50
50
50
32
32
32
14
14
14
 Arithmetic operations – Matrices
Performing operations between matrices
A/B
A./B










0000
.
3
6667
.
2
3333
.
2
0000
.
3
5000
.
2
0000
.
2
0000
.
3
0000
.
2
0000
.
1
=
? (matrices
singular)










3
/
9
3
/
8
3
/
7
2
/
6
2
/
5
2
/
4
1
/
3
1
/
2
1
/
1
 Arithmetic operations – Matrices
Performing operations between matrices
A^B
A.^B










729
512
343
36
25
16
3
2
1
=
??? Error using ==> ^
At least one operand must be scalar










3
3
3
2
2
2
1
1
1
9
8
7
6
5
4
3
2
1
Scalar functions – used for scalars and operate
element-wise when applied to a matrix or vector
e.g. sin cos tan atan asin log
abs angle sqrt round floor
At any time you can use the command
help to get help
e.g. >>>help sin
 Matlab editor
 Use scripts to execute a series of Matlab commands
20
Matlab Desktop
Press to create new m-
file in the matlab editor
 Scripts will manipulate and
store variables and matrices
in the Matlab Workspace
(memory).
 They can be called from the
Matlab command line by
typing the (case sensitive!)
filename of the script file.
>> myscript
 Scripts can be opened in the
editor by the following
>> open myscript
21
Highlight a few lines of your
script by left- clicking and
dragging the mouse over the
lines. Right-click the
highlighted lines and select
Evaluate Selection.
Will be slightly
different in
Linux
>> I=iterate(5)
I =
1 4 9 16 25
22
output
input
function name
for statement block
function keyword
help lines for function
Access the comments of
your Matlab functions
>> help iterate Make sure you save changes to the
m-file before you call the function!
>> [i j]=sort2(2,4)
i =
4
j =
2
>>
23
Functions can have many
outputs contained in a matrix
Remember to use the
Matlab help command for
syntax
>> help if
if statement
block
24
Method is linear
>>
i =
4
i =
16
i =
256
While statement block Switch statement block
Without ; to
print output
>> figure % create new figure
>> t=0:pi/12:8*pi;
>> y=cos(t);
>> plot(t,y,‘b.-')
25
Investigate the function
>> y=A*cos(w*t+phi);
for different values of phi (eg: 0, pi/4, pi/3,
pi/2), w (eg: 1, 2, 3, 4) and A (eg: 1, 0.5, 2). Use
the hold on Matlab command to display your
plots in the same figure. Remember to type
hold off to go back to normal plotting mode.
Try using different plot styles (help plot)
A = amplitude
phi = phase
w = angular frequency = 2*pi*frequency
Plot style
Data visualisation – plotting graphs
Example on plot – 2 dimensional plot
Example on plot – 2 dimensional plot
>>> x=linspace(0,(2*pi),100);
>>> y1=sin(x);
>>> y2=cos(x);
>>> plot(x,y1,'r-')
>>> hold
Current plot held
>>> plot(x,y2,'g--')
>>>
Add title, labels and legend
title xlabel ylabel legend
Use ‘copy’ and ‘paste’ to add to your window–
based document, e.g. MSword
Data visualisation – plotting graphs
0 1 2 3 4 5 6 7
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
angular frequency (rad/s)
y1
and
y2
Example on plot
sin(x)
cos(x)
Example on plot – 2 dimensional plot
eg1_plt.m
Example – RLC circuit
Exercise 1:
Write an m–file to plot Z, Xc and XLversus
frequency for R =10, C = 100 uF, L = 0.01 H.
+
V
–
R = 10 C
L
Example – RLC circuit
Total impedance is given by:
L
C X
X 
When
LC
1
o 

Example – RLC circuit
For a given values of C and L, plot the following versus the frequency
a) the total impedance ,
b) Xc and XL
c) phase angle of the total impedance
for 100 <  < 2000
Example – RLC circuit
+
V
–
R = 10 C
L
< ¦ save !
> rand load guide
~= zeros … get
== min ' ' set
>= max { }
<= repmat try
& axis catch
32
Remember to use the Matlab help command if you get stuck
string
cell
error handling
Operating
system
command
Graphical
user interface
Continue in next line
Every function must begin with a header:
function output=function_name(inputs)
Output variable
Must match the file
name
input variable
Used to model, analyze and simulate dynamic
systems using block diagrams.
Provides a graphical user interface for constructing
block diagram of a system – therefore is easy to use.
However modeling a system is not necessarily easy !
Model – simplified representation of a system – e.g. using
mathematical equation
We simulate a model to study the behavior of a system –
need to verify that our model is correct – expect results
Knowing how to use Simulink or MATLAB does not
mean that you know how to model a system
Problem: We need to simulate the resonant circuit
and display the current waveform as we change the
frequency dynamically.
+
v(t) = 5 sin t
–
i 10  100 uF
0.01 H
Varies  from 0
to 2000 rad/s
Observe the current. What do we expect ?
The amplitude of the current waveform will become
maximum at resonant frequency, i.e. at  = 1000 rad/s
How to model our resonant circuit ?
+
v(t) = 5 sin t
–
i 10  100 uF
0.01 H



 idt
C
1
dt
di
L
iR
v
Writing KVL around the loop,
LC
i
dt
i
d
L
R
dt
di
dt
dv
L
1
2
2



Differentiate wrt time and re-arrange:
Taking Laplace transform:
LC
I
I
s
sI
L
R
L
sV 2












LC
1
s
L
R
s
I
L
sV 2
Thus the current can be obtained from the voltage:













LC
1
s
L
R
s
)
L
/
1
(
s
V
I
2
LC
1
s
L
R
s
)
L
/
1
(
s
2


V I
Start Simulink by typing simulink at Matlab prompt
Simulink library and untitled windows appear
It is here where we
construct our model.
It is where we obtain
the blocks to
construct our model
Constructing the model using Simulink:
‘Drag and drop’ block from the Simulink library
window to the untitled window
1
s+1
Transfer Fcn
simout
To Workspace
Sine Wave
Constructing the model using Simulink:
LC
1
s
L
R
s
)
L
/
1
(
s
2

 6
2
10
1
s
1000
s
)
100
(
s



100s
s +1000s+1e6
2
Transfer Fcn
v
To Workspace1
i
To Workspace
Sine Wave
We need to vary the frequency and observe the current
100s
s +1000s+1e6
2
Transfer Fcn1
v
To Workspace3
w
To Workspace2
i
To Workspace
Ramp
s
1
Integrator
sin
Elementary
Math
Dot Product3
Dot Product2
1000
Constant
5
Amplitude
…From initial problem definition, the input is 5sin(ωt).
You should be able to decipher why the input works, but you
do not need to create your own input subsystems of this form.
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-1
-0.5
0
0.5
1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-5
0
5
The waveform can be displayed using scope – similar
to the scope in the lab
100s
s +1000s+1e6
2
Transfer Fcn
0.802
Slider
Gain
Scope
s
1
Integrator
sin
Elementary
Math
Dot Product2
5
Constant1
2000
Constant

Weitere ähnliche Inhalte

Ähnlich wie 1.1Introduction to matlab.pptx

Matlab 1
Matlab 1Matlab 1
Matlab 1
asguna
 

Ähnlich wie 1.1Introduction to matlab.pptx (20)

Matlab ch1 intro
Matlab ch1 introMatlab ch1 intro
Matlab ch1 intro
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
Matlab1
Matlab1Matlab1
Matlab1
 
Matlab 1
Matlab 1Matlab 1
Matlab 1
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
 
Es272 ch1
Es272 ch1Es272 ch1
Es272 ch1
 
Matlab anilkumar
Matlab  anilkumarMatlab  anilkumar
Matlab anilkumar
 
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptxMATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
 
Matlabch01
Matlabch01Matlabch01
Matlabch01
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projects
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
 
MatlabSimulinkTutorial.ppt
MatlabSimulinkTutorial.pptMatlabSimulinkTutorial.ppt
MatlabSimulinkTutorial.ppt
 
Matlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.pptMatlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.ppt
 
An Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked ExamplesAn Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked Examples
 
Matlab basic and image
Matlab basic and imageMatlab basic and image
Matlab basic and image
 
Introduction of MatLab
Introduction of MatLab Introduction of MatLab
Introduction of MatLab
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Summer training matlab
Summer training matlab Summer training matlab
Summer training matlab
 

Kürzlich hochgeladen

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 

Kürzlich hochgeladen (20)

Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 

1.1Introduction to matlab.pptx

  • 1.
  • 2. Contents Built in functions Getting Started Vectors and Matrices Introduction Simulink Modeling examples MATLAB SIMULINK M–files : script and functions
  • 4.  Don’t have to declare type  Don’t even have to initialise  Just assign in command window >> >> a=12; % variable a is assigned 12 4 Matlab prompt assign operator suppress command output comment operator Matlab prompt assign operator suppress command output comment operator Try the same line without the semicolon and comments
  • 5.  View variable contents by simply typing the variable name at the command prompt >> a a = 12 >> >> a*2 a = 24 >> 5
  • 6.  The workspace is Matlab’s memory  Can manipulate variables stored in the workspace >> b=10; >> c=a+b c = 22 >> 6
  • 7.  Display contents of workspace >> whos Name Size Bytes Class a 1x1 8 double array b 1x1 8 double array c 1x1 8 double array Grand total is 3 elements using 24 bytes >>  Delete variable(s) from workspace >> clear a b; % delete a and b from workspace >> whos >> clear all; % delete all variables from workspace >> whos 7
  • 8.  Don’t need to initialise type, or dimensions >>A = [3 2 1; 5 1 0; 2 1 7] A = 3 2 1 5 1 0 2 1 7 >> 8 square brackets to define matrices semicolon for next row in matrix
  • 9.  Access elements of a matrix >>A(1,2) ans= 2  Remember Matrix(row, column)  Naming convention Matrix variables start with a capital letter while vectors or scalar variables start with a simple letter 9 A = 3 2 1 5 1 0 2 1 7 indices of matrix element(s)
  • 10.  VERY important operator in Matlab  Means ‘to’ >> 1:10 ans = 1 2 3 4 5 6 7 8 9 10 >> 1:2:10 ans = 1 3 5 7 9 10 Try the following >> x=0:pi/12:2*pi; >> y=sin(x)
  • 11. >>A(3,2:3) ans = 1 7 >>A(:,2) ans = 2 1 1 11 A = 3 2 1 5 1 0 2 1 7 What’ll happen if you type A(:,:) ?
  • 12. >> A ' % transpose >> B*A % matrix multiplication >> B.*A % element by element multiplication >> B/A % matrix division >> B./A % element by element division >> [B A] % Join matrices (horizontally) >> [B; A] % Join matrices (vertically) 12 A = 3 2 1 5 1 0 2 1 7 B = 1 3 1 4 9 5 2 7 2 Create matrices A and B and try out the matrix operators in this slide Enter matrix B into the Matlab workspace
  • 13.  Arithmetic operations – Matrices Performing operations to every entry in a matrix Add and subtract >>> A=[1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 >>> >>> A+3 ans = 4 5 6 7 8 9 10 11 12 >>> A-2 ans = -1 0 1 2 3 4 5 6 7
  • 14.  Arithmetic operations – Matrices Performing operations to every entry in a matrix Multiply and divide >>> A=[1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 >>> >>> A*2 ans = 2 4 6 8 10 12 14 16 18 >>> A/3 ans = 0.3333 0.6667 1.0000 1.3333 1.6667 2.0000 2.3333 2.6667 3.0000
  • 15.  Arithmetic operations – Matrices Performing operations to every entry in a matrix Power >>> A=[1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 >>> A^2 = A * A To square every element in A, use the element–wise operator .^ >>> A.^2 ans = 1 4 9 16 25 36 49 64 81 >>> A^2 ans = 30 36 42 66 81 96 102 126 150
  • 16.  Arithmetic operations – Matrices Performing operations between matrices >>> A=[1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 >>> B=[1 1 1;2 2 2;3 3 3] B = 1 1 1 2 2 2 3 3 3 A*B                     3 3 3 2 2 2 1 1 1 9 8 7 6 5 4 3 2 1 A.*B           3 x 9 3 x 8 3 x 7 2 x 6 2 x 5 2 x 4 1 x 3 1 x 2 1 x 1           27 24 21 12 10 8 3 2 1 = =           50 50 50 32 32 32 14 14 14
  • 17.  Arithmetic operations – Matrices Performing operations between matrices A/B A./B           0000 . 3 6667 . 2 3333 . 2 0000 . 3 5000 . 2 0000 . 2 0000 . 3 0000 . 2 0000 . 1 = ? (matrices singular)           3 / 9 3 / 8 3 / 7 2 / 6 2 / 5 2 / 4 1 / 3 1 / 2 1 / 1
  • 18.  Arithmetic operations – Matrices Performing operations between matrices A^B A.^B           729 512 343 36 25 16 3 2 1 = ??? Error using ==> ^ At least one operand must be scalar           3 3 3 2 2 2 1 1 1 9 8 7 6 5 4 3 2 1
  • 19. Scalar functions – used for scalars and operate element-wise when applied to a matrix or vector e.g. sin cos tan atan asin log abs angle sqrt round floor At any time you can use the command help to get help e.g. >>>help sin
  • 20.  Matlab editor  Use scripts to execute a series of Matlab commands 20 Matlab Desktop Press to create new m- file in the matlab editor
  • 21.  Scripts will manipulate and store variables and matrices in the Matlab Workspace (memory).  They can be called from the Matlab command line by typing the (case sensitive!) filename of the script file. >> myscript  Scripts can be opened in the editor by the following >> open myscript 21 Highlight a few lines of your script by left- clicking and dragging the mouse over the lines. Right-click the highlighted lines and select Evaluate Selection. Will be slightly different in Linux
  • 22. >> I=iterate(5) I = 1 4 9 16 25 22 output input function name for statement block function keyword help lines for function Access the comments of your Matlab functions >> help iterate Make sure you save changes to the m-file before you call the function!
  • 23. >> [i j]=sort2(2,4) i = 4 j = 2 >> 23 Functions can have many outputs contained in a matrix Remember to use the Matlab help command for syntax >> help if if statement block
  • 24. 24 Method is linear >> i = 4 i = 16 i = 256 While statement block Switch statement block Without ; to print output
  • 25. >> figure % create new figure >> t=0:pi/12:8*pi; >> y=cos(t); >> plot(t,y,‘b.-') 25 Investigate the function >> y=A*cos(w*t+phi); for different values of phi (eg: 0, pi/4, pi/3, pi/2), w (eg: 1, 2, 3, 4) and A (eg: 1, 0.5, 2). Use the hold on Matlab command to display your plots in the same figure. Remember to type hold off to go back to normal plotting mode. Try using different plot styles (help plot) A = amplitude phi = phase w = angular frequency = 2*pi*frequency Plot style
  • 26. Data visualisation – plotting graphs Example on plot – 2 dimensional plot Example on plot – 2 dimensional plot >>> x=linspace(0,(2*pi),100); >>> y1=sin(x); >>> y2=cos(x); >>> plot(x,y1,'r-') >>> hold Current plot held >>> plot(x,y2,'g--') >>> Add title, labels and legend title xlabel ylabel legend Use ‘copy’ and ‘paste’ to add to your window– based document, e.g. MSword
  • 27. Data visualisation – plotting graphs 0 1 2 3 4 5 6 7 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 angular frequency (rad/s) y1 and y2 Example on plot sin(x) cos(x) Example on plot – 2 dimensional plot eg1_plt.m
  • 28. Example – RLC circuit Exercise 1: Write an m–file to plot Z, Xc and XLversus frequency for R =10, C = 100 uF, L = 0.01 H. + V – R = 10 C L
  • 29. Example – RLC circuit Total impedance is given by: L C X X  When LC 1 o  
  • 30. Example – RLC circuit
  • 31. For a given values of C and L, plot the following versus the frequency a) the total impedance , b) Xc and XL c) phase angle of the total impedance for 100 <  < 2000 Example – RLC circuit + V – R = 10 C L
  • 32. < ¦ save ! > rand load guide ~= zeros … get == min ' ' set >= max { } <= repmat try & axis catch 32 Remember to use the Matlab help command if you get stuck string cell error handling Operating system command Graphical user interface Continue in next line
  • 33. Every function must begin with a header: function output=function_name(inputs) Output variable Must match the file name input variable
  • 34. Used to model, analyze and simulate dynamic systems using block diagrams. Provides a graphical user interface for constructing block diagram of a system – therefore is easy to use. However modeling a system is not necessarily easy !
  • 35. Model – simplified representation of a system – e.g. using mathematical equation We simulate a model to study the behavior of a system – need to verify that our model is correct – expect results Knowing how to use Simulink or MATLAB does not mean that you know how to model a system
  • 36. Problem: We need to simulate the resonant circuit and display the current waveform as we change the frequency dynamically. + v(t) = 5 sin t – i 10  100 uF 0.01 H Varies  from 0 to 2000 rad/s Observe the current. What do we expect ? The amplitude of the current waveform will become maximum at resonant frequency, i.e. at  = 1000 rad/s
  • 37. How to model our resonant circuit ? + v(t) = 5 sin t – i 10  100 uF 0.01 H     idt C 1 dt di L iR v Writing KVL around the loop,
  • 38. LC i dt i d L R dt di dt dv L 1 2 2    Differentiate wrt time and re-arrange: Taking Laplace transform: LC I I s sI L R L sV 2             LC 1 s L R s I L sV 2
  • 39. Thus the current can be obtained from the voltage:              LC 1 s L R s ) L / 1 ( s V I 2 LC 1 s L R s ) L / 1 ( s 2   V I
  • 40. Start Simulink by typing simulink at Matlab prompt Simulink library and untitled windows appear It is here where we construct our model. It is where we obtain the blocks to construct our model
  • 41. Constructing the model using Simulink: ‘Drag and drop’ block from the Simulink library window to the untitled window 1 s+1 Transfer Fcn simout To Workspace Sine Wave
  • 42. Constructing the model using Simulink: LC 1 s L R s ) L / 1 ( s 2   6 2 10 1 s 1000 s ) 100 ( s    100s s +1000s+1e6 2 Transfer Fcn v To Workspace1 i To Workspace Sine Wave
  • 43. We need to vary the frequency and observe the current 100s s +1000s+1e6 2 Transfer Fcn1 v To Workspace3 w To Workspace2 i To Workspace Ramp s 1 Integrator sin Elementary Math Dot Product3 Dot Product2 1000 Constant 5 Amplitude …From initial problem definition, the input is 5sin(ωt). You should be able to decipher why the input works, but you do not need to create your own input subsystems of this form.
  • 44. 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 -1 -0.5 0 0.5 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 -5 0 5
  • 45. The waveform can be displayed using scope – similar to the scope in the lab 100s s +1000s+1e6 2 Transfer Fcn 0.802 Slider Gain Scope s 1 Integrator sin Elementary Math Dot Product2 5 Constant1 2000 Constant