Materi Workshop MATLAB+WS

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 87

MATLAB 101

MATLAB for Practical Use in Electrical Engineering


INTRODUCTION OF
MATLAB
WHAT IS MATLAB?

MATLAB is an interactive program for numerical computation and data


visualization; it is used extensively by control engineers for analysis and design

There are many different toolboxes available which extend the basic functions of
MATLAB into different application areas; in these tutorials, we will make extensive
use of the Control Systems Toolbox.

MATLAB is supported on Unix, Macintosh, and Windows environments; a student


version of MATLAB is available for personal computers.
BASIC FEATURE
● It is a high-level language for numerical computation, visualization and application development.
● It also provides an interactive environment for iterative exploration, design and problem solving.
● It provides vast library of mathematical functions for linear algebra, statistics, Fourier analysis, filtering,
optimization, numerical integration and solving ordinary differential equations.
● It provides built-in graphics for visualizing data and tools for creating custom plots.
● MATLAB's programming interface gives development tools for improving code quality maintainability
and maximizing performance.
● It provides tools for building applications with custom graphical interfaces.
● It provides functions for integrating MATLAB based algorithms with external applications and languages
such as C, Java, .NET and Microsoft Excel.
WHY MATLAB?
OVERVIEW OF BASIC
PROGRAMMING IN
MATLAB
DATA TYPES
● int8 & uint8 • cell array
● int16 & uint16 • structure
● int32 & uint32 • function handle
● int64 & uint64 • user classes
● single • java classes
● double
● logical
● char
DECISION MAKING
● If .. end
● If .. elseif .. end
● If .. elseif .. else .. end
● Nested if
● Switch
● Nested switch
ITERATION
● For loop
● While loop
● Nested loop
PLOTTING
SYSTEM MODELING
USING TRANSFER FUNCTION
TF
• tf function on matlab is used to make a transfer function of a system.
The arguments of this function are arrays that describe numerator and
denumerator coefficient.

 
R(s) C(s)

 
R(s) C(s)
SYNTAX
EXAMPLE
EXAMPLE
EXAMPLE
FEEDBACK
• This function is used to generate transfer function of a negative feedback
system.

 
R(s) C(s)
-

 
SYNTAX
Sys = feedback(G,H)

G and H is a transfer function


EXAMPLE 1
Determine the transfer function of this unity gain feedback system
 
R(s) C(s)
-
EXAMPLE 2
 
R(s) C(s)
-

• 
ZPK
• Zpk function can be used to determine transfer function of a system that the poles, zeros
and amplifier gain are provided.
• Syntax:
Sys=zpk(Z,P,K)
With z is array containing zero(s), P is array containing pole(s) and k is the amplifier gain value

 
R(s) C(s)

note :
EXAMPLE
• Determine the transfer function of a system that has a zero on s=-1
and two poles on s=-2 and s=-4
PZMAP
• Pzmap function is used to graph the location of poles & zeros of a
system in s plane.

Syntax:
pzmap(G)

G: transfer function
EXAMPLE 1
• Determine the poles and zeros of this system

 
R(s) C(s)
CONTOH 2

 
R(s) C(s)
-
DIGITAL FILTER DESIGN
IIR & FIR
In signal processing, a finite impulse response (FIR) filter is a filter whose impulse response (or response to any
finite length input) is of finite duration, because it settles to zero in finite time. This is in contrast to
infinite impulse response (IIR) filters, which may have internal feedback and may continue to respond
indefinitely (usually decaying).
In this PPT :
IIR :
- Butterworth filter design
- Chebyshev filter design
FIR :
- Window-based FIR filter design
- Frequency sampling-based FIR filter design
IIR : Butter
Butterworth filter design : a type of signal processing filter designed to have a frequency response as
flat as possible in the passband
IIR : BUTTER
Syntax :
[b,a] = butter(n,Wn)
[b,a] = butter(n,Wn) returns the transfer function coefficients of an nth-order lowpass digital
Butterworth filter with normalized cutoff frequency Wn.
[b,a] = butter(n,Wn,ftype)
[b,a] = butter(n,Wn,ftype) designs a lowpass, highpass, bandpass, or bandstop Butterworth filter,
depending on the value of ftype and the number of elements of Wn. The resulting bandpass and bandstop
designs are of order 2n.
[z,p,k] = butter(___)
[z,p,k] = butter(___) designs a lowpass, highpass, bandpass, or bandstop digital Butterworth filter and
returns its zeros, poles, and gain. This syntax can include any of the input arguments in previous syntaxes.
IIR : BUTTER
Syntax :
[A,B,C,D] = butter(___)
[A,B,C,D] = butter(___) designs a lowpass, highpass, bandpass, or bandstop digital Butterworth filter
and returns the matrices that specify its state-space representation.
[___] = butter(___,'s')
[___] = butter(___,'s') designs a lowpass, highpass, bandpass, or bandstop analog Butterworth filter
with cutoff angular frequency Wn.
IIR : BUTTER
example1
6th-order lowpass Butterworth filter with a
cutoff frequency of 300 Hz, which, for data
sampled at 1000 Hz, corresponds to 0.6π
rad/sample. Plot its magnitude and phase
responses. Use it to filter a 1000-sample
random signal.
IIR : BUTTER
example2
6th-order Butterworth bandstop filter with
normalized edge frequencies of 0.2π and 0.6π
rad/sample. Plot its magnitude and phase
responses. Use it to filter random data.
IIR : CHEBY1, CHEBY 2
Chebyshev filters are analog or digital filters having a steeper roll-off than Butterworth filters, and
have passband ripple (type I) or stopband ripple (type II)
IIR : CHEBY1
[b,a] = cheby1(n,Rp,Wp) returns the transfer function coefficients of an nth-order lowpass digital
Chebyshev Type I filter with normalized passband edge frequency Wp and Rp decibels of peak-to-peak
passband ripple.
[b,a] = cheby1(n,Rp,Wp,ftype) designs a lowpass, highpass, bandpass, or bandstop Chebyshev Type I
filter, depending on the value of ftype and the number of elements of Wp. The resulting bandpass and
bandstop designs are of order 2n.
[z,p,k] = cheby1(___) designs a lowpass, highpass, bandpass, or bandstop digital Chebyshev Type I filter
and returns its zeros, poles, and gain. This syntax can include any of the input arguments in previous
syntaxes.
[A,B,C,D] = cheby1(___) designs a lowpass, highpass, bandpass, or bandstop digital Chebyshev Type I
filter and returns the matrices that specify its state-space representation.
[___] = cheby1(___,'s') designs a lowpass, highpass, bandpass, or bandstop analog Chebyshev Type I
filter with passband edge angular frequency Wp and Rp decibels of passband ripple.
CHEBY1 : EXAMPLE
Design a 8th-order Chebyshev Type I bandstop filter with normalized edge
frequencies of and rad/sample and 7 dB of passband ripple. Plot its magnitude and
phase responses. Use it to filter random data.

[b,a] = cheby1(4,7,[0.5 0.8],'stop');


freqz(b,a)
dataIn = randn(1000,1);
dataOut = filter(b,a,dataIn);
IIR : CHEBY2
[b,a] = cheby2(n,Rs,Ws) returns the transfer function coefficients of an nth-order lowpass digital
Chebyshev Type II filter with normalized stopband edge frequency Ws and Rs decibels of stopband
attenuation down from the peak passband value.

[b,a] = cheby2(n,Rs,Ws,ftype) designs a lowpass, highpass, bandpass, or bandstop Chebyshev Type II


filter, depending on the value of ftype and the number of elements of Ws. The resulting bandpass and
bandstop designs are of order 2n.
[z,p,k] = cheby2(___) designs a lowpass, highpass, bandpass, or bandstop digital Chebyshev
Type II filter and returns its zeros, poles, and gain. This syntax can include any of the input
arguments in previous syntaxes.
[A,B,C,D] = cheby2(___) designs a lowpass, highpass, bandpass, or bandstop digital
Chebyshev Type II filter and returns the matrices that specify its state-space representation.
[___] = cheby2(___,'s') designs a lowpass, highpass, bandpass, or bandstop analog Chebyshev
Type II filter with stopband edge angular frequency Ws and Rs decibels of stopband
attenuation.
CHEBY2 : EXAMPLE
Design a 8th-order Chebyshev Type II bandstop filter with normalized edge frequencies of 0.4 pi
and 0.8pi rad/sample and 50 dB of passband attenuation. Plot its magnitude and phase responses.
Use it to filter random data

[b,a] = cheby2(4,50,[0.4 0.8],'bandpass');


freqz(b,a)
dataIn = randn(1000,1);
dataOut = filter(b,a,dataIn);
FIR : Fir1 (Window-based FIR filter design)
b = fir1(n,Wn) uses a Hamming window to design an nth-order lowpass, bandpass, or
multiband FIR filter with linear phase. The filter type depends on the number of elements of
Wn.
b = fir1(n,Wn,ftype) designs a lowpass, highpass, bandpass, bandstop, or multiband filter,
depending on the value of ftype and the number of elements of Wn.
b = fir1(___,window) designs the filter using the vector specified in window and any of the
arguments from previous syntaxes.
b = fir1(___,scaleopt) additionally specifies whether or not the magnitude response of the
filter is normalized.
Note: Use fir2 for windowed filters with arbitrary frequency response.
FIR1 : EXAMPLE
FIR : Fir2(Frequency sampling-based FIR filter)
b = fir2(n,f,m) returns an nth-order FIR filter with frequency-magnitude characteristics
specified in the vectors f and m. The function linearly interpolates the desired frequency
response onto a dense grid and then uses the inverse Fourier transform and a Hamming
window to obtain the filter coefficients.
b = fir2(n,f,m,npt,lap) specifies npt, the number of points in the interpolation grid, and lap, the
length of the region that fir2 inserts around duplicate frequency points which specify steps in
the frequency response.
b = fir2(___,window) specifies a window vector to use in the design in addition to any input arguments
from previous syntaxes.

