SlideShare a Scribd company logo
An Introductory on
MATLAB and Simulink
MNS UET MULTAN
How To install MATLAB in
PC/Laptops!
Click here
Introduction to
MATLAB and Simulink
What can you gain from the course ?
Know basics of MATLAB/Simulink
– know how to solve simple problems
Know what MATLAB/Simulink is
Know how to get started with MATLAB/Simulink
Be able to explore MATLAB/Simulink on
your own !
Introduction to
MATLAB and Simulink
Contents
Built in functions
Getting Started
Vectors and Matrices
Introduction
Simulink
Modeling examples
MATLAB
SIMULINK
M–files : script and functions
Introduction
MATLAB – MATrix LABoratory
– Initially developed by a lecturer in 1970’s to help students
learn linear algebra.
– It was later marketed and further developed under
MathWorks Inc. (founded in 1984) – www.mathworks.com
– Matlab is a software package which can be used to perform
analysis and solve mathematical and engineering problems.
– It has excellent programming features and graphics capability
– easy to learn and flexible.
– Available in many operating systems – Windows, Macintosh,
Unix, DOS
– It has several tooboxes to solve specific problems.
Introduction
Simulink
– Used to model, analyze and simulate dynamic
systems using block diagrams.
– Fully integrated with MATLAB , easy and fast to
learn and flexible.
– It has comprehensive block library which can be
used to simulate linear, non–linear or discrete
systems – excellent research tools.
– C codes can be generated from Simulink models for
embedded applications and rapid prototyping of
control systems.
Getting Started
Run MATLAB from Start  Programs  MATLAB
Depending on version used, several windows appear
• For example in Release 13 (Ver 6), there are several
windows – command history, command, workspace, etc
• For Matlab Student – only command window
Command window
• Main window – where commands are entered
Example of MATLAB Release 13 desktop
Variables
– Vectors and Matrices –
ALL variables are matrices
Variables
•They are case–sensitive i.e x  X
•Their names can contain up to 31 characters
•Must start with a letter
Variables are stored in workspace
e.g. 1 x 1 4 x 1 1 x 4 2 x 4






4239
6512 7123












3
9
2
3
 4
Vectors and Matrices
 How do we assign a value to a variable?
>>> v1=3
v1 =
3
>>> i1=4
i1 =
4
>>> R=v1/i1
R =
0.7500
>>>
>>> whos
Name Size Bytes Class
R 1x1 8 double array
i1 1x1 8 double array
v1 1x1 8 double array
Grand total is 3 elements using 24 bytes
>>> who
Your variables are:
R i1 v1
>>>
Vectors and Matrices

















18
16
14
12
10
B
 How do we assign values to vectors?
>>> A = [1 2 3 4 5]
A =
1 2 3 4 5
>>>
>>> B = [10;12;14;16;18]
B =
10
12
14
16
18
>>>
A row vector –
values are
separated by
spaces
A column
vector –
values are
separated by
semi–colon
(;)
 54321A 
Vectors and Matrices
If we want to construct a vector of, say, 100
elements between 0 and 2 – linspace
>>> c1 = linspace(0,(2*pi),100);
>>> whos
Name Size Bytes Class
c1 1x100 800 double array
Grand total is 100 elements using 800 bytes
>>>
 How do we assign values to vectors?
Vectors and Matrices
 How do we assign values to vectors?
If we want to construct an array of, say, 100
elements between 0 and 2 – colon notation
>>> c2 = (0:0.0201:2)*pi;
>>> whos
Name Size Bytes Class
c1 1x100 800 double array
c2 1x100 800 double array
Grand total is 200 elements using 1600 bytes
>>>
Vectors and Matrices
 How do we assign values to matrices ?
Columns separated by
space or a comma
Rows separated by
semi-colon
>>> A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
>>> 









987
654
321
Vectors and Matrices
 How do we access elements in a matrix or a vector?
Try the followings:
>>> A(2,3)
ans =
6
>>> A(:,3)
ans =
3
6
9
>>> A(1,:)
ans =
1 2 3
>>> A(2,:)
ans =
4 5 6
Vectors and Matrices
 Some special variables
