MATLAB Programming Lecture 6
MATLAB Programming Lecture 6
Lecture No.“6”
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
Math. Functions
Polynomials t = 0 : 0.001 :1 ;
that represents time running from 0 to 1 s in
Common sequences waveforms:
MATLAB plotting
Curve fitting
Delta=[1 zeros(1,99)];
Symbolic math.
plot(Delta)
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
MATLAB plotting
Flow Control
System analysis
%Step generation R=5u(t)
Curve fitting
y=5*ones(1,100);
Symbolic math.
plot(y)
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
MATLAB plotting
fs=10000; %sample rate
2-D & 3-D Graphics
t=0:1/fs:1;
Flow Control
y=sawtooth(2*pi*50*t);
System analysis
plot(t,y)
Curve fitting
axis([ 0 0.2 -1 1])
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
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
Polynomials
clear all, close all, clc
MATLAB plotting
fs=1000;
2-D & 3-D Graphics t = 0 : 1/fs : 2; % 1 kHz sample freq for
Flow Control
1 sec
System analysis
d = 0 : 1/3 : 2; % 3 Hz repetition freq
Curve fitting
y = pulstran(t,d,'tripuls',0.1,-1);
Symbolic math. plot(t,y)
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
MATLAB plotting
clear all, close all, clc
2-D & 3-D Graphics
t = 0 : 1/50000 : 0.01;
Flow Control
d =0 : 1/1000 : 0.01;
System analysis y = pulstran(t,d,'gauspuls',10000,0.5);
Curve fitting plot(t,y)
Symbolic math. axis([0 0.01 -2 2])
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
clear all, close all, clc
Polynomials
t=linspace(-5,5);
MATLAB plotting
y=sinc(t);
2-D & 3-D Graphics plot(t,y)
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
Flow Control
>> [u,t] = gensig( type, periode)
System analysis
>> [u,t] = gensig(type, periode,Tf,Ts)
%generates test signal specified by its
Curve fitting
Symbolic math.
type, periode , time duration Tf and the
Signal processing
spacing Ts between the time samples t
of the signal .
Function m-file
The following types of signals are
MATLAB Simulink
available.
Final Test Industrial Electronics and Automatic Control Engineering
'sin'-------->Sine wave.
Faculty of Electronic Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
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
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
[u,t] = gensig('pulse',5,30,0.1);
Polynomials
plot(t,u)
MATLAB plotting
axis([0 30 -1 1.5])
2-D & 3-D Graphics
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
Fourier Analysis
SIMATIC HMI
MATLAB
The Human
Programming
1. Fourier analysis is useful for data
Machine Interface
Introduction
and signal analysis.
MATLAB Basics
2. It breaks down a signal into
Vectors & Arrays fundamental sinusoids of different
Math. Functions frequencies.
Polynomials 3. For periodic signals or vector data,
MATLAB plotting Fourier analysis is performed using
2-D & 3-D Graphics Fourier series.
Flow Control 4. For non-periodic signals or vector
System analysis data, Fourier analysis is performed
Curve fitting using Fourier transform.
Symbolic math.
5. For sampled vector data, Fourier
Signal processing
analysis is performed using the
Function m-file
discrete Fourier transform (DFT).
MATLAB Simulink
6. The fast Fourier transform (FFT) is an
Final Test
efficient algorithm for computing the
Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Fourier Analysis
SIMATIC HMI
MATLAB
The Human
Programming
Syntax:
Machine Interface
Introduction >> Y = fft(X) %returns the
MATLAB Basics
MATLAB plotting
>> Y = fft(X,n) %returns the n-point
2-D & 3-D Graphics
DFT of vector X. .
Flow Control
System analysis
Note:
Curve fitting
If the length of X is less than n, X is added
Symbolic math.
with zeros to length n.
Signal processing
If the length of X is greater than n, the
Function m-file
increase of X is truncated or
MATLAB Simulink
neglected.
Final Test If X is a matrix, fft returns
Faculty of Electronic Engineering
the Fourier
Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Fourier Analysis
SIMATIC HMI
MATLAB Example :
The Human
Programming
Machine Interface Use of Fourier analysis to find the
Introduction frequency components of a noisy time
MATLAB Basics domain signal containing 50 Hz and 120 Hz
Vectors & Arrays
and corrupt it with some zero-mean random
Math. Functions
noise. Consider data sampled at 1000 Hz:
Polynomials
fs=1000;
MATLAB plotting
t = 0:1/fs:1;
2-D & 3-D Graphics
y = sin(2*pi*50*t)+sin(2*pi*120*t)+
Flow Control
2*randn(size(t));
System analysis
plot(y(1:100))
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
Fourier Analysis
SIMATIC HMI
MATLAB
The Human
Programming
Y = fft(y,512); % Taking the
Machine Interface
512-point (FFT).
Introduction
MATLAB Basics
Pyy = Y.* conj(Y) / 512; %power
Vectors & Arrays
spectrum, a measurement
Math. Functions f = fs*(0:256)/512;
Polynomials plot(f,Pyy(1:257))
fundamental
MATLAB plotting components
2-D & 3-D Graphics have higher
Flow Control
power
spectrum
System analysis
magnitudes.
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
Fourier Analysis
SIMATIC HMI
MATLAB
The Human
Programming
Magnitude and Phase of Transformed Data :
Machine Interface
Introduction MATLAB functions abs and angle are
MATLAB Basics used to calculate magnitude and phase.
Vectors & Arrays
MATLAB plotting
fs=100;
2-D & 3-D Graphics
t = 0:1/fs:9.99;
Flow Control
x = sin(2*pi*15*t) + sin(2*pi*40*t);
System analysis
y = fft(x); %computes DFT of the
data sequence.
Curve fitting
Symbolic math.
n=length(y);
Signal processing m = abs(y);
Function m-file p = unwrap(angle(y)); % unwrap to
MATLAB Simulink
remove phase jumps
Final Test Faculty of Electronic Engineering
greater than pi to
Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Fourier Analysis
SIMATIC HMI
MATLAB
The Human subplot(2,1,1),
Programming
Machine Interface
plot(f,m)
Introduction
grid on
MATLAB Basics
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Signal processing
High Pass Filter. Band Stop
Filter.
Function m-file
MATLAB Simulink
3.Filter Design Strategies.
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
Elliptic (ellip) Chebyshev
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Magnitude
MATLAB Basics
MATLAB plotting
Fc Frequency
2-D & 3-D Graphics
Magnitude
Introduction
MATLAB Basics
Stop Pass Stop
Vectors & Arrays
Band Band Band
Math. Functions
Polynomials
Fc1 Fc2
MATLAB plotting
Frequency
2-D & 3-D Graphics Band Stop Filter:
Flow Control
System analysis
Function m-file
Fc1 Fc2
MATLAB Simulink
Frequency
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Math. Functions
Magnitude
Flow Control
Fc1 Fc2
System analysis
Frequency
Curve fitting
Ideal Filter Response Practical Filter Response
Symbolic math.
Math. Functions
2. Design the specified filter i.e. obtain a, b parameters.
n
Polynomials
MATLAB plotting B ( z ) k 0 n
b z n
b0 b1 z 1 bn z n
H ( z) n 1 n
; a0 1
A( z ) 1 a1 z an z
n
2-D & 3-D Graphics n
a z
Flow Control
k 0
System analysis
Curve fitting
3. Analyze the designed filter responses.
Symbolic math.
e.g. [H,W] = freqz(b,a,N); %frequency response analysis
The number of points N by default 512 if it isn’t written
Signal processing
Function m-file 4. Apply the given signal to the designed filter by:
MATLAB Simulink sf = filter(b, a, S); %SF is the filtered signal
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
Polynomials
plot(freq,mag),grid on
xlabel('Freq (Hz)')
MATLAB plotting
ylabel('Magnitude')
2-D & 3-D Graphics
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
f :is a vector of frequency points, specified in the range between 0
Polynomials
and 1. The first point of f must be 0 and the last point 1, with all
MATLAB plotting
intermediate points in increasing order. Duplicate frequency
2-D & 3-D Graphics points are allowed.
Flow Control m :is a vector containing the desired magnitude response at the
System analysis points specified in f.
Curve fitting
Symbolic math.
f and m must be the same length.
Signal processing
plot(f,m) displays the filter shape.
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
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
Provides
MATLAB plotting The simplest kind of M-
2-D & 3-D Graphics Scripts
file. m-file Function m-file
extensibility to
They contain series of MATLAB
Flow Control
functions by
MATLAB commands,
System analysis
creating new
such as computations
Curve fitting functions
that you have to
Symbolic math. (program
perform repeatedly
Signal processing routines), that
from the command
accept input and
Function m-file
line.
MATLAB Simulink
return output
arguments.
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
User-defined Functions
SIMATIC HMI
MATLAB Functions look exactly like scripts, but
The Human
Programming
Machine Interface Functions must have a function
Introduction
declaration
MATLAB Basics
Math. Functions
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
User-defined Functions
SIMATIC HMI
MATLAB
The Human
User-defined functions design
Programming
Machine Interface
Introduction
steps:
MATLAB Basics 1. From Menu bar select File-->New-->Function
Vectors & Arrays or open a new m-file editor.
Math. Functions 2. Define Function m-file declaration line as:
Inputs must be
Polynomials
specified
MATLAB plotting
Final Test Faculty of Electronic Engineering Industrial Electronics and Automatic Control Engineering
for internal use only Automation
Dr./ Essam and
Nabil
Drives
User-defined Functions
SIMATIC HMI
MATLAB 3. Write Comment lines that describe detailed
The Human
Programming
Machine Interface help information a bout your function.
Introduction 4. Write all equations that required in your
MATLAB Basics function.
Vectors & Arrays 5. Save the Functions m-file with the same
Math. Functions name of the function. (Don’t change default
Polynomials
name)
Exampl
6. Now, your function is ready for using.
MATLAB plotting
e:
2-D & 3-D Graphics
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
User-defined Functions
SIMATIC HMI
MATLAB
The Human
Programming Exampl
Machine Interface
Introduction function
e: Z=impedance(R,L,C,f)
MATLAB Basics
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