C Language Is Simplest. C Language Is Simplest
C Language Is Simplest. C Language Is Simplest
C Language is Simplest.
2
Name is
MATLAB
Recent Trends in Computational
techniques for Engineering
Use of MATLAB in
Engineering
Prof. Ashish M. Kothari
Department of Electronics & Communication Engineering
Atmiya Institute of Technology & Science, Rajkot
Topics..
7
What is MATLAB ??
Basic Matrix Operations
Script Files and M-files
Some more Operations and Functions
Plotting functions ..
Topics..
8
What is MATLAB ??
Basic Matrix Operations
Script Files and M-files
Some more Operations and Functions
Plotting functions
MATLAB
9
“MATrix LABoratory”
Starting MATLAB
On a Microsoft Windows platform, to start MATLAB, double-click the
MATLAB shortcut icon on your Windows desktop.
You can change the directory in which MATLAB starts, define startup
options including running a script upon startup, and reduce startup time in
some situations.
MATLAB- Starting & Quiting
12
Quiting MATLAB
What is MATLAB ??
Basic Matrix Operations
Script Files and M-files
Some more Operations and Functions
Plotting functions ..
Math & Assignment Operators
20
- (unary) + (unary)
Addition + a+b
Subtraction - a-b
Assignment = a=b (assign b to a)
Other MATLAB symbols
21
>> prompt
... continue statement on next line
, separate statements and data
% start comment which ends at end of line
; (1) suppress output
(2) used as a row separator in a matrix
: specify range
MATLAB Relational Operators
22
Scalars are matrices with only one row AND one column
MATLAB Matrices
25
» a_value=23
a_value =
23
MATLAB Matrices
26
rowvec =
12 14 63
MATLAB Matrices
27
colvec =
13
45
-2
MATLAB Matrices
28
» matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9]
matrix =
1 2 3
4 5 6
7 8 9
Extracting a Sub-Matrix
29
sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ;
» matrix=[1,2,3;4,5,6;7,8,9] » col_two=matrix( : , 2)
matrix = col_two =
1 2 3
4 5 6 2
7 8 9 5
8
MATLAB Matrices
31
matrix =
» rowvec=matrix(2 : 2 , 1 :
3)
1 2 3
4 5 6 rowvec =
7 8 9
4 5 6
Topics..
32
What is MATLAB ??
Basic Matrix Operations
Script Files /M-files and Function Files
Some more Operations and Functions
Plotting functions ..
Use of M-File
33
Click to create
a new M-File
M-File as script file
34
What is MATLAB ??
Basic Matrix Operations
Script Files and M-files
Some more Operations and Functions
Plotting functions ..
Some Useful MATLAB commands
37
if expression1 % is true
% execute these commands
elseif expression2 % is true
% execute these commands
else % the default
% execute these commands
end
MATLAB Repetition Structures
41
x=0.1:0.1:10;
b=sin(x);
Plot(b,x)
------ no
computational
complexity
Scalar - Matrix Addition
43
» a=3;
» b=[1, 2, 3;4, 5, 6]
b=
1 2 3
4 5 6
» c= b+a % Add a to each element of b
c=
4 5 6
7 8 9
Scalar - Matrix Subtraction
44
» a=3;
» b=[1, 2, 3;4, 5, 6]
b=
1 2 3
4 5 6
» c = b - a %Subtract a from each element of b
c=
-2 -1 0
1 2 3
Scalar - Matrix Multiplication
45
» a=3;
» b=[1, 2, 3; 4, 5, 6]
b=
1 2 3
4 5 6
» c = a * b % Multiply each element of b by a
c=
3 6 9
12 15 18
Scalar - Matrix Division
46
» a=3;
» b=[1, 2, 3; 4, 5, 6]
b=
1 2 3
4 5 6
» c = b / a % Divide each element of b by a
c=
0.3333 0.6667 1.0000
1.3333 1.6667 2.0000
The use of “.” – “Element” Operation
47
Given A:
Signal Processing
Image Processing
Communications
System Identification
Wavelet Filter Design
Control System
Fuzzy Logic
Robust Control
µ-Analysis and Synthesis
LMI Control
Model Predictive Control
…
MATLAB Demo
49
why
What is MATLAB ??
Basic Matrix Operations
Script Files and M-files
Some more Operations and Functions
Plotting functions ..
Plot
52
Example
PLOT Linear plot.
x = [-3 -2 -1 0 1 2 3];
PLOT(X,Y) plots vector Y
y1 = (x.^2) -1;
versus vector X
plot(x, y1,'bo-.');
PLOT(Y) plots the columns of
Y versus their index
PLOT(X,Y,S) with plot
symbols and colors
See also SEMILOGX,
SEMILOGY, TITLE,
XLABEL, YLABEL, AXIS,
AXES, HOLD, COLORDEF,
LEGEND, SUBPLOT...
52
Plot Properties
53
Example
53
Hold
54
Example
HOLD Hold current graph. ...
hold on;
HOLD ON holds the current
y2 = x + 2;
plot and all axis properties so
plot(x, y2, 'g+:');
that subsequent graphing
commands add to the existing
graph.
HOLD OFF returns to the
default mode
HOLD, by itself, toggles the
hold state.
54
Subplot
55
55
Figure
56
Example
x = [-3 -2 -1 0 1 2 3];
y1 = (x.^2) -1;
% Plot y1 in the 1st Figure
plot(x, y1,'bo-.');
xlabel('x values');
ylabel('y values');
% Plot y2 in the 2nd Figure
figure
y2 = x + 2;
plot(x, y2, 'g+:');
56
Surface Plot
57
x = 0:0.1:2;
y = 0:0.1:2;
[xx, yy] = meshgrid(x,y);
zz=sin(xx.^2+yy.^2);
surf(xx,yy,zz)
xlabel('X axes')
ylabel('Y axes')
57
3 D Surface Plot
58
contourf-colorbar-plot3-waterfall-contour3-mesh-surf
58
MATLAB Applications
59
DSP
>> t=-2*pi:0.1:2*pi;
y=1.5*sin(t);
plot(t,y);
xlabel('------> time')
ylabel('------> sin(t)')
59
MATLAB Applications
60
DSP
>> t=-2*pi:0.1:2*pi;
y=1.5*cos(t);
stem(t,y);
xlabel('------> time')
ylabel('------> sin(t)')
60
MATLAB Applications
61
DSP
n=input('enter value of n')
t=0:1:n-1;
y1=ones(1,n); %unit step
y2=[zeros(1,4) ones(1,n-4)]; %delayed unit step
subplot(2,1,1);
stem(t,y1,'filled');ylabel('amplitude');
xlabel('n----->');ylabel('amplitude');
subplot(2,1,2);
stem(t,y2,'filled');
xlabel('n----->');ylabel('amplitude');
61
MATLAB Applications
62
DSP
62
MATLAB Applications
63
Control Systems
Transfer Function
p1 s n + p 2 s n −1 + ... + p n +1
H (s ) =
q1 s m + q1 s m −1 + ... + q m +1
where
p1 , p 2 ... p n +1 numerator coefficients
q1 , q1 ... q m +1 denominator coefficients
63
MATLAB Applications
64
Control Systems
Transfer Function
Consider a linear time invariant (LTI) single-
input/single-output system
Control Systems
Transfer Function
>> num = [4 3]; >> [num,den] =
>> den = [1 6 5]; tfdata(sys,'v')
>> sys = tf(num,den) num =
Transfer function: 0 4 3
4s+3 den =
----------------- 1 6 5
s^2 + 6 s + 5
65
MATLAB Applications
66
Control Systems
Zero-pole-gain model (ZPK)
(s − p1)(s − p2 ) +... + (s − pn )
H (s ) = K
(s −q1)(s −q2 ) +... + (s −qm )
where
p1 , p2 ... pn+1 the zeros of H(s)
q1,q1 ... qm+1 the poles of H(s)
66
MATLAB Applications
67
Control Systems
Zero-pole-gain model (ZPK)
Consider a Linear time invariant (LTI) single-
input/single-output system
y ''+ 6 y '+ 5 y = 4u '+ 3u
Applying Laplace Transform to both sides with
zero initial conditions
Y (s) 4s + 3 4( s + 0.75)
G (s) = = 2 =
U ( s ) s + 6 s + 5 ( s + 1)( s + 5)
67
MATLAB Applications
68
Control Systems
>> G=tf([4 3],[1 6 5]) >> [z,p,k]=zpkdata(G)
Transfer function: z=
4s+3 [-0.7500]
-------------
s^2 + 6 s + 5 p=
[-5;-1]
k=
4
68
MATLAB Applications
69
Control Systems
>> bode(G)
69
MATLAB Applications
70
Control Systems
>> rlocus(G)
70
MATLAB Applications
71
Control Systems
>> nyquist(G)
71
MATLAB Applications
72
Control Systems
>> step(G)
72
MATLAB Applications
73
Control Systems
>> impulse(G)
73
MATLAB Applications
74
Communication
>> t=0:pi/100:2*pi;
>> x=sin(t);
>> subplot(211);plot(t,x);
>> Fc=1000;
>> Fs=2000;
>> y=ammod(x,Fc,Fs);
>> subplot(212);plot(t,y);
74
MATLAB Applications
75
Communication
75
Online MATLAB Resources
76
www.mathworks.com/
www.mathtools.net/MATLAB
www.math.utah.edu/lab/ms/matlab/matlab.html
web.mit.edu/afs/athena.mit.edu/software/matlab/
www/home.html
www.utexas.edu/its/rc/tutorials/matlab/
www.math.ufl.edu/help/matlab-tutorial/
www.indiana.edu/~statmath/math/matlab/links.html
www-h.eng.cam.ac.uk/help/tpl/programs/matlab.html
76
Reference Books
77
77
Some More Web Resources
78
Newsgroup:
comp.soft-sys.matlab
78
Thanks
79
Questions ??