beep
pi ()
inf (e.g. 1/0)
i, j ( )1
>>> 1/0
Warning: Divide by zero.
ans =
Inf
>>> pi
ans =
3.1416
>>> i
ans =
0+ 1.0000i
Vectors and Matrices
 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
Vectors and Matrices
 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
Vectors and Matrices
 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
Vectors and Matrices
 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 



















333
222
111
987
654
321
A.*B










3x93x83x7
2x62x52x4
1x31x21x1










272421
12108
321
=
=










505050
323232
141414
Vectors and Matrices
 Arithmetic operations – Matrices
Performing operations between matrices
A/B
A./B










0000.36667.23333.2
0000.35000.20000.2
0000.30000.20000.1
=
? (matrices singular)










3/93/83/7
2/62/52/4
1/31/21/1
Vectors and Matrices
 Arithmetic operations – Matrices
Performing operations between matrices
A^B
A.^B










729512343
362516
321
=
??? Error using ==> ^
At least one operand must be scalar










333
222
111
987
654
321
Built in functions
(commands)
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
Built in functions (commands)
>>> a=linspace(0,(2*pi),10)
a =
Columns 1 through 7
0 0.6981 1.3963 2.0944 2.7925 3.4907
4.1888
Columns 8 through 10
4.8869 5.5851 6.2832
>>> b=sin(a)
b =
Columns 1 through 7
0 0.6428 0.9848 0.8660 0.3420 -0.3420
-0.8660
Columns 8 through 10
-0.9848 -0.6428 0.0000
>>>
Built in functions (commands)
Vector functions – operate on vectors returning
scalar value
e.g. max min mean prod sum length
>>> max(b)
ans =
0.9848
>>> max(a)
ans =
6.2832
>>> length(a)
ans =
10
>>>
>>> a=linspace(0,(2*pi),10);
>>> b=sin(a);
Built in functions (commands)
Matrix functions – perform operations on
matrices
>>> help elmat
>>> help matfun
e.g. eye size inv det eig
At any time you can use the command
help to get help
Built in functions (commands)
Matrix functions – perform operations on
matrices
>>> x=rand(4,4)
x =
0.9501 0.8913 0.8214 0.9218
0.2311 0.7621 0.4447 0.7382
0.6068 0.4565 0.6154 0.1763
0.4860 0.0185 0.7919 0.4057
>>> xinv=inv(x)
xinv =
2.2631 -2.3495 -0.4696 -0.6631
-0.7620 1.2122 1.7041 -1.2146
-2.0408 1.4228 1.5538 1.3730
1.3075 -0.0183 -2.5483 0.6344
>>> x*xinv
ans =
1.0000 0.0000 0.0000 0.0000
0 1.0000 0 0.0000
0.0000 0 1.0000 0.0000
0 0 0.0000 1.0000
>>>
Built in functions (commands)
Data visualisation – plotting graphs
>>> help graph2d
>>> help graph3d
e.g. plot polar loglog mesh
semilog plotyy surf
Built in functions (commands)
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
eg1_plt.m
Built in functions (commands)
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)
y1andy2
Example on plot
sin(x)
cos(x)
Example on plot – 2 dimensional plot
eg1_plt.m
Built in functions (commands)
Data visualisation – plotting graphs
Example on mesh and surf – 3 dimensional plot
>>> [t,a] = meshgrid(0.1:.01:2, 0.1:0.5:7);
>>> f=2;
>>> Z = 10.*exp(-a.*0.4).*sin(2*pi.*t.*f);
>>> surf(Z);
>>> figure(2);
>>> mesh(Z);
Supposed we want to visualize a function
Z = 10e(–0.4a) sin (2ft) for f = 2
when a and t are varied from 0.1 to 7 and 0.1 to 2, respectively
eg2_srf.m
Built in functions (commands)
Data visualisation – plotting graphs
Example on mesh and surf – 3 dimensional plot
eg2_srf.m
Built in functions (commands)
Data visualisation – plotting graphs
Example on mesh and surf – 3 dimensional plot
>>> [x,y] = meshgrid(-3:.1:3,-3:.1:3);
>>> z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
- 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
- 1/3*exp(-(x+1).^2 - y.^2);
>>> surf(z);
eg3_srf.m
Built in functions (commands)
Data visualisation – plotting graphs
Example on mesh and surf – 3 dimensional plot
eg2_srf.m
Solution : use M-files
M-files :
Script and function files
When problems become complicated and require re–
evaluation, entering command at MATLAB prompt is
not practical
Collections of commands
Executed in sequence when called
Saved with extension “.m”
Script Function
User defined commands
Normally has input &
output
Saved with extension “.m”
M-files : script and function files (script)
At Matlab prompt type in edit to invoke M-file editor
Save this file
as test1.m
eg1_plt.m
M-files : script and function files (script)
To run the M-file, type in the name of the file at the
prompt e.g. >>> test1
Type in matlabpath to check the list of directories
listed in the path
Use path editor to add the path: File  Set path …
It will be executed provided that the saved file is in the
known path
 Function is a ‘black box’ that communicates with
