You are on page 1of 71

Impulse

Input
Response
x[n] h[n]
0.5

(3,3,2)
0

10

11

10

0.2

0.1

0.4

0.2

0.6

0.3

0.8
1
1.2
1.4
1.6
1.8
2

0.5
1

BASIC MATLAB

0.5 0
1

0.2

0.4

OPERATIONS

0.6

0.5 0.8

0.4

0.5
FOR
MULTIPLICATION: 1
0.6
*NO.OF
COLUMNS = NO.OF(3,3,4)
ROWS
0.7
0.8

0.9
a=[1
2 3];
1
(3 COLUMNS) b=[1 2 3;4 5 0.5
6;7 8 9];
1
(3 ROWS) c=a*b

c=
30 36

0
0.5

42

FOR ELEMENTS MULTIPLICATION:


(3,3,5)
*SQUARE MATRIX = SQUARE
MATRIX
0
a=[4 2;2 4];
(2 ROWS=2 COLUMNS)
b=[1 2;2 1];
(2 ROWS=2 COLUMNS)
c=a*b
c=
8 10
10
8

0.5
1
0
0.5
1

FOR DIVISION:
*NO.OF ROWS = NO.OF COLUMNS
a=[1 2:2 1];
(2 ROWS)
b=[1 2];
(2 COLUMNS)
c= b\a
c=
0
0
0
0.5000 1.0000 0.5000
FOR ELEMENTS DIVISION:
a=[1 2 4 7];
b=[2 4 7 5 ];
c=b\a
c=
0
0

0
0

0
0

Prepared by:Hayat Wali


Iqra University

0
0
Page 1

0.1429
0

0.2857 0.5714
0
0
0

1.0000

c=a\b
c=
0
0
0
0.2857

0
0
0
0
0
0
0
0
0
0.5714 1.0000

0.7143

POWER OF ELEMENTS:
a=[1 2 3 4];
b=(a.^2)
b=1
4

16

c=(a.^3)
c=1
8 27

64

d=(a.^4)
d=1 16

81 256

& So On
ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION:
a=[1 4 2 5];
a+1
ans =2
a-1
ans = 0

5
3

3
1

6
4

a*1
ans =1

a/1
ans =1

a\1
ans =
0
0
0
0.2000

COLUMNS ADDITION:
a=[2 4;5 6];
sum(a)
ans =
7

10

ROWS ADDITION:
a=[2 4;5 6];
sum(a,2)
ans =
6
11
ALL ELEMENTS ADDITION:
a=[2 4;5 6];
sum(sum(a))
ans =
17
INVERSE:
a=[2 4;5 6];
inv(a)
ans =
-0.7500 0.5000
0.6250 -0.2500
DETERMINATE:
a=[2 4;5 6];
det(a)
ans =
-8
MEAN:
Prepared by:Hayat Wali
Iqra University

Page 3

a=[2 4;5 6];


mean(a)
ans =
3.5000
STD:

5.0000

a=[2 4;5 6];


std(a)
ans =
2.1213

1.4142

VARIATION:
a=[2 4;5 6];
var(a)
ans =
4.5000

2.0000

FOR MAXIMUM ROW:


a=[1 2 3 4;4 5 6 7;7 8 9 7];
max(a)
ans =
7

FOR MINIMUM ROW:


a=[1 2 3 4;4 5 6 7;7 8 9 7];
min(a)
ans =
1

FOR MAXIMUM ELEMENT:


a=[1 2 3 4;4 5 6 7;7 8 9 7];
max(max(a))

ans =
9
FOR MINIMUM ELEMENT:
a=[1 2 3 4;4 5 6 7;7 8 9 7];
min(min(a))
ans =
1
FOR SPECIFIC ROW:
a=[1 4 7;2 5 8;1 4 7];
a(2,:)
ans =
2

FOR SPECIFIC COLUMN:


a=[1 4 7;2 5 8;1 4 7];
a(:,3)
ans =
7
8
7
FOR SPECIFIC ELEMENT:
a=[1 4 7;2 5 8;1 4 7];
a(2,3)
ans =
8
SIZE OF MATRIX:
a=[1 2 4 7;4 5 8 7;4 1 4 4];
size(a)
ans =
3

Prepared by:Hayat Wali


Iqra University

Page 5

