Professional Documents
Culture Documents
Basic Matlab Operations
Basic Matlab Operations
Basic Matlab Operations
FOR MULTIPLICATION:
*NO.OF COLUMNS = NO.OF ROWS
a=[1 2 3];
(3 COLUMNS) b=[1 2 3;4 5 6;7 8 9];
(3 ROWS) c=a*b
c=
30 36
42
0
0
0
0
0
0
0.2857 0.5714
0
0
0
1.0000
Page 1
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:
Prepared by:Hayat Wali
Iqra University
Page 2
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:
a=[2 4;5 6];
mean(a)
Prepared by:Hayat Wali
Iqra University
Page 3
ans =
3.5000
STD:
5.0000
1.4142
VARIATION:
a=[2 4;5 6];
var(a)
ans =
4.5000
2.0000
Page 4
SIZE OF ROWS:
Prepared by:Hayat Wali
Iqra University
Page 5
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];
Prepared by:Hayat Wali
Iqra University
Page 6
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)
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
t=[pi*(0:0.02:2)];
y=sin(t+pi/2);
plot(y)
1
t=[pi*(0:0.02:2)];
plot(t,sin(t))
0.8
0.6
0.4
0.8
0.2
0.6
0.4
-0.2
0.2
-0.4
-0.6
-0.2
-0.8
-1
-0.4
0
20
40
60
80
100
120
-0.6
-0.8
-1
t=[pi*(0:0.02:2)];
plot(t,cos(t))
Page 8
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
t=[pi*(0:0.02:2)];
Prepared by:Hayat Wali
Iqra University
Page 9
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
Page 10
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)];
plot(t,sawtooth(t))
Page 11
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);
Prepared by:Hayat Wali
Iqra University
Page 12
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')
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')
Page 14
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-')
1
plot(x,sin(x),'g+')
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
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),'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^')
Page 16
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
plot(x,cos(x),'kd')
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
plot(x,cos(x),'r+')
Page 17
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
200
400
600
800
1000
1200
t=[0:0.001:1]';
Prepared by:Hayat Wali
Iqra University
Page 18
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
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
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
x=0:0.001:2*pi;
fill(x,sin(x),'g')
Prepared by:Hayat Wali
Iqra University
Page 19
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
0.2
t=0:0.00001:10;
Prepared by:Hayat Wali
Iqra University
Page 20
y=sawtooth(2*pi*3*t*3);
plot(t,y)
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
10
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
Page 21
t=0:0.0001:100;
rectpuls(t);
plot(t,rectpuls(t))
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')
Page 22
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
1 00
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
Page 23
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 24
x=linspace(0,2*pi);
subplot(2,2,1)
0
1
.
5
2
4
6
8
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)
((3,3,3)
0
1
3,3,1)
3,3,2)
3,3,4)
3,3,5)
.
5
x=linspace(0,2*pi);
subplot(3,3,1)
subplot(3,3,2)
subplot(3,3,3)
Prepared by:Hayat Wali
Iqra University
subplot(3,3,4)
subplot(3,3,5)
plot(x,sin(x))
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))
2.5
x 10
1.5
0.5
0
-10
-8
-6
-4
-2
10
PLOTTING WITH
COLOURS, TITLES, & LABELS
Prepared by:Hayat Wali
Iqra University
Page 26
x=[-10:0.01:10];
plot(x,sin(x))
hold on
grid on
plot(x,sin(2*x),'r--')
title('Multi sine plot')
ylabel('y-axis')
xlabel('x-axis')
legend('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
Prepared by:Hayat Wali
Iqra University
Page 27
x=[10000,10000];
y=[1000,1000];
loglog(x,y)
4.5
4
3.5
3
2.5
2
3
10
10
10
10
10
10
3
10
10
10
AXIS DEFINING
axis([0 10 0 10])
(xlim 0
10; ylim 0
10)
Page 28
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)
10
9
8
7
6
5
4
3
2
1
0
-10
-8
-6
-4
-2
10
PLOT TOOLS
Prepared by:Hayat Wali
Iqra University
Page 29
3D PLOTTING
Prepared by:Hayat Wali
Iqra University
Page 30
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
4
2
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);
Prepared by:Hayat Wali
Iqra University
Page 31
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.')
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;
Prepared by:Hayat Wali
Iqra University
Page 32
Z=sin(R)./R;
mesh(X,Y,Z)
surf(X,Y,Z)
colormap gray
0.5
-0.5
10
5
10
5
0
0
-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
0
0
-5
-5
-10
-10
[X,Y]=meshgrid(-8:0.5:8);
Prepared by:Hayat Wali
Iqra University
Page 33
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
0
0
-5
-5
-10
-10
x=[7 3 9 2 11 15 20 7 5 9];
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);
Prepared by:Hayat Wali
Iqra University
Page 34
15
10
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
NUMERICAL ANALYSIS
Prepared by:Hayat Wali
Iqra University
Page 35
syms t
f=@(t,y)2.*y-1
f=
@(t,y)2.*y-1
ode45(f,[0,1],1)
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)
2.5
x 10
14
1.5
0.5
0.1
0.2
0.3
0.4
0.5
0.6
0.7
f=@(t,y)2.*y^3-1;
ode45(f,[-1,1],-1)
Page 36
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
Page 37
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)
Page 38
-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)
Page 39
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)
ans =
cos(x)
syms x
diff(sin(x),x,2)
ans =
-sin(x)
syms x
diff(sin(x),x,3)
ans =
-cos(x)
Prepared by:Hayat Wali
Iqra University
Page 40
syms x
diff(sin(x),x,4)
ans =
sin(x)
SUBSTITUTING VALUES
This is the shortcut command for substituting values in any function.
Subs(diff(f(x)),x,?)
syms x
syms x
diff(tan(x))
ans =
1+tan(x)^2
subs(ans,x,2)
ans =
5.7744
syms x
subs(diff(tan(x)),x,2)
ans =
5.7744
syms x
subs(diff(sin(x)),x,1)
ans =
0.5403
syms x
subs(diff(cos(x)),x,36)
ans =
0.9918
syms x
diff(tan(x^6-3*x+5))
ans =
(1+tan(x^6-3*x+5)^2)*(6*x^5-3)
Prepared by:Hayat Wali
Iqra University
(Differentiate Tanx)
(Differentiate Tan(x^6-3*x+5))
Page 41
subs(ans,x,3/2)
ans =
69.9149
subs(diff(tan(x^6-3*x+5)),x,3/2)
ans =
69.9149
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
quad8(inline(sin(x^5+x^3)'),0,pi/2)
/2) is limit
ans =
0.2910
Prepared by:Hayat Wali
Iqra University
Page 42
RELATIONAL OPERATION
x=[1 2;2 3;5 6]
x=
1
2
5
2
3
6
x>2
ans =
0
0
1
x>1
ans =
0
1
1
3>1
ans =
1
3<1
ans =
0
3<=6
ans =
1
3==4
Page 43
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 =
2
x([2 3 4])
ans =
2
x(2)
ans =
2
x(3)
ans =
5
x(x>2)
ans =
5
3
6
x(x>1)
ans =
Prepared by:Hayat Wali
Iqra University
Page 44
2
5
2
3
6
POLYNOMIAL EQUATIONS
Polynomial equations are derived from word Poly means many.
1.0000
plot(x,y,'o',x,fit(1)*x+fit(2))
22
20
18
16
14
12
10
8
6
4
2
10
12
14
16
18
20
In MATLAB
Page 45
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
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
Page 46
MATRICES
Manual
A =[2 3;1 4]
IN MATLAB
A=[2 3;1 4];
A^-1
A=
2
1
3
4
ans =
0.8000 -0.6000
-0.2000 0.4000
Inverse of A:
Adj A = 4
-3
-1
|A| = (4x2)-(-1x-3)
|A| = 8-3
|A| = 5
A-1 = 4 -3-1 25
Prepared by:Hayat Wali
Iqra University
Page 47
A-1 =
0.8000 -0.6000
-0.2000
0.4000
Manual
IN MATLAB
A=[2 3;1 4]
A=
B=[9;3];
2
1
3
4
B=[9;3]
Prepared by:Hayat Wali
Iqra University
X=A^-1*B
X=
Page 48
B=
5.4000
9
-0.6000
X =[X1;X2]
X=
X1
X2
AX=B
X=A-1 B
X=
X1= 5.4000
X2= -0.6000
Page 49
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.
Page 50
Example:
Assigning Comments:
Page 51
Solution is:
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
10
Page 52
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
9
9
16
16
25
16
25
36
Page 53
1
a=
1
a=
1
a=
1
16
25
36
49
16
25
36
49
64
16
25
36
49
64
81
16
25
36
49
64
81 100
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
Page 54
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:
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
10
By pressing 3:
1
0.8
0.6
By pressing 4:
0.4
0.2
0
-0.2
Prepared
by:Hayat Wali
-0.4
Iqra University
-0.6
Page 55
-0.8
-1
10
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
10
10
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
Page 56
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).
Page 57
10
11
Page 58
1
0 .9
0 .8
0 .7
0 .6
0 .5
0 .4
0 .3
0 .2
0 .1
0
10
11
10
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
Page 59
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
0
10
1000
800
600
400
200
10
10
20
30
40
50
60
70
80
90
100
Page 60
2.5
x 10
1.5
0.5
10
10
CONVOLUTION
Method #1(On command window)
X[n] =0.5n u[n]
H[n]=1
Y[n]=?
0n4
Input:
n=0:10;
x=0.5.^n;
stem(n,x)
Prepared by:Hayat Wali
Iqra University
Page 61
Input x[n]
9
8
7
6
5
4
3
2
1
0
.0
1
2
3
4
5
6
7
8
9
Impulse Response:
h=ones(1,5);
h=[h ones(1,5)];
stem(h)
Convolution:
y=conv(x,h);
stem(y)
Page 62
8
6
4
0
1
2
Y[n]
2
4
6
8
.0
2
4
6
8
LAPLACE TRANSFORM
The laplace transform of a signal x(t),
Prepared by:Hayat Wali
Iqra University
Page 63
S2+2S-3=0
S2+3S-S-3 =
S2-1
S2=1
S= 1
=0
S (S +3)-1(S +3)
(S -1)( S +3) = 0
S=1;S=-3
=0
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
Output
Input
Transfer function
Zeros
Page 64
P1=roots(i);
pzmap(Z1,P1)
Poles
S-plane Map
Po le - Z e ro Ma p
1
0.8
0.6
0.4
Imaginary Axis
0.2
0
- 0 .2
- 0 .4
- 0 .6
- 0 .8
-1
-3
-2 .5
-2
-1 .5
-1
- 0 .5
0 .5
Re a l A x is
Page 65
o=[1 0 -1];
i=[1 2 -3];
h=tf(o,i);
Z1=roots(o);
P1=roots(i);
pzmap(Z1,P1)
sgrid
Output
Input
Transfer function
Zeros
Poles
S-plane Map
Po le - Z e ro Ma p
1
0 .92
0.8
0.8 6
0 .7 6
1 .5
0.8 6
0 .7 6
0 .5 8
0 .35
0 .9 6
0.6
0 .9 8 4
0.4
Imaginary Axis
0.2
0 .9 9 6
3
0
- 0 .2
2 .5
0 .5
0 .9 9 6
- 0 .4
0 .9 8 4
- 0 .6
- 0 .8
0 .9 6
0 .92
-1
-3
-2 .5
-2
-1 .5
0 .5 8
-1
0 .35
- 0 .5
0 .5
Re a l A x is
Page 66
t=0:1/8192:1;
x=cos(2*pi*400*t);
soundsc(x,8198)
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
Prepared by:Hayat Wali
Iqra University
Page 67
Phase difference
dxdy=(-2x+1) dx
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
Page 68
Page 69