workspace through input and output variables.
INPUT OUTPUTFUNCTION
– Commands
– Functions
– Intermediate variables
M-files : script and function files (function)
Every function must begin with a header:
M-files : script and function files (function)
function output=function_name(inputs)
Output variable
Must match the
file name
input variable
 Function – a simple example
function y=react_C(c,f)
%react_C calculates the reactance of a capacitor.
%The inputs are: capacitor value and frequency in hz
%The output is 1/(wC) and angular frequency in rad/s
y(1)=2*pi*f;
w=y(1);
y(2)=1/(w*c);
M-files : script and function files (function)
File must be saved to a known path with filename the same as the
function name and with an extension ‘.m’
Call function by its name and arguments
help react_C will display comments after the header
 Function – a more realistic example
function x=impedance(r,c,l,w)
%IMPEDANCE calculates Xc,Xl and Z(magnitude) and
%Z(angle) of the RLC connected in series
%IMPEDANCE(R,C,L,W) returns Xc, Xl and Z (mag) and
%Z(angle) at W rad/s
%Used as an example for IEEE student, UTM
%introductory course on MATLAB
if nargin <4
error('not enough input arguments')
end;
x(1) = 1/(w*c);
x(2) = w*l;
Zt = r + (x(2) - x(1))*i;
x(3) = abs(Zt);
x(4)= angle(Zt);
M-files : script and function files (function) impedance.m
We can now add our function to a script M-file
R=input('Enter R: ');
C=input('Enter C: ');
L=input('Enter L: ');
w=input('Enter w: ');
y=impedance(R,C,L,w);
fprintf('n The magnitude of the impedance at %.1f
rad/s is %.3f ohmn', w,y(3));
fprintf('n The angle of the impedance at %.1f rad/s is
%.3f degreesnn', w,y(4));
M-files : script and function files (function) eg7_fun.m
Simulink
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 !
Simulink
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

More Related Content

What's hot

Matlab Graphics Tutorial
Matlab Graphics TutorialMatlab Graphics Tutorial
Matlab Graphics Tutorial
Cheng-An Yang
 
Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4
Randa Elanwar
 
Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4
Randa Elanwar
 
Mat lab
Mat labMat lab
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
Mukesh Kumar
 
BRESENHAM’S LINE DRAWING ALGORITHM
BRESENHAM’S  LINE DRAWING ALGORITHMBRESENHAM’S  LINE DRAWING ALGORITHM
BRESENHAM’S LINE DRAWING ALGORITHM
St Mary's College,Thrissur,Kerala
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notes
Infinity Tech Solutions
 
Leip103
Leip103Leip103
Intro to matlab
Intro to matlabIntro to matlab
Intro to matlab
Norhan Mohamed
 
Graphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsGraphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygons
Ketan Jani
 
MATLAB - Aplication of Arrays and Matrices in Electrical Systems
MATLAB - Aplication of Arrays and Matrices in Electrical SystemsMATLAB - Aplication of Arrays and Matrices in Electrical Systems
MATLAB - Aplication of Arrays and Matrices in Electrical Systems
Shameer Ahmed Koya
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
Satish Gummadi
 
Intro to tsql unit 10
Intro to tsql   unit 10Intro to tsql   unit 10
Intro to tsql unit 10
Syed Asrarali
 
Matlab ch1 (6)
Matlab ch1 (6)Matlab ch1 (6)
Matlab ch1 (6)
mohsinggg
 
1.2 matlab numerical data
1.2  matlab numerical data1.2  matlab numerical data
1.2 matlab numerical data
TANVIRAHMED611926
 
Programming with matlab session 1
Programming with matlab session 1Programming with matlab session 1
Programming with matlab session 1
Infinity Tech Solutions
 
Aaa ped-4- Data manipulation: Numpy
Aaa ped-4- Data manipulation: Numpy Aaa ped-4- Data manipulation: Numpy
Aaa ped-4- Data manipulation: Numpy
AminaRepo
 
matlab
matlabmatlab
matlab
Farhan Ahmed
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
Dan Bowen
 
Dda algo notes
Dda algo notesDda algo notes
Dda algo notes
shreeja asopa
 

What's hot (20)

Matlab Graphics Tutorial
Matlab Graphics TutorialMatlab Graphics Tutorial
Matlab Graphics Tutorial
 
Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4Introduction to matlab lecture 3 of 4
Introduction to matlab lecture 3 of 4
 
Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4
 
Mat lab
Mat labMat lab
Mat lab
 
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
 
BRESENHAM’S LINE DRAWING ALGORITHM
BRESENHAM’S  LINE DRAWING ALGORITHMBRESENHAM’S  LINE DRAWING ALGORITHM
BRESENHAM’S LINE DRAWING ALGORITHM
 
INTRODUCTION TO MATLAB session with notes
  INTRODUCTION TO MATLAB   session with  notes  INTRODUCTION TO MATLAB   session with  notes
INTRODUCTION TO MATLAB session with notes
 
Leip103
Leip103Leip103
Leip103
 
Intro to matlab
Intro to matlabIntro to matlab
Intro to matlab
 
Graphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygonsGraphics6 bresenham circlesandpolygons
Graphics6 bresenham circlesandpolygons
 
MATLAB - Aplication of Arrays and Matrices in Electrical Systems
MATLAB - Aplication of Arrays and Matrices in Electrical SystemsMATLAB - Aplication of Arrays and Matrices in Electrical Systems
MATLAB - Aplication of Arrays and Matrices in Electrical Systems
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
Intro to tsql unit 10
Intro to tsql   unit 10Intro to tsql   unit 10
Intro to tsql unit 10
 
Matlab ch1 (6)
Matlab ch1 (6)Matlab ch1 (6)
Matlab ch1 (6)
 
1.2 matlab numerical data
1.2  matlab numerical data1.2  matlab numerical data
1.2 matlab numerical data
 
Programming with matlab session 1
Programming with matlab session 1Programming with matlab session 1
Programming with matlab session 1
 
Aaa ped-4- Data manipulation: Numpy
Aaa ped-4- Data manipulation: Numpy Aaa ped-4- Data manipulation: Numpy
Aaa ped-4- Data manipulation: Numpy
 
matlab
matlabmatlab
matlab
 
Programming python quick intro for schools
Programming python quick intro for schoolsProgramming python quick intro for schools
Programming python quick intro for schools
 
Dda algo notes
Dda algo notesDda algo notes
Dda algo notes
 

Similar to Introduction of MatLab

Matlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.pptMatlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.ppt
PrasenjitDey49
 
MatlabSimulinkTutorial.ppt
MatlabSimulinkTutorial.pptMatlabSimulinkTutorial.ppt
MatlabSimulinkTutorial.ppt
jeronimored
 
Matlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.pptMatlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.ppt
DrBashirMSaad
 
1.1Introduction to matlab.pptx
1.1Introduction to matlab.pptx1.1Introduction to matlab.pptx
1.1Introduction to matlab.pptx
BeheraA
 
Matlab
MatlabMatlab
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlab
TUOS-Sam
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
aboma2hawi
 
A practical work of matlab
A practical work of matlabA practical work of matlab
A practical work of matlab
SalanSD
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
Mohd Esa
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
chestialtaff
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
Murshida ck
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
joellivz
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
Vinay Kumar
 
bobok
bobokbobok
Learn Matlab
Learn MatlabLearn Matlab
Learn Matlab
Abd El Kareem Ahmed
 
Matlabch01
Matlabch01Matlabch01
Matlabch01
Mohammad Ayyash
 
Matlab
MatlabMatlab
Es272 ch1
Es272 ch1Es272 ch1
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
062MayankSinghal
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
reddyprasad reddyvari
 

Similar to Introduction of MatLab (20)

Matlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.pptMatlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.ppt
 
MatlabSimulinkTutorial.ppt
MatlabSimulinkTutorial.pptMatlabSimulinkTutorial.ppt
MatlabSimulinkTutorial.ppt
 
Matlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.pptMatlab_Simulink_Tutorial.ppt
Matlab_Simulink_Tutorial.ppt
 
1.1Introduction to matlab.pptx
1.1Introduction to matlab.pptx1.1Introduction to matlab.pptx
1.1Introduction to matlab.pptx
 
Matlab
MatlabMatlab
Matlab
 
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlab
 
Matlab-1.pptx
Matlab-1.pptxMatlab-1.pptx
Matlab-1.pptx
 
A practical work of matlab
A practical work of matlabA practical work of matlab
A practical work of matlab
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginnersAn Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
 
Introduction to Matlab - Basic Functions
Introduction to Matlab - Basic FunctionsIntroduction to Matlab - Basic Functions
Introduction to Matlab - Basic Functions
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
 
bobok
bobokbobok
bobok
 
Learn Matlab
Learn MatlabLearn Matlab
Learn Matlab
 
Matlabch01
Matlabch01Matlabch01
Matlabch01
 
Matlab
MatlabMatlab
Matlab
 
Es272 ch1
Es272 ch1Es272 ch1
Es272 ch1
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
 

More from Imran Nawaz

Design Considerations for AASHTO Flexible pavement design
Design Considerations for AASHTO Flexible pavement designDesign Considerations for AASHTO Flexible pavement design
Design Considerations for AASHTO Flexible pavement design
Imran Nawaz
 
Determining equivalent single wheel load.(ESWL)
Determining equivalent single wheel load.(ESWL) Determining equivalent single wheel load.(ESWL)
Determining equivalent single wheel load.(ESWL)
Imran Nawaz
 
Pavement & Foundations
Pavement & FoundationsPavement & Foundations
Pavement & Foundations
Imran Nawaz
 
Pavement & Foundations
Pavement & FoundationsPavement & Foundations
Pavement & Foundations
Imran Nawaz
 
Banqiao Dam Failure
Banqiao Dam FailureBanqiao Dam Failure
Banqiao Dam Failure
Imran Nawaz
 
Use of computer in project management
Use of computer in project managementUse of computer in project management
Use of computer in project management
Imran Nawaz
 
Inventory Management
Inventory ManagementInventory Management
Inventory Management
Imran Nawaz
 
Seismic motions, Measuring earth quake sizes
Seismic motions, Measuring earth quake sizesSeismic motions, Measuring earth quake sizes
Seismic motions, Measuring earth quake sizes
Imran Nawaz
 
Assignment 2
Assignment 2Assignment 2
Assignment 2
Imran Nawaz
 
Concrete ppt
Concrete pptConcrete ppt
Concrete ppt
Imran Nawaz
 
Fundamentals of computer
Fundamentals of computerFundamentals of computer
Fundamentals of computer
Imran Nawaz
 

More from Imran Nawaz (11)

Design Considerations for AASHTO Flexible pavement design
Design Considerations for AASHTO Flexible pavement designDesign Considerations for AASHTO Flexible pavement design
Design Considerations for AASHTO Flexible pavement design
 
Determining equivalent single wheel load.(ESWL)
Determining equivalent single wheel load.(ESWL) Determining equivalent single wheel load.(ESWL)
Determining equivalent single wheel load.(ESWL)
 
Pavement & Foundations
Pavement & FoundationsPavement & Foundations
Pavement & Foundations
 
Pavement & Foundations
Pavement & FoundationsPavement & Foundations
Pavement & Foundations
 
Banqiao Dam Failure
Banqiao Dam FailureBanqiao Dam Failure
Banqiao Dam Failure
 
Use of computer in project management
Use of computer in project managementUse of computer in project management
Use of computer in project management
 
Inventory Management
Inventory ManagementInventory Management
Inventory Management
 
Seismic motions, Measuring earth quake sizes
Seismic motions, Measuring earth quake sizesSeismic motions, Measuring earth quake sizes
Seismic motions, Measuring earth quake sizes
 
Assignment 2
Assignment 2Assignment 2
Assignment 2
 
Concrete ppt
Concrete pptConcrete ppt
Concrete ppt
 
Fundamentals of computer
Fundamentals of computerFundamentals of computer
Fundamentals of computer
 

Recently uploaded

ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
nooriasukmaningtyas
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
PauloRodrigues104553
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
awadeshbabu
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
Ratnakar Mikkili
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
PuktoonEngr
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 

Recently uploaded (20)

ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
Low power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniquesLow power architecture of logic gates using adiabatic techniques
Low power architecture of logic gates using adiabatic techniques
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 

Introduction of MatLab

  • 1. An Introductory on MATLAB and Simulink MNS UET MULTAN
  • 2. How To install MATLAB in PC/Laptops!
  • 4.
  • 5.
  • 6. Introduction to MATLAB and Simulink What can you gain from the course ? Know basics of MATLAB/Simulink – know how to solve simple problems Know what MATLAB/Simulink is Know how to get started with MATLAB/Simulink Be able to explore MATLAB/Simulink on your own !
  • 7. Introduction to MATLAB and Simulink Contents Built in functions Getting Started Vectors and Matrices Introduction Simulink Modeling examples MATLAB SIMULINK M–files : script and functions
  • 8. Introduction MATLAB – MATrix LABoratory – Initially developed by a lecturer in 1970’s to help students learn linear algebra. – It was later marketed and further developed under MathWorks Inc. (founded in 1984) – www.mathworks.com – Matlab is a software package which can be used to perform analysis and solve mathematical and engineering problems. – It has excellent programming features and graphics capability – easy to learn and flexible. – Available in many operating systems – Windows, Macintosh, Unix, DOS – It has several tooboxes to solve specific problems.
  • 9. Introduction Simulink – Used to model, analyze and simulate dynamic systems using block diagrams. – Fully integrated with MATLAB , easy and fast to learn and flexible. – It has comprehensive block library which can be used to simulate linear, non–linear or discrete systems – excellent research tools. – C codes can be generated from Simulink models for embedded applications and rapid prototyping of control systems.
  • 10. Getting Started Run MATLAB from Start  Programs  MATLAB Depending on version used, several windows appear • For example in Release 13 (Ver 6), there are several windows – command history, command, workspace, etc • For Matlab Student – only command window Command window • Main window – where commands are entered
  • 11. Example of MATLAB Release 13 desktop
  • 12. Variables – Vectors and Matrices – ALL variables are matrices Variables •They are case–sensitive i.e x  X •Their names can contain up to 31 characters •Must start with a letter Variables are stored in workspace e.g. 1 x 1 4 x 1 1 x 4 2 x 4       4239 6512 7123             3 9 2 3  4
  • 13. Vectors and Matrices  How do we assign a value to a variable? >>> v1=3 v1 = 3 >>> i1=4 i1 = 4 >>> R=v1/i1 R = 0.7500 >>> >>> whos Name Size Bytes Class R 1x1 8 double array i1 1x1 8 double array v1 1x1 8 double array Grand total is 3 elements using 24 bytes >>> who Your variables are: R i1 v1 >>>
  • 14. Vectors and Matrices                  18 16 14 12 10 B  How do we assign values to vectors? >>> A = [1 2 3 4 5] A = 1 2 3 4 5 >>> >>> B = [10;12;14;16;18] B = 10 12 14 16 18 >>> A row vector – values are separated by spaces A column vector – values are separated by semi–colon (;)  54321A 
  • 15. Vectors and Matrices If we want to construct a vector of, say, 100 elements between 0 and 2 – linspace >>> c1 = linspace(0,(2*pi),100); >>> whos Name Size Bytes Class c1 1x100 800 double array Grand total is 100 elements using 800 bytes >>>  How do we assign values to vectors?
  • 16. Vectors and Matrices  How do we assign values to vectors? If we want to construct an array of, say, 100 elements between 0 and 2 – colon notation >>> c2 = (0:0.0201:2)*pi; >>> whos Name Size Bytes Class c1 1x100 800 double array c2 1x100 800 double array Grand total is 200 elements using 1600 bytes >>>
  • 17. Vectors and Matrices  How do we assign values to matrices ? Columns separated by space or a comma Rows separated by semi-colon >>> A=[1 2 3;4 5 6;7 8 9] A = 1 2 3 4 5 6 7 8 9 >>>           987 654 321
  • 18. Vectors and Matrices  How do we access elements in a matrix or a vector? Try the followings: >>> A(2,3) ans = 6 >>> A(:,3) ans = 3 6 9 >>> A(1,:) ans = 1 2 3 >>> A(2,:) ans = 4 5 6
  • 19. Vectors and Matrices  Some special variables beep pi () inf (e.g. 1/0) i, j ( )1 >>> 1/0 Warning: Divide by zero. ans = Inf >>> pi ans = 3.1416 >>> i ans = 0+ 1.0000i
  • 20. Vectors and Matrices  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
  • 21. Vectors and Matrices  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
  • 22. Vectors and Matrices  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
  • 23. Vectors and Matrices  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                     333 222 111 987 654 321 A.*B           3x93x83x7 2x62x52x4 1x31x21x1           272421 12108 321 = =           505050 323232 141414
  • 24. Vectors and Matrices  Arithmetic operations – Matrices Performing operations between matrices A/B A./B           0000.36667.23333.2 0000.35000.20000.2 0000.30000.20000.1 = ? (matrices singular)           3/93/83/7 2/62/52/4 1/31/21/1
  • 25. Vectors and Matrices  Arithmetic operations – Matrices Performing operations between matrices A^B A.^B           729512343 362516 321 = ??? Error using ==> ^ At least one operand must be scalar           333 222 111 987 654 321
  • 26. Built in functions (commands) 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
  • 27. Built in functions (commands) >>> a=linspace(0,(2*pi),10) a = Columns 1 through 7 0 0.6981 1.3963 2.0944 2.7925 3.4907 4.1888 Columns 8 through 10 4.8869 5.5851 6.2832 >>> b=sin(a) b = Columns 1 through 7 0 0.6428 0.9848 0.8660 0.3420 -0.3420 -0.8660 Columns 8 through 10 -0.9848 -0.6428 0.0000 >>>
  • 28. Built in functions (commands) Vector functions – operate on vectors returning scalar value e.g. max min mean prod sum length >>> max(b) ans = 0.9848 >>> max(a) ans = 6.2832 >>> length(a) ans = 10 >>> >>> a=linspace(0,(2*pi),10); >>> b=sin(a);
  • 29. Built in functions (commands) Matrix functions – perform operations on matrices >>> help elmat >>> help matfun e.g. eye size inv det eig At any time you can use the command help to get help
  • 30. Built in functions (commands) Matrix functions – perform operations on matrices >>> x=rand(4,4) x = 0.9501 0.8913 0.8214 0.9218 0.2311 0.7621 0.4447 0.7382 0.6068 0.4565 0.6154 0.1763 0.4860 0.0185 0.7919 0.4057 >>> xinv=inv(x) xinv = 2.2631 -2.3495 -0.4696 -0.6631 -0.7620 1.2122 1.7041 -1.2146 -2.0408 1.4228 1.5538 1.3730 1.3075 -0.0183 -2.5483 0.6344 >>> x*xinv ans = 1.0000 0.0000 0.0000 0.0000 0 1.0000 0 0.0000 0.0000 0 1.0000 0.0000 0 0 0.0000 1.0000 >>>
  • 31. Built in functions (commands) Data visualisation – plotting graphs >>> help graph2d >>> help graph3d e.g. plot polar loglog mesh semilog plotyy surf
  • 32. Built in functions (commands) 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 eg1_plt.m
  • 33. Built in functions (commands) 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) y1andy2 Example on plot sin(x) cos(x) Example on plot – 2 dimensional plot eg1_plt.m
  • 34. Built in functions (commands) Data visualisation – plotting graphs Example on mesh and surf – 3 dimensional plot >>> [t,a] = meshgrid(0.1:.01:2, 0.1:0.5:7); >>> f=2; >>> Z = 10.*exp(-a.*0.4).*sin(2*pi.*t.*f); >>> surf(Z); >>> figure(2); >>> mesh(Z); Supposed we want to visualize a function Z = 10e(–0.4a) sin (2ft) for f = 2 when a and t are varied from 0.1 to 7 and 0.1 to 2, respectively eg2_srf.m
  • 35. Built in functions (commands) Data visualisation – plotting graphs Example on mesh and surf – 3 dimensional plot eg2_srf.m
  • 36. Built in functions (commands) Data visualisation – plotting graphs Example on mesh and surf – 3 dimensional plot >>> [x,y] = meshgrid(-3:.1:3,-3:.1:3); >>> z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2); >>> surf(z); eg3_srf.m
  • 37. Built in functions (commands) Data visualisation – plotting graphs Example on mesh and surf – 3 dimensional plot eg2_srf.m
  • 38. Solution : use M-files M-files : Script and function files When problems become complicated and require re– evaluation, entering command at MATLAB prompt is not practical Collections of commands Executed in sequence when called Saved with extension “.m” Script Function User defined commands Normally has input & output Saved with extension “.m”
  • 39. M-files : script and function files (script) At Matlab prompt type in edit to invoke M-file editor Save this file as test1.m eg1_plt.m
  • 40. M-files : script and function files (script) To run the M-file, type in the name of the file at the prompt e.g. >>> test1 Type in matlabpath to check the list of directories listed in the path Use path editor to add the path: File  Set path … It will be executed provided that the saved file is in the known path
  • 41.  Function is a ‘black box’ that communicates with workspace through input and output variables. INPUT OUTPUTFUNCTION – Commands – Functions – Intermediate variables M-files : script and function files (function)
  • 42. Every function must begin with a header: M-files : script and function files (function) function output=function_name(inputs) Output variable Must match the file name input variable
  • 43.  Function – a simple example function y=react_C(c,f) %react_C calculates the reactance of a capacitor. %The inputs are: capacitor value and frequency in hz %The output is 1/(wC) and angular frequency in rad/s y(1)=2*pi*f; w=y(1); y(2)=1/(w*c); M-files : script and function files (function) File must be saved to a known path with filename the same as the function name and with an extension ‘.m’ Call function by its name and arguments help react_C will display comments after the header
  • 44.  Function – a more realistic example function x=impedance(r,c,l,w) %IMPEDANCE calculates Xc,Xl and Z(magnitude) and %Z(angle) of the RLC connected in series %IMPEDANCE(R,C,L,W) returns Xc, Xl and Z (mag) and %Z(angle) at W rad/s %Used as an example for IEEE student, UTM %introductory course on MATLAB if nargin <4 error('not enough input arguments') end; x(1) = 1/(w*c); x(2) = w*l; Zt = r + (x(2) - x(1))*i; x(3) = abs(Zt); x(4)= angle(Zt); M-files : script and function files (function) impedance.m
  • 45. We can now add our function to a script M-file R=input('Enter R: '); C=input('Enter C: '); L=input('Enter L: '); w=input('Enter w: '); y=impedance(R,C,L,w); fprintf('n The magnitude of the impedance at %.1f rad/s is %.3f ohmn', w,y(3)); fprintf('n The angle of the impedance at %.1f rad/s is %.3f degreesnn', w,y(4)); M-files : script and function files (function) eg7_fun.m
  • 46. Simulink 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 !
  • 47. Simulink 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