SIZE OF ROWS:
a=[1 2 4 7;4 5 8 7;4 1 4 4];
size(a,1)
ans =
3
SIZE OF COLUMNS:
a=[1 2 4 7;4 5 8 7;4 1 4 4];
size(a,2)
ans =
4
ALL ZEROS WITH REFERENCE OF ANY MATRIX:
a=[1 2 4 7;4 5 8 7;4 1 4 4];
zeros(size(a))
ans =
0
0
0

0
0
0

0
0
0

0
0
0

REFRENCE ELEMENTS:
a=[1 2 4 7;4 5 8 7;4 1 4 4];
a(2:3,3:4)
ans =
8
4

7
4

PLOTING IN MATLAB
Asin(wt+ )
A=Amplitude
w=Angular frequency
t=Time
=Phase Differences

(w=2f)
(f=w/2)

x=[1 2 4 5 7];
y=[4 7 8 5 8];
plot(x,y)
8
7.5
7

Y-Axis

6.5
6
5.5
5
4.5
4

4
X-axis

x=[1 2 4 5 7];
y=[4 7 8 5 8];
plot(y,x)
7

X-Axis

4.5

5.5

6
Y=Axis

6.5

7.5

t=[pi*(0:0.02:2)];
y=sin(t);
plot(y)

Prepared by:Hayat Wali


Iqra University

Page 7

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

20

40

60

80

100

120

40

60

80

100

120

t=[pi*(0:0.02:2)];
y=sin(t+pi/2);
plot(y)
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

20

t=[pi*(0:0.02:2)];
plot(t,sin(t))
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

t=[pi*(0:0.02:2)];
plot(t,cos(t))

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

t=[pi*(0:0.02:2)];
y=3*sin(3*t+0);
plot(y)
3

-1

-2

-3

20

40

60

80

100

120

40

60

80

100

120

t=[pi*(0:0.02:2)];
y=2*sin(6*t+pi);
plot(y)
2
1.5
1
0.5
0
-0.5
-1
-1.5
-2

20

Prepared by:Hayat Wali


Iqra University

Page 9

t=[pi*(0:0.02:2)];
y=2*cos(2*t+0);
plot(y)
2
1.5
1
0.5
0
-0.5
-1
-1.5
-2

20

40

60

80

100

120

t=[pi*(0:0.02:2)];
plot(t,sin(t))
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

t=[pi*(0:0.02:2)];
plot(t,cos(t))

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

t=[pi*(0:0.02:2)];
plot(t,sinc(t))
1

0.8

0.6

0.4

0.2

-0.2

-0.4

t=[pi*(0:0.02:2)];
plot(t,exp(t))
600

500

400

300

200

100

t=[pi*(0:0.02:10)];
Prepared by:Hayat Wali
Iqra University

Page 11

plot(t,sawtooth(t))
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

15

20

25

30

35

x=[-5:0.0001:5];
y=x.^2;
plot(y)
25

20

15

10

10

12
4

x 10

x=[-5:0.0001:5];
y=x.^3;
plot(y)
150

100

50

-50

-100

-150

10

12
4

x 10

x=linspace(-5,5);
y=sinc(x);
plot(x,y)
1

0.8

0.6

0.4

0.2

-0.2

-0.4
-5

-4

-3

-2

-1

PLOTING
WITH COLOURS
t=[0:0.0001:2*pi];
plot(t,sin(t),'k')
With Black
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

plot(t,sin(t),'g')
Prepared by:Hayat Wali
Iqra University

Page 13

With Green
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

plot(t,sin(t),'b')

With Blue
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

plot(t,cos(t),'r')

With Red
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

PLOTING
WITH DESIGNS & COLOURS
x=[0:0.1:2*pi];
plot(x,sin(x),'o-')

Prepared by:Hayat Wali


Iqra University

Page 15

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

plot(x,sin(x),'g+')
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

plot(x,sin(x),'kO')

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

plot(x,cos(x),'R^')

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

plot(x,cos(x),'kd')
Prepared by:Hayat Wali
Iqra University

Page 17

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

plot(x,cos(x),'r+')

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

DIFFERENT TYPES OF PLOTING


t=[0:0.001:1];
plot([t t.^2 t.^3])
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

200

400

600

800

1000

1200

t=[0:0.001:1]';
plot([t,sin(t),cos(t)])

Prepared by:Hayat Wali


Iqra University

Page 19

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

200

400

600

800

1000

1200

t=[0:0.001:1];
plot(t,[sin(t) cos(t)])

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

0.1

x=0:0.001:2*pi;
fill(x,sin(x),'g')

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