Note: Use fir1 for window-based standard lowpass, bandpass, highpass, bandstop, and
multiband configurations.
FIR2 : EXAMPLE
f = [0 0.6 0.6 1];
m = [1 1 0 0];

b1 = fir2(40,f,m);
[h1,w] = freqz(b1,1);

plot(f,m,w/pi,abs(h1))
xlabel('\omega / \pi')
lgs = {'Ideal','fir2 default'};
legend(lgs)
MODULATION AND DEMODULATION
MODULATION
Process of varying the properties of a specific waveform, also called the carrier
signal, using a modulating signal that contains information that is trying to be
transmitted.
DEMODULATION
Demodulating is used to recover the message signal from the modulated wave
MODULATION TYPES

* Hands on hanya diperuntukan untuk Amplitude Modulation


SYNTAX USED

AM DSB(Dual Side Band)

z = ammod (x,Fc,Fs); • Fs : Sampling Frequency


z = amdemod(y,Fc,Fs); • Fc : Carrier Frequency
• y : Modulated Signal
AM SSB(Single Side Band) • x : Modulating Signal /
Message
z = ssbmod (x,Fc,Fs);
z = ssbdemod(y,Fc,Fs);
PLOTTING

• Subplot : Membuat grid untuk menggabungkan beberapa plot kedalam satu


figure.
(ex : subplot(3,1,1) berarti grid dengan 3 baris, 1 kolom, dan plot ke 1 )
• Plot : plotting sebuah fungsi dengan t adalah sampling time dan x adalah fungsi
terhadap t
• xlabel : label sumbu x
• Title : judul plot
• Grid : menampilkan grid pada plot
Plotting DSB
PLOTTING SSB
Result
ROOT LOCUS
ROOT LOCUS
• Root locus is a graphical method for examining how the roots of a system
change with variation of a certain system parameter, commonly a gain within
a feedback system
• It can be used to analyze the system response and stability margin
 
R(s)   C(s)
-

 
R(s) C(s)
-
• Syntax:
rlocus(sys,K)or
rlocus(sys)

sys is the open loop transfer function of the system


 
R(s) C(s)
-

 
EXAMPLE 1
• Sketch the root locus graph of this system using the rlocus function

 
R(s) C(s)
-
EXAMPLE 2
• Sketch the root locus graph of this system using the rlocus function

 
R(s) C(s)
-
EXAMINING GAIN, FREQUENCY, ETC
BODE PLOT
• Bode plot is used to graph the frequency response of the system
• Bode plot could also be used to determine the system stability margin
• Syntax:
bode(sys) or
bode(sys1,sys2,……)

Sys, sys1,… is the open loop transfer function


EXAMPLE 1
• Graph the bode plot of this system

 
R(s) C(s)
EXAMPLE 2
• This unity gain feedback system has a plant with the following
transfer function G(s) = . Graph the bode plot of the open loop
transfer function then determine whether the system is stable or
not.

R(s)
20 C(s)

- 𝑠 (𝑠+2)(𝑠+5)
DETERMINING SYSTEM STABILITY

Right click on the picture Click on characteristic  all


stability margin
It should shows like the picture above Click on the blue dot, it should shows
information about PM and GM

A system is stable when both the PM and GM of the open loop transfer function are positive.
Hence the system is stable
• GM = 36.9dB means that the system will remain stable until the
amplifier gain reach the maximum value of 36.9dB = 69.98 times
STEP RESPONSE
 
u(t)
• Step function is used to get system response when the input is a unit step function.
• Syntax:
step(sys) or
step(sys1,sys2, ….)

With sys1,sys2… are the transfer function of the system


EXAMPLE 1
• Graph the step response of this system using step function

 
R(s) C(s)
EXAMPLE 2
• Graph the step response of this system for k=1,2 and 3. Store each of the transfer
function on a different variable then graph the step response on a single picture
• Examine the pole-zero map of the system on each k value.
• Examine the root locus of the system.

 
R(s) C(s)
-
THANKS!
Does anyone have any questions?

Keprof EL

You might also like