“Introduction to MATLAB & SIMULINK”
Presented by
Amarjeetsingh Thakur
Asst. Professor
Dept. of Electronics & Communication Engg.
KLETU ,Hubli, Karnataka, India
1
WELCOME TO THE WORLD
OF MATLAB!!!
2
Playing w ith arrays of numbers
3
Creating and working with Arrays of
numbers
>> x=[4 5 6 ] % x is a row vector with three
elements
>> y=[1 2 3]
>> z=x+y % z=5 7 9
>> z=x-y %z= 3 3 3
>> j=[1;2;3] % x is a column vector with three
elements
>> z=x.*y % z= 4 10 18
>> z=x./y % z=4.0000 2.5000 2.0000
4
Contd…
x=linspace(0,10,5) % x = 0 2.5000 5.0000 7.5000
10.0000
Create a vector x with 5 elements linearly
spaced between 0 and 10
z=sqrt(x) % Square root of x
5
Creating p lots in mat lab so easy!!!
6
Creating simp le p lots
%Draw a circle of unit radius
theta= linspace(0,2*pi,100); % Create a linearly
spaced 100 elements long vector Θ
x=cos(theta);
y=sin(theta); % Calculate x and y coordinates
plot(x,y); % Plot x vs y
axis(‘equal’); % Set length scales of two axes to be
same
xlabel(‘x’);
ylabel(‘y’);
title(‘Circle of unit radius’);
7
Contd…
% Plot sine wave
% Plot created on 2/1/2014
x=linspace(0,2*pi,100);
plot(x,sin(x));
plot(x,sin(x),’r’); % Color of sine wave is red
plot(x,sin(x),’+’);
xlabel(‘x’);
ylabel(‘sin(x)’);
title(‘Plot created by Amarsingh’);
8
Contd…
% overlapping of sine waves
x=linspace(0,2*pi,100);
plot(x,sin(x),x,sin(x),’o’)
xlabel(‘x’);
ylabel(‘sin(x)’);
9
Contd…
x=linspace(0,2*pi,100);
subplot(3,1,1); %Divide figure window into 3
rows, 1 column and place the figure in first part
plot(x,sin(x));
subplot(3,1,2);
plot(x,cos(x));
subplot(3,1,3);
plot(x,tan(x));
10
Plotting the given function
>>syms x
>> subplot(3,1,1)
>> f=sin(x)
>> ezplot(f)
>> subplot(3,1,2)
>> f=cos(x)
>> ezplot(f)
>> subplot(3,1,3)
>> f=x+10
>> ezplot(f)
11
12
MATLAb w ill make us to enjoy
symbolic comp utation 
13
Symbolic Computation
>> syms x y % x & y as symbolic variables
>> f=(x+y)^2 % x^2 + 2*x*y + y^2
>>expand(f) % x^2 + 2*x*y + y^2
>>pretty(ans) % Use pretty to get the
expression in more readable form
14
Contd…
% Expansion of trignometric expression
>>z=sin(x+y)
>>expand(z) % cos(x)*sin(y) + cos(y)*sin(x)
>>pretty(ans) % cos(x) sin(y) + cos(y) sin(x)
>>z=cos(x+y)
>> expand(z) % cos(x)*cos(y) - sin(x)*sin(y)
>>pretty(ans)
>> z=tan(x+y)
>> expand(z) % -(tan(x) + tan(y))/(tan(x)*tan(y) - 1)
>> pretty(ans)
15
Contd..
% Differentiation of an expression
>> syms x y
>> z=sin(x+y)
>> diff(z) % differentiate z with respect to x.
16
Contd…
% Second derivative of z with respect to x
>> z=sin(x)
>> diff(diff(z)) % -sin(x)
>> diff(diff(z,x)) % -sin(x)
>> z=x^3+x^2+x
>> diff(diff(z,x)) % 6*x + 2
>> pretty(ans) % 6 x + 2
17
Contd…
% Indefinite integral
>> z= sin(x)
>> int(z,x) % -cos(x)
>> int(int(z,x)) %-sin(x) % Double integration
w.r.t. x
18
Contd…
% Definite integral
>> z=x^2
>> j=int(z,x,0,2) % j=8/3
>> k=int(j,x,0,3) % k=8
19
Have fun p laying w it h mat rice s 
20
Matrices & Vectors
% Create a random matrix 4X3
x=rand(4,3)
x(3:4,2:3) % get those elements of A that are
located in 3 to 4 and columns 2 to 3
x(:,4)=x(:,1) % Add a fourth column to x and set
it equal to the first column of x
x(2:4,2:4)=eye(3) % Replace the last 3X3
submatrix of x by a identity matrix
x([1 3],:)=[] % Delete the first and third rows of x
21
Contd…
x=round(x) % Round off all entries of x
22
Contd..
% Magic square matrix
x=magic(3) % x = magic(n) returns an n-by-n
matrix constructed from the integers 1
through n^2 with equal row and column sums.
The order n must be a scalar greater than or
equal to 3.
23
Programs? ? ?
Yes !!!you love it 
24
Loop s, B ranche s, and cont rol -flow
1. For loops: A for loop is used to repeat a
statement or a group of statements for a
fixed number of times.
>>for m=1:100
>>num=1/(m+1)
>>end
25
Contd…
2. If-elseif-else statements
>>i=6; j=21;
>>If i>5
>>k=i;
>>elseif (i>1) & (j==20)
>>k=5*i+j;
>>else
>>k=1;
>>end
26
Contd…
3. switch-case-otherwise
color=input(‘color=’,’s’);
switch color
case ‘red’
c=[1 0 0];
case ‘green’
c=[0 1 0];
case ‘blue’
c=[0 0 1];
otherwise
error(‘Invalid choice of color’)
end 27
MATLAB makes you smarter to do
linear algebra op erations 
28
Linear Algebra
%Task: To solve a set of given linear algebraic
equations
5x-3y+2z=10
-3x+8y+4z=20
2x+4y-9z=9
>> A=[5 -3 2; -3 8 4; 2 4 -9];
>> b=[10; 20; 9];
>> x=Ab
>> c=A*x % 10.00 20.00 9.00
29
Contd..
%Task: Compute roots of polynomials
x^5-3*x^3+x^2-9
>> c=[1 0 -3 1 -9];
>> roots(c)
30
FUN WITH BAR CHARTS & PIE CHARTS
31
B a s i c 2 - D p l o t s
% World population by continents.
>>cont=char(‘Asia’,’Europe’,’Africa’,
’N.America’,’S.America’);
>>pop=[3332;696;694;437;307];
>>barh(pop) % Bar chart
>>for i=1:5
gtext(cont(i,:));
>>end
>>xlabel(‘population in millions’)
>>title(‘World poplation (1992)’,’fontsize’,18)
32
33
Contd..
>>cont=char(‘Asia’,’Europe’,’Africa’,
’N.America’,’S.America’);
>>pop=[3332;696;694;437;307];
>>pie(pop) % Pie chart
>>for i=1:5
gtext(cont(i,:));
>>end
>>xlabel(‘population in millions’)
>>title(‘World poplation (1992)’,’fontsize’,18)
34
35
CONTd..
>>pop=[807;3701;731;481;349];
>>continents={'Africa','Asia','Europe','N.America
','S.America'};
>> pie3(pop,continents)
>> title('world population in 2003')
36
37
Ploting grap h on semilog she e t
>> t=linspace(0,2*pi,200);
>> x=exp(-t);
>> y=t;
>> semilogx(x,y)
>> grid
>> grid on
38
39
Contd..
>> t=linspace(0,2*pi,200);
>> semilogy(t,exp(t))
>> grid
40
41
HURRAY!!!
LEARNING SIMULINK IS SO FUN!!!
42
43
Display of sine wave on scope
44
Multiplexing & Demultiplexing of
signals
45
Logical Operations
46
Addition/Subtraction Operation
47
Comparing a constant
48
Plane take-off demo model
49
Plane take-off with trajectory tracing
50
References
www.mathworks.com
“Getting started with MATLAB” by Rudra
Pratap Singh.
51
Any QUERRIES??????
52
53

“Introduction to MATLAB & SIMULINK”