fs=10000;
t=0:1/fs:1/5;
y=sawtooth(2*pi*5*t);
plot(t,y)
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

0.02

0.04

0.06

0.08

0.1

0.12

0.14

0.16

0.18

t=0:0.00001:10;
y=sawtooth(2*pi*3*t*3);
plot(t,y)
Prepared by:Hayat Wali
Iqra University

Page 21

0.2

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

t=0:0.00001:10;
y=sawtooth(t,.5);
plot(t,y)

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

t=0:0.0001:100;
rectpuls(t);
plot(t,rectpuls(t))

10

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

10

20

30

40

50

60

70

80

90

100

t=0:0.0001:100;
plot(t,square(t))
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

20

30

40

50

60

70

80

90

100

t=0:0.0001:100;
y=square(t,80);
fill(t,y,'r')

Prepared by:Hayat Wali


Iqra University

Page 23

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

20

30

40

50

60

70

80

90

100

30

40

50

60

70

80

90

100

t=0:0.0001:100;
y=square(t,100);
fill(t,y,'g')
y=square(t,40);
fill(t,y,'r')
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

20

PLOTING WITH (Sine & Exponential)


t=[0:0.01:2*pi];
y=exp(sin(t));
plotyy(t,y,t,y,'plot','stem')

153.5
7
3

6
2.5
153

2.5

5
2

152.5
4
1.5

1.5

3
152
1
2

151.5
0.5
1

151
0
151
0

0.5

1 151.5

1523

152.5
4

153 6

0
153.5
7

t=[0:0.1:2*pi];
y=exp(sin(t));
plotyy(t,y,t,y,'plot','stem')
153.5
7
3

6
2.5
153

2.5

5
2

152.5
4
1.5

1.5

3
152
1
2

151.5
0.5
1

151
0
151
0

0.5

1 151.5

1523

152.5
4

153 6

0
153.5
7

SUBPLOTTING
For plotting many Figures in a single figure
Prepared by:Hayat Wali
Iqra University

Page 25

x=linspace(0,2*pi);
subplot(2,2,1)

(linspace is used for equal spacing b/w each number)


(2Rows, 2columns & 1st fig)

x=linspace(0,2*pi);
subplot(3,3,1)
subplot(3,3,2)
subplot(3,3,3)
subplot(3,3,4)
subplot(3,3,5)

(3Rows, 3columns & 1st fig)


(3Rows, 3columns & 2st fig)
(3Rows, 3columns & 3rd fig)
(3Rows, 3columns & 4th fig)
(3Rows, 3columns & 5th fig)

x=linspace(0,2*pi);
subplot(3,3,1)
subplot(3,3,2)
subplot(3,3,3)
subplot(3,3,4)
subplot(3,3,5)
plot(x,sin(x))

(3Rows, 3columns & 1st fig)


(3Rows, 3columns & 2st fig)
(3Rows, 3columns & 3rd fig)
(3Rows, 3columns & 4th fig)
(3Rows, 3columns & 5th fig)

1
0.8

0.5

0.6
0
0.4
-0.5
-1

0.2
0

0.8

0.8

0.6

0.6

0.4

0.4

0.2

0.2

0.5

0.5

0.5

x=[-10:0.01:10];
plot(x,exp(x))
grid on
hold on
plot(x,exp(0.95*x))
plot(x,exp(0.85*x))

(grid on is used for lining in graph)


(hold on is used for holding a figure for all graphs)

2.5

x 10

1.5

0.5

0
-10

-8

-6

-4

-2

10

PLOTTING WITH
COLOURS, TITLES, & LABELS
x=[-10:0.01:10];
plot(x,sin(x))
hold on
Prepared by:Hayat Wali
Iqra University

Page 27

grid on
plot(x,sin(2*x),'r--')
title('Multi sine plot')
ylabel('y-axis')
xlabel('x-axis')
legend('SinX','Sine2X')

(title is used for assigning a Title)


(ylabel is used for assigning Y-Label)
(xlabel is used for assigning X-Label)
(legend is used for assigning
separate notations for graphs)

Multi sine plot


1
SinX
Sine2X

0.8
0.6
0.4

y-axis

0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-10

-8

-6

-4

-2

0
x-axis

10

SEMI-LOG PLOTTING
x=[1000 10000 100000];
y=[2 4 6];
semilogx(x,y)

6
5.5
5
4.5
4
3.5
3
2.5
2
3
10

10

10

x=[10000,10000];
y=[1000,1000];
loglog(x,y)
4

10

10

10
3
10

10

10

AXIS DEFINING
axis([0 10 0 10])

Prepared by:Hayat Wali


Iqra University

(xlim 0 10; ylim 0 10)

Page 29

10
9
8
7
6
5
4
3
2
1
0

10

axis([0 4 0 1])

(xlim 0 4; ylim 0 1)

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

0.5

1.5

2.5

3.5

axis([-10 10 0 10])

(xlim -10 10; ylim -10 10)

10
9
8
7
6
5
4
3
2
1
0
-10

-8

-6

-4

-2

10

PLOT TOOLS

UTILITIES OF PLOT TOOLS:


Used for plotting different figures
Used for designing graph in many ways
Used for Title, X-label, Y-label & Legend as well
Giving 2D & 3D views
Changing colors
Giving Text
& many other tools can be used for plotting graphs
Prepared by:Hayat Wali
Iqra University

Page 31

3D PLOTTING
x=pi*(0:0.05:1);
y=2*x;
[X,Y]=meshgrid(x,y);
plot(X(:),Y(:),'k.')
plot(X(:),Y(:),'k.')
surf(X,Y,sin(X^2))
camlight left
lighting phong

0.5

-0.5

-1
8
6

4
3

1
0

x=pi*(0:0.05:1);
y=2*x;
[X,Y]=meshgrid(x,y);
plot(X(:),Y(:),'k.')
surf(X,Y,sin(X.^2+Y))

0.5

-0.5

-1
8
6

4
3

1
0

x=pi*(0:0.05:1);
y=2*x;
[X,Y]=meshgrid(x,y);
plot(X(:),Y(:),'k.')
surf(x,y,sin(X))

1
0.8
0.6
0.4
0.2
0
8
6

4
3

4
2

1
0

x=pi*(0:0.05:1);
y=2*x;
[X,Y]=meshgrid(x,y);
plot(X(:),Y(:),'k.')

Prepared by:Hayat Wali


Iqra University

Page 33

surf(x,y,cos(X.^2))

0.5

-0.5

-1
8
6

4
3

4
2

1
0

[X,Y]=meshgrid(-8:0.5:8);
R=sqrt(X.^2+Y.^2)+eps;
Z=sin(R)./R;
mesh(X,Y,Z)
surf(X,Y,Z)
colormap gray

0.5

-0.5
10
5

10
5

-5

-5
-10

-10

[X,Y]=meshgrid(-8:0.5:8);
R=sqrt(X.^2+Y.^2)+eps;
Z=sin(R)./R;

mesh(X,Y,Z)
surf(X,Y,Z)
colormap hsv

0.5

-0.5
10
5

10
5

-5

-5
-10

-10

[X,Y]=meshgrid(-8:0.5:8);
R=sqrt(X.^2+Y.^2)+eps;
Z=sin(R)./R;
mesh(X,Y,Z)
surf(X,Y,Z)
colormap copper

0.5

-0.5
10
5

10
5

-5

-5
-10

-10

x=[7 3 9 2 11 15 20 7 5 9];
Prepared by:Hayat Wali
Iqra University

Page 35

bar([0:length(x)-1],x)
th=[0:0.0001:2*pi];
rho=2*sin(th).*cos(th);
polar(th,rho)
90

120

60
0.8
0.6

150

30
0.4
0.2

180

210

330

240

300
270

x=rand([1 100]);
hist(x,10);
15

10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

NUMERICAL ANALYSIS
syms t
f=@(t,y)2.*y-1
f=
@(t,y)2.*y-1
ode45(f,[0,1],1)

Prepared by:Hayat Wali


Iqra University

Page 37

4.5

3.5

2.5

1.5

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

f=@(t,y)2.*y^2-1
ode45(f,[0,1],1)
14

2.5

x 10

1.5

0.5

0.1

f=@(t,y)2.*y^3-1;
ode45(f,[-1,1],-1)

0.2

0.3

0.4

0.5

0.6

0.7

x 10

-2

-4

-6

-8

-10

-12

-14
-1

-0.95

-0.9

-0.85

-0.8

-0.75

f=@(t,y)2.*y-23;
ode45(f,[0,1],1)
10
0
-10
-20
-30
-40
-50
-60
-70

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

0.3

0.4

0.5

0.6

0.7

0.8

0.9

f=@(t,y)2.*y-2;
ode45(f,[0,1],1)
2
1.8
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0

0.1

0.2

Prepared by:Hayat Wali


Iqra University

Page 39

f=@(t,y)2.*y-2;
ode45(f,[-1,1],-1)
0

-20

-40

-60

-80

-100

-120
-1

-0.8

-0.6

-0.4

-0.2

0.2

0.4

0.6

0.8

-0.4

-0.2

0.2

0.4

0.6

0.8

f=@(t,y)2.*y-23;
ode45(f,[-1,1],1)
100

-100

-200

-300

-400

-500

-600
-1

-0.8

-0.6

f=@(t,y)2.*y-3;
ode45(f,[-1,1],1)

-5

-10

-15

-20

-25

-30
-1

-0.8

-0.6

-0.4

-0.2

0.2

0.4

0.6

0.8

DIFFERENTIATION
Single Derivative:
syms x
g=sin(x);
diff(g)
ans =
cos(x)

d(g)/dx=d(sinx)/dx=Cosx

diff(x)
ans =
1

d(x)/dx=1

Single Derivative:
syms x
g=sin(x);
diff(g,x)

g=sin(x)
d(g)/dx=d(sinx)/dx=Cosx

ans =
cos(x)
g=cos(x);
diff(g,x)
ans =
-sin(x)
Prepared by:Hayat Wali
Iqra University

Page 41

Substitute Values (In Radian):


syms x
g=sin(x);
diff(g,x)
ans =
cos(x)
subs(ans,x,2.1)
ans =
-0.5048

cosx=cos(2.1)

Double Derivative:
syms x
g=sin(x);
diff(g,x,2)

g=sin(x)
d(g)/dx=d(sinx)/dx

ans =
-sin(x)

Higher Order Derivatives:


syms x
diff(sin(x),x,1)

(1 Represents for 1st Derivative)

ans =
cos(x)

syms x
diff(sin(x),x,2)

(2 Represents for 2nd Derivative)

ans =
-sin(x)

syms x
diff(sin(x),x,3)

(3 Represents for 3rd Derivative)

ans =
-cos(x)

syms x
diff(sin(x),x,4)

(4 Represents for 4th Derivative)

ans =
sin(x)

SUBSTITUTING VALUES
This is the shortcut command for substituting values in any function.

Subs(diff(f(x)),x,?)
syms x

(syms is Short-cut for constructing symbolic objects.)

syms x
diff(tan(x))
ans =
1+tan(x)^2

(Differentiate Tanx)

subs(ans,x,2)
ans =
5.7744

(Putting x=2 in the answer)

syms x
subs(diff(tan(x)),x,2)
ans =
5.7744

(Putting x=2 after differentiate Tanx)

syms x
subs(diff(sin(x)),x,1)
ans =
0.5403

(Putting x=1 after differentiate Sinx)

syms x
subs(diff(cos(x)),x,36)
ans =
0.9918
Prepared by:Hayat Wali
Iqra University

(Putting x=36 after differentiate Cosx)

Page 43

syms x
diff(tan(x^6-3*x+5))
ans =
(1+tan(x^6-3*x+5)^2)*(6*x^5-3)

(Differentiate Tan(x^6-3*x+5))

subs(ans,x,3/2)
ans =
69.9149

(Putting x=3/2 in the answer)

subs(diff(tan(x^6-3*x+5)),x,3/2)
ans =
69.9149

(Putting x=3/2 after differentiating Tan(x^6-3*x+5) )

INTEGERATION
syms x
int(sin(x),x)
ans =

(Integrate sin(x) )

-cos(x)
syms x
int(x*sin(x),x)
ans =

(Integrate xsin(x))

sin(x)-x*cos(x)

DOUBLE INTEGERATION:
double(int(sin(x^5+x^3),x,0,pi/2))
sin(x^5+x^3) ) & (0-/2) is limit
ans =

(Integrate

0.2910
quad8(inline(sin(x^5+x^3)'),0,pi/2)
/2) is limit
ans =
0.2910

(Integrate sin(x^5+x^3) ) & (0-

(Integrate sin(x^5+x^3) ) & (0-

quad8(inline(sin(x^5+x^3)'),0,pi/2)
/2) is limit
ans =
0.2910

RELATIONAL OPERATION
x=[1 2;2 3;5 6]
x=
1
2
5

2
3
6

x>2
ans =
0
0
1

(x>2 shows (1) the areas where x is greater than 2 otherwise 0)


0
1
1

x>1
ans =
0
1
1

(x>1 shows (1) the areas where x is greater than 1 otherwise 0)


1
1
1

3>1
ans =
1
3<1

Prepared by:Hayat Wali


Iqra University

Page 45

ans =
0
3<=6
ans =
1
3==4
ans =
0
3<=3
ans =
1

POSITION/REFERENCE LOCATOR
COLOUMN WISE
x=[1 2;2 3;5 6]
x=
1
2
5

2
3
6

x([2])
ans =

(Locating position at 2 in column)

2
x([2 3 4])
ans =
2
x(2)
ans =

(Locating position at 2,3,4 in column)


2
(Locating position at 2 in column)

2
x(3)
ans =
5

(Locating position at 3 in column)

x(x>2)
ans =

(Shows all values that are greater than 2)

5
3
6
x(x>1)
ans =

(Shows all values that are greater than 1)

2
5
2
3
6

POLYNOMIAL EQUATIONS
Polynomial equations are derived from word Poly means many.
We are here to find out the slop of the equations.
E.g.:( ax^3+bx^2+cx+d )
x=[1:2:20];
y=[2:2:20];
x=x';
y=y';
fit=polyfit(x,y,1)
fit =
1.0000

1.0000

plot(x,y,'o',x,fit(1)*x+fit(2))
22
20
18
16
14
12
10
8
6
4
2

10

Prepared by:Hayat Wali


Iqra University

12

14

16

18

20

Page 47

Finding Polynomial Equation by Roots:

If roots are:
x= +3
x= -1
We use the command POLY to converts the roots into polynomial.
Manually:

In MATLAB

E.g.: (x-3)(x+1)
x2 + x - 3x - 3= 0
x2 -2x -3 = 0

a=[3;-1]
poly(a)
ans =

1 -2 -3
x2 -2x -3 = 0

Finding Roots by Polynomial Equation:

If equation is:
x2 -2x -3 = 0
We use the command ROOTS to find out the roots of the equation:
Manually:
x2-2x-3 = 0
x2+x -3x-3 = 0
(x-3)(x+1)=0
x= +3
x= -1

In MATLAB
p=[1 -2 -3];
roots(p)
ans =
+3
-1
OR

roots([1 -2 -3])
ans =
+3
-1

Evaluate the Polynomial:

If the Polynomial Equation is:


F(x)=x3+6x-3=0
F(x)=x3+0x2+6x-3=0
Coefficients are [1 0 6 -3]
Evaluating by x=2

v=[1 0 6 -3];
polyval(v,2)
ans =
17
Evaluating by x=3
v=[1 0 6 -3];
polyval(v,3)
ans =
42

MATRICES
Manual

IN MATLAB

A =[2 3;1 4]

A=[2 3;1 4];

A=

A^-1
2

ans =

Inverse of A:

0.8000 -0.6000
-0.2000

A-1 =Adj A|A|

Adj A = 4

-3

-1

Prepared by:Hayat Wali


Iqra University

Page 49

0.4000

|A| = (4x2)-(-1x-3)
|A| = 8-3
|A| = 5

A-1 =

A-1 =

-3-1

25

0.8000 -0.6000
-0.2000 0.4000

Manual

IN MATLAB

A=[2 3;1 4]

A=[2 3;1 4];

A=

B=[9;3];
X=A^-1*B

X=

B=[9;3]

5.4000

B=

-0.6000
9
3

X =[X1;X2]
X=

X1
X2

AX=B
X=A-1 B

X=

Prepared by:Hayat Wali


Iqra University

Page 51

X1= 5.4000
X2= -0.6000

PROGRAMMING IN MATLAB
Programming is defined as list of instructions.
We create M-file for algorithm of any program.
F5 is used as a shortcut key to run a program.

Steps for writing & running a program:

1. Click new & go to the M-file.


2. Write algorithm of any program,
3. Save it using Ctrl-S or by clicking save button after assigning file name.

4. Go to the Matlab command window and type the file name.

Example:

Go to M-file & write program.


clc
x=0:0.0001:10
save & file name E.g. (Sine1.m)
Go to Matlab Command Window & type file name E.g. (Sine1).

Assigning Comments:

Go to M-file & write program.


% (Write anything for comments).
E.g.: % Hey how are you buddy?
Save it by assigning any file name E.g. (buddy.m).
Go to Matlab Command Window & type file name E.g. (help buddy).

Prepared by:Hayat Wali


Iqra University

Page 53

[x1,x2] Output Arguments

Function [x1, x2] =quadratic (a,b,c)

(a,b,c) Input Arguments

Program 1: (Plotting Sine wave):

Go to M-file & write algorithm of program.


x=0:0.00001:10;
y=sin(x);
plot(x,y)
Save it by assigning file name (a1.m).
Go to the Matlab command window and type the file name (a1).

Solution is:
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

Program 2: (Quadratic Equation Solver):


x=-bb2-4ac2a

We are going to solve a quadratic equation by quadratic formula algorithm.

Go to M-file & write algorithm of program.


function[x1,x2]=quadratic(a,b,c);
a=2;
b=3;
c=4;
d=sqrt(b^2-4*a*c);
x1=(-b+d)/(2*a)
x2=(-b-d)/(2*a)

Save it by assigning file name (quadratic.m).


Go to the Matlab command window and type the file name (quadratic).
Solution is:
x1 =
-0.7500 + 1.1990i
x2 =
-0.7500 - 1.1990i

Program 3: (Displaying a using for-loop):

Go to M-file & write algorithm of program.


a=1;
for i=[1:10];
a=a+i;
disp(a)
end
Save it by assigning file name (a3.m).
Go to the Matlab command window and type the file name (a3).
Solution is:
a1
2
4
7
11
16
22
29
37
46
56

Program 4: (Displaying a using for-loop):

Go to M-file & write algorithm of program.


for i=1:10;
a(i)=i*i
end
Save it by assigning file name (a4.m).
Go to the Matlab command window and type the file name (a4).
Solution is:
a1
a=
1
a=
1 4

Prepared by:Hayat Wali


Iqra University

Page 55

a=
1 4
a=
1 4
a=
1 4
a=
1 4
a=
1 4
a=
1 4
a=
1 4
a=
1 4

9
9

16

16

25

16

25

36

16

25

36

49

16

25

36

49

64

16

25

36

49

64

81

16

25

36

49

64

81 100

Program 5: (Plotting sine wave using No. of cycles & frequency):

Go to M-file & write algorithm of program.


f=input('enter frequency');
n=input('enter n.o of cycles');
t=(0:0.0001:n/f);
y=sin(2*pi*f*t);
plot(t,y)
Save it by assigning file name (a5.m).
Go to the Matlab command window and type the file name (a5).
Solution is:
a1
enter frequency3
enter n.o of cycles3

(No. of Frequencies are 3)


(No. of Cycles are 3)

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

Program 6: (Plotting sine & cosine waves using while-loop ):

Go to M-file & write algorithm of program.


x=0:0.1:10;
while(1>0)
a=menu('sine cosine',1,2,3,4,5);
plot(x,sin(x))
if (a==2)
plot(x,cos(x))
elseif(a==3)
stem(x,sin(x))
elseif(a==4)
stem(x,cos(x))
elseif(a==5)
break
end
end
Save it by assigning file name (a6.m).
Go to the Matlab command window and type the file name (a6).
A table will appear (Sine Cosine) containing numbers from 1-5.
Solution is:
By pressing 1:
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

By pressing 2:

Prepared by:Hayat Wali


Iqra University

Page 57

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

10

10

By pressing 3:
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

By pressing 4:
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

By pressing 5: (Program ended)

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

Program 7: (Displaying random numbers):

Go to M-file & write algorithm of program.


t=rand(1);
if t>0.75;
s=0
elseif t<0.25;
s=1
else
a=1-2*(t-0.25)
end
Save it by assigning file name (a7.m).
Go to the Matlab command window and type the file name (a7).

Program 8: (Displaying Tables of 1,2,3,4,5):


Prepared by:Hayat Wali
Iqra University

Page 59

Go to M-file & write algorithm of program.


clc
while(1>0)
a=menu('table',1,2,3,4,5);
if(a==1)
for s=1:10
z=1*s;
disp('1X');disp(s);disp('=');disp(z);
end
end
if(a==2)
for s=1:10
z=2*s;
disp('2x');disp(s);disp('=');disp(z);
end
end
if(a==3)
for s=1:10
z=3*s;
disp('3x');disp(s);disp('=');disp(z);
end
end
if(a==4)
for s=1:10
z=4*s;
disp('4x');disp(s);disp('=');disp(z);
end
end
if(a==5)
break
end
end
Save it by assigning file name (a8.m).
Go to the Matlab command window and type the file name (a8).

SIGNALS & SYSTEMS

USING MATLAB
CREATING SIGNALS (IN DISCREATE TIME):
Impulse function:
x=[1:11];
y=[1 zeros(1,10)];
stem(x,y)

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

10

11

Unit step function:


x=[1:11];
y=[ones(1,11)];
stem(x,y)
1
0 .9
0 .8
0 .7
0 .6
0 .5
0 .4
0 .3
0 .2
0 .1
0

Prepared by:Hayat Wali


Iqra University

10

11

Page 61

x=[1:10];
y=[0,0 ones(1,8)];
stem(x,y)
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

10

Exponential function (Decaying):


n=1:10;
x=0.5.^n;
stem(n,x)
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0

Exponential function (Increasing):


n=0:10;
x=2.^n;
stem(n,x)

10

1200

1000

800

600

400

200

10

CREATING SIGNALS (IN CONTINUOUS TIME):


Unit step function:
t=ones(1,100);
plot(t)
2
1.8
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0

10

20

30

40

50

60

70

80

90

100

Exponential function (Increasing):


t=1:0.001:10;
y=exp(t);
plot(t,y)
2.5

x 10

1.5

0.5

Prepared by:Hayat Wali


Iqra University

10

Page 63

Exponential function (Decaying):


t=1:0.001:10;
y=exp(-t);
plot(t,y)
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0

10

CONVOLUTION
Method #1(On command window)
X[n] =0.5n u[n]
H[n]=1
Y[n]=?

Input:
n=0:10;
x=0.5.^n;
stem(n,x)

Impulse Response:
h=ones(1,5);
h=[h ones(1,5)];
stem(h)

Convolution:
y=conv(x,h);
stem(y)

0n4

Method #2(By programming)

LAPLACE TRANSFORM
The laplace transform of a signal x(t),

X(t) X(s)=- xte-st dt


H(s) = T.F =OutputInput
H(s) = T.F =S2-1S2+2S-3

S2-1 = 0
S2=1
S= 1

S2+2S-3=0
S2+3S-S-3 = 0
S (S +3)-1(S +3) = 0
Prepared by:Hayat Wali
Iqra University

Page 65

(S -1)( S +3) = 0
S=1;S=-3

IN MATLAB:
For Equation:

For Roots:

o=[1 0 -1];
i=[1 2 -3];

Z1=roots(o)
Z1 =
1
-1
P1=roots(i)
P1 =
-3
1

h=tf(o,i)
Transfer function:
s^2 - 1
------------s^2 + 2 s - 3

For plotting S-plane:


o=[1 0 -1];
i=[1 2 -3];
h=tf(o,i);
Z1=roots(o);
P1=roots(i);
pzmap(Z1,P1)

Output
Input
Transfer function
Zeros
Poles
S-plane Map

Pole-Zero Map
1
0.8
0.6

Imaginary Axis

0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-3

-2.5

-2

-1.5

-1
Real Axis

For plotting S-plane using Sgrid:


o=[1 0 -1];
i=[1 2 -3];
h=tf(o,i);
Z1=roots(o);
P1=roots(i);

Output
Input
Transfer function
Zeros
Poles

Prepared by:Hayat Wali


Iqra University

Page 67

-0.5

0.5

pzmap(Z1,P1)
sgrid

S-plane Map

Pole-Zero Map
1
0.92
0.8

0.86

0.76

0.58

0.35

0.96

0.6
0.984

Imaginary Axis

0.4
0.2

0.996

3
0
-0.2

2.5

1.5

0.5

0.996

-0.4
0.984
-0.6
-0.8

0.96
0.92

-1
-3

-2.5

0.86
-2

-1.5

0.76

0.58

-1
Real Axis

For viewing samples of audio file:


a=wavread(file location',samples)
E.g.: a=wavread('C:\WINDOWS\Media\tada',2000)
MATLAB SOUND:
t=0:1/8192:1;
x=cos(2*pi*400*t);
soundsc(x,8198)

0.35

-0.5

0.5

For Noise:
t=0:1/8192:1;
x=cos(2*pi*400*t);
soundsc(x,8000)
noise=randn(8192,1);
soundsc(noise,8000)

SIMULINK
X(t)=Acos(wt+)
A Gain
Cos Trigonometric function
W Frequency
T Time
Phase difference

dxdy=(-2x+1) dx

Prepared by:Hayat Wali


Iqra University

Page 69

dx=-2x+1dx

X=-2x22+x+c

X=-x2+x+c

X(k)=e(k-3)+2.2x(k-1)-1.57x(k-2)+0.3x(k-3)
For 0k8

Prepared by:Hayat Wali


Iqra University

Page 71

You might also like