MATLAB Programming Lecture 4
MATLAB Programming Lecture 4
Lecture No.“4”
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface
Introduction
MATLAB
MATLAB Basics
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
The Language of
Curve fitting Technical Computing
Symbolic math.
Signal processing
Prepared by:
Function m-file
Dr./ Essam Nabil
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Flow Control
SIMATIC HMI
MATLAB
The Human
Programming
This section covers those MATLAB
Machine Interface
Introduction
functions that provide conditional program
MATLAB Basics
control and program control over program
Vectors & Arrays loops.
Math. Functions
Polynomials
Flow Control Statements
MATLAB plotting
Flow Control
Flow Control
SIMATIC HMI
MATLAB
The Human
Relational Operators:
Programming
Machine Interface
Introduction
MATLAB Basics
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Logical Operators:
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Function m-file
discount=5/100
MATLAB Simulink
end
Final Test Faculty of Electronic Engineering
cost=price-price*discount
Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
elseif discount=30/100
Polynomials
expression2 elseif price>900
MATLAB plotting
Statements2; discount=25/100
2-D & 3-D Graphics
elseif elseif price>800
Flow Control
expression3 discount=20/100
System analysis
statements3; elseif price>500
Curve fitting
… discount=10/100
Symbolic math. else else
Signal processing Statementsn; discount=5/100
Function m-file end end
MATLAB Simulink cost = price -
Final Test Faculty of Electronic Engineering price*discount
Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Flow Control
Syntax1:
System analysis
switch expression (scalar or string)
Curve fitting
case value1
Symbolic math.
statement1;
Signal processing …
Function m-file otherwise
MATLAB Simulink statement, ..., statement
Final Test end
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Final Test
plot(T,Y)
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Flow Control
while expression n=0;
System analysis
Statements; while 2^n < 10
Curve fitting
end n=n+1
Symbolic math.
Signal processing
end
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
System analysis
SIMATIC HMI
MATLAB
The Human
Any dynamical system are described by
Programming
Machine Interface set of differential equations that can be
Introduction
modeled in time-domain using state space
MATLAB Basics
Transfer Function
SIMATIC HMI
MATLAB
The Human
Transfer functions are frequency-domain
Programming
Machine Interface representations of LTI systems. A SISO transfer
Introduction function is a ratio of polynomials:
MATLAB Basics
Math. Functions
To enter transfer function specify the
Polynomials
numerator and denominator polynomials:
MATLAB plotting
5
2-D & 3-D Graphics
G(s)=
Flow Control
-------------
System analysis
Curve fitting
s^2 + 2 s + 5
Symbolic math.
>>n=5;
Signal processing
>>d=[1 2 5];
Function m-file
>>printsys(n,d)
MATLAB Simulink
num/den =
Final Test
5
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Transfer Function
SIMATIC HMI
MATLAB
The Human
OR:
Programming
Machine Interface use tf function to construct the system
Introduction
transfer function :
MATLAB Basics
>> n=5;
Vectors & Arrays
>> d=[1 2 5];
Math. Functions
>> G=tf(n,d)
Polynomials
Transfer function:
MATLAB plotting
5
2-D & 3-D Graphics
-------------
Flow Control s^2 + 2 s + 5
Ex.1: 3 (s + 2)
System analysis
G(s)= -----------------------
Curve fitting
Symbolic math.
(s^2 + 4 s+ 5)
Signal processing
(s+3)
Function m-file
>> n=3*[1 2];
MATLAB Simulink
>> d=conv([1 4 5],[1 3]);
Final Test >> G=tf(n,d)
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Transfer Function
SIMATIC HMI
MATLAB
The Human
Programming
Machine Interface Ex.2:
Introduction
10 (s + 2)
MATLAB Basics
Transfer Function
SIMATIC HMI
MATLAB
The Human
Creation of Zero-Pole-Gain Models:
Programming
Machine Interface zpk function is used to create zero-pole-gain
Introduction
models (ZPK objects) or to convert TF or SS
MATLAB Basics
models to zero-pole-gain form.
Vectors & Arrays
Math. Functions
Syntax
Polynomials
sys = zpk ( z, p, k) %creates a
MATLAB plotting
continuous-time zero
2-D & 3-D Graphics
pole-gain model
Flow Control
with zeros z, poles
System analysis p, and gain(s) k.
Curve fitting Ex.2: 10 (s + 3)
Symbolic math. G(s)= ------------------
Signal processing (s+1)(s+2)(s+5)
Function m-file >> z=-3;
MATLAB Simulink >> p=[-1 -2 -5];
Final Test >>of Electronic
Faculty k=10; Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Transfer Function
SIMATIC HMI
MATLAB
The Human
System Response:
Programming
Machine Interface 1. Time response:
Introduction
Impulse response:-
MATLAB Basics
Transfer Function
SIMATIC HMI
MATLAB
The Human
50 (s + 2)
Programming
Machine Interface G(s)= --------------------
Introduction
(s^2 + 4 s+ 25)(s+3)
MATLAB Basics
Transfer Function
SIMATIC HMI
MATLAB
The Human Root locus diagram:-
Programming
Machine Interface
The root locus gives the closed-loop pole
Introduction
MATLAB Basics
trajectories as a function of the feedback gain
Vectors & Arrays
K (assuming negative feedback). Root locus
Math. Functions
are used to study the effects of varying
Polynomials
feedback gains on closed-loop pole locations.
In turn, these locations provide indirect
MATLAB plotting
information on the time and frequency
2-D & 3-D Graphics
responses.
Flow Control
System analysis
>>rlocus(G) %calculates and plots
Curve fitting
Symbolic math.
the root locus of
Signal processing
the open-loop SISO
Function m-file
model sys.
MATLAB Simulink
>>[r,k] = rlocus(sys) % return the
Final Test vector k of selected Industrial Electronics and Automatic Control Engineering
Faculty of Electronic Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Transfer Function
SIMATIC HMI
MATLAB
The Human
50 (s + 2)
Programming
Machine Interface G(s)= --------------------
Introduction
(s^2 + 4 s+
MATLAB Basics
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Transfer Function
SIMATIC HMI
MATLAB
The Human
Programming
2. Frequency response:
Machine Interface Bode plots:-
Introduction
MATLAB Basics
>>bode(G) % plots Bode
Vectors & Arrays
plot for the model G
Math. Functions >>[mag,phase,w] = bode(G) %
Polynomials computes the magnitude
MATLAB plotting mag and
2-D & 3-D Graphics phase values of the
Flow Control
Transfer Function
SIMATIC HMI
MATLAB
The Human
50 (s + 2)
Programming
Machine Interface G(s)= --------------------
Introduction
(s^2 + 4 s+
MATLAB Basics
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Transfer Function
SIMATIC HMI
MATLAB
The Human
50 (s + 2)
Programming
Machine Interface G(s)= --------------------
Introduction
(s^2 + 4 s+
MATLAB Basics
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Transfer Function
SIMATIC HMI
MATLAB
The Human
Discrete-Time System Models
Programming
Machine Interface The discrete-time system models are
Introduction representational schemes for digital filters. The
MATLAB Basics
transfer function is a basic z-domain
Vectors & Arrays
representation of a digital filter, expressing the
Math. Functions
filter as a ratio of two polynomials.
Polynomials
Transfer Function
SIMATIC HMI
MATLAB Transforming Between Discrete-Time
The Human
Programming
Machine Interface and Continuous-Time Representations:
Introduction
>>sysd = c2d(sysc,Ts) %discretizes the
MATLAB Basics
Transfer Function
SIMATIC HMI
MATLAB
The Human s-1
Programming
Machine Interface
G(s)= --------------
Introduction
MATLAB Basics
s^2 + 4 s +
Vectors & Arrays
5
Math. Functions >>n= [1 -1];
Polynomials >>d= [1 4 5];
MATLAB plotting >>H=tf(n,d);
2-D & 3-D Graphics >>Hd =
Flow Control c2d(H,0.1,'foh')
System analysis Transfer function:
Curve fitting 0.04226 z^2 -
Symbolic math. 0.01093 z - 0.03954
Signal processing ----------------------------
Function m-file -----
MATLAB Simulink
z^2 - 1.629 z +
Final Test
0.6703
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Transfer Function
SIMATIC HMI
MATLAB
The Human
Programming >>sysc = d2c(sysd) %produces a
Machine Interface
Introduction
continuous-time model
MATLAB Basics sysc that is
Vectors & Arrays equivalent to the discrete
Math. Functions time LTI model sysd
Polynomials using zero-order
MATLAB plotting hold on the inputs.
2-D & 3-D Graphics >>sysc = d2c(sysd,method) %gives
Flow Control access to alternative
System analysis
conversion schemes.
Curve fitting
The string method
Symbolic math.
selects the conversion
Signal processing
method among
Function m-file
the following:
MATLAB Simulink
‘zoh‘ -----Zero-order hold on the inputs. The
Final Test Industrial Electronics and Automatic Control Engineering
control inputs
Faculty of Electronic Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Transfer Function
SIMATIC HMI
MATLAB
The Human
(z+0.2)
Programming
Machine Interface
Introduction
H(z)=-------------------------
MATLAB Basics
Transfer Function
SIMATIC HMI
MATLAB
The Human >>sysd2 = d2d(sysd1,Ts,method)
Programming
Machine Interface
%resamples the discrete
Introduction
time model sys to produce an
MATLAB Basics
Math. Functions
Output equation
Polynomials where
MATLAB plotting x :is the state vector ∈Rnx1
2-D & 3-D Graphics n :system order
Flow Control A :is the system matrix ∈Rnxn
System analysis B :is the control matrix ∈Rnxp
Curve fitting p :no. of inputs
Symbolic math. u :is the input control vector ∈Rpx1
Signal processing y :is the measured output ∈Rqx1
Function m-file
C :is the output matrix ∈Rqxn
MATLAB Simulink
q :no. of outputs
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
Polynomials
rank(Cm) =n
MATLAB plotting
MATLAB Simulink
OR:
Final Test
>> Cm = ctrb(sys) Industrial%calculates the
Electronics and Automatic Control Engineering
Faculty of Electronic Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Polynomials
System analysis
-------
Curve fitting
Symbolic math.
Signal processing
Equating coefficients of (2)
Function m-file
equations (1) and (2) and solving
MATLAB Simulink
Final Test
for the controller gain matrix K
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Symbolic math.
K for the desired
Signal processing
closed loop pole
Function m-file
p, using
MATLAB Simulink
Ackermann's formula.
Final Test Notes:
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Signal processing
if(Cm_test~=0)
Function m-file
p=[-1 -2 -3]; %The desired
MATLAB Simulink
closed-loop poles i.e. p
Final Test
K = place(A,B,p) Industrial%finds
Faculty of Electronic Engineering
controller
Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Observer design
using pole-placement method
SIMATIC HMI
MATLAB
The Human Observability matrix
Programming
Machine Interface
Introduction
MATLAB Basics
Math. Functions
Polynomials
MATLAB plotting
The system is said to be completely
2-D & 3-D Graphics
state observable
Flow Control
if only: rank(Om) =n
Synatx:
System analysis
>>Om = obsv(A,B) %calculates the
Curve fitting
Symbolic math.
observability
Signal processing
matrix of the
Function m-file
state-space model
MATLAB Simulink OR:
Final Test >> Om = obsv (sys) %calculates
Industrial Electronics
Faculty of Electronic Engineering and Automatic Controlthe
Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Observer design
using pole-placement method
SIMATIC HMI
MATLAB
The Human
Programming
Observer schematic diagram:
Machine Interface
Introduction
MATLAB Basics
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Observer design
using pole-placement method
SIMATIC HMI
MATLAB
The Human
The observer state equations will be:
Programming
Machine Interface
Introduction
MATLAB Basics
Math. Functions
State estimation error is given by:
Polynomials
MATLAB plotting
Symbolic math.
(1)
-------
Signal processing
Function m-file
Equating coefficients of equations (1) and
MATLAB Simulink (2) L
(2) and solving for observer gain matrix
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Observer design
using pole-placement method
SIMATIC HMI
MATLAB
The Human Observer design using MATLAB:
Programming
Machine Interface
Introduction >>L = place(A’,C’,p) %computes
MATLAB Basics observer gain matrix
Vectors & Arrays
L for the
Math. Functions
desired poles locations p
Polynomials
OR:
MATLAB plotting
Symbolic math.
desired poles locations p
Signal processing
using
Function m-file
Ackermann's formula.
MATLAB Simulink
Notes:
Final Test
The length of p must
Faculty of Electronic Engineering
match the row
Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Observer design
using pole-placement method
SIMATIC HMI
MATLAB
The Human Application example:
Programming
Machine Interface
Introduction
clear,clc
MATLAB Basics
%The system state-space model
Vectors & Arrays parameters
Math. Functions A = [0 1 0;0 0 1;-12 -19 -8];
Polynomials
B = [0;0;1];
MATLAB plotting
C = [1 0 0];
2-D & 3-D Graphics
Om = obsv(A,C) %computes
Flow Control
Observability matrix
System analysis
Om_test=det(Om)
Curve fitting
%Observability test
Symbolic math.
Signal processing
if(Om_test~=0)
Function m-file
p=[-5 -10 -30]; %The desired
MATLAB Simulink
Observer poles i.e. p
Final Test
L = place(A’,C’,p) Industrial Electronics
Faculty of Electronic Engineering
%finds and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
Polynomials
MATLAB plotting
Flow Control
System analysis
Curve fitting
Symbolic math.
Signal processing
Function m-file
MATLAB Simulink
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering