hayatwali.weebly.comhayatwali.weebly.com/uploads/5/8/3/5/5835489/basic_m… · web viewbasic...
TRANSCRIPT
BASIC MATLAB OPERATIONSFOR 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
FOR ELEMENTS’ MULTIPLICATION:*SQUARE MATRIX = SQUARE MATRIX
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
FOR DIVISION:*NO.OF ROWS = NO.OF COLUMNS
a=[1 2:2 1]; (2 ROWS) b=[1 2]; (2 COLUMNS) c= b\ac = 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 0 0 0.1429 0.2857 0.5714 1.0000 0 0 0 0
Prepared by:Hayat WaliIqra University Page 1
c=a\bc =
0 0 0 0 0 0 0 0 0 0 0 0 0.2857 0.5714 1.0000 0.7143
POWER OF ELEMENTS:
a=[1 2 3 4];
b=(a.^2)b=1 4 9 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+1ans =2 5 3 6
a-1ans = 0 3 1 4
a*1ans =1 4 2 5
a/1ans =1 4 2 5
a\1ans = 0 0 0 0.2000
Prepared by:Hayat WaliIqra University Page 2
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 =
17INVERSE:
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
Prepared by:Hayat WaliIqra University Page 3
MEAN:
a=[2 4;5 6];
mean(a)ans =
3.5000 5.0000STD:
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 8 9 7
FOR MINIMUM ROW:
a=[1 2 3 4;4 5 6 7;7 8 9 7]; min(a)ans =
1 2 3 4
Prepared by:Hayat WaliIqra University Page 4
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 5 8
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
Prepared by:Hayat WaliIqra University Page 5
SIZE OF MATRIX:
a=[1 2 4 7;4 5 8 7;4 1 4 4];
size(a) ans =
3 4
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 7 4 4
Prepared by:Hayat WaliIqra University Page 6
PLOTING IN MATLAB
Asin(wt+ θ) (w=2πf)A=Amplitude (f=w/2π)w=Angular frequencyt=Timeθ=Phase Differences
x=[1 2 4 5 7];y=[4 7 8 5 8];plot(x,y)
1 2 3 4 5 6 74
4.5
5
5.5
6
6.5
7
7.5
8
X-axis
Y-A
xis
x=[1 2 4 5 7];y=[4 7 8 5 8];plot(y,x)
4 4.5 5 5.5 6 6.5 7 7.5 81
2
3
4
5
6
7
Y=Axis
X-A
xis
Prepared by:Hayat WaliIqra University Page 7
t=[pi*(0:0.02:2)];y=sin(t);plot(y)
0 20 40 60 80 100 120-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=sin(t+pi/2);plot(y)
t=[pi*(0:0.02:2)];plot(t,sin(t))
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Prepared by:Hayat WaliIqra University Page 8
0 20 40 60 80 100 120-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))
0 1 2 3 4 5 6 7-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)
0 20 40 60 80 100 120-3
-2
-1
0
1
2
3
t=[pi*(0:0.02:2)];y=2*sin(6*t+pi);plot(y)
0 20 40 60 80 100 120-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
Prepared by:Hayat WaliIqra University Page 9
t=[pi*(0:0.02:2)];y=2*cos(2*t+0);plot(y)
0 20 40 60 80 100 120-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
t=[pi*(0:0.02:2)];plot(t,sin(t))
0 1 2 3 4 5 6 7-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))
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Prepared by:Hayat WaliIqra University Page 10
t=[pi*(0:0.02:2)];plot(t,sinc(t))
0 1 2 3 4 5 6 7-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
t=[pi*(0:0.02:2)];plot(t,exp(t))
0 1 2 3 4 5 6 70
100
200
300
400
500
600
t=[pi*(0:0.02:10)];plot(t,sawtooth(t))
0 5 10 15 20 25 30 35-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Prepared by:Hayat WaliIqra University Page 11
x=[-5:0.0001:5];y=x.^2;plot(y)
0 2 4 6 8 10 12
x 104
0
5
10
15
20
25
x=[-5:0.0001:5];y=x.^3;plot(y)
0 2 4 6 8 10 12
x 104
-150
-100
-50
0
50
100
150
x=linspace(-5,5);y=sinc(x);plot(x,y)
-5 -4 -3 -2 -1 0 1 2 3 4 5-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Prepared by:Hayat WaliIqra University Page 12
PLOTINGWITH COLOURS
t=[0:0.0001:2*pi];
plot(t,sin(t),'k')
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1With Black
plot(t,sin(t),'g')
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1With Green
Prepared by:Hayat WaliIqra University Page 13
plot(t,sin(t),'b')
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1With Blue
plot(t,cos(t),'r')
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1With Red
Prepared by:Hayat WaliIqra University Page 14
PLOTINGWITH DESIGNS & COLOURS
x=[0:0.1:2*pi];
plot(x,sin(x),'o-')
plot(x,sin(x),'g+')
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Prepared by:Hayat WaliIqra University Page 15
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
plot(x,sin(x),'kO')
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
plot(x,cos(x),'R^')
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Prepared by:Hayat WaliIqra University Page 16
plot(x,cos(x),'kd')
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
plot(x,cos(x),'r+')
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Prepared by:Hayat WaliIqra University Page 17
DIFFERENT TYPES OF PLOTING
t=[0:0.001:1]’;plot([t t.^2 t.^3])
0 200 400 600 800 1000 12000
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
t=[0:0.001:1]';plot([t,sin(t),cos(t)])
0 200 400 600 800 1000 12000
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Prepared by:Hayat WaliIqra University Page 18
t=[0:0.001:1]’;plot(t,[sin(t) cos(t)])
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
x=0:0.001:2*pi;fill(x,sin(x),'g')
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Prepared by:Hayat WaliIqra University Page 19
fs=10000;t=0:1/fs:1/5;y=sawtooth(2*pi*5*t);plot(t,y)
0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
t=0:0.00001:10;y=sawtooth(2*pi*3*t*3);plot(t,y)
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Prepared by:Hayat WaliIqra University Page 20
t=0:0.00001:10;y=sawtooth(t,.5);plot(t,y)
0 1 2 3 4 5 6 7 8 9 10-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))
0 10 20 30 40 50 60 70 80 90 1000
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Prepared by:Hayat WaliIqra University Page 21
t=0:0.0001:100;plot(t,square(t))
0 10 20 30 40 50 60 70 80 90 100-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
t=0:0.0001:100;y=square(t,80);fill(t,y,'r')
0 10 20 30 40 50 60 70 80 90 100-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
t=0:0.0001:100;y=square(t,100);fill(t,y,'g')y=square(t,40);fill(t,y,'r')
0 10 20 30 40 50 60 70 80 90 100-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Prepared by:Hayat WaliIqra University Page 22
PLOTING WITH (Sine & Exponential)
t=[0:0.01:2*pi];y=exp(sin(t));plotyy(t,y,t,y,'plot','stem')
0 1 2 3 4 5 6 7151
151.5
152
152.5
153
153.5
0 1 2 3 4 5 6 7151
151.5
152
152.5
153
153.5
0 1 2 3 4 5 6 7151
151.5
152
152.5
153
153.5
151 151.5 152 152.5 153 153.50
1
2
3
4
5
6
7
151 151.5 152 152.5 153 153.50
1
2
3
4
5
6
7
0 1 2 3 4 5 6 7151
151.5
152
152.5
153
153.5
0 1 2 3 4 5 6 70
0.5
1
1.5
2
2.5
3
0 1 2 3 4 5 6 70
0.5
1
1.5
2
2.5
3
t=[0:0.1:2*pi];y=exp(sin(t));plotyy(t,y,t,y,'plot','stem')
0 1 2 3 4 5 6 7151
151.5
152
152.5
153
153.5
0 1 2 3 4 5 6 7151
151.5
152
152.5
153
153.5
0 1 2 3 4 5 6 7151
151.5
152
152.5
153
153.5
151 151.5 152 152.5 153 153.50
1
2
3
4
5
6
7
151 151.5 152 152.5 153 153.50
1
2
3
4
5
6
7
0 1 2 3 4 5 6 7151
151.5
152
152.5
153
153.5
0 1 2 3 4 5 6 70
0.5
1
1.5
2
2.5
3
0 1 2 3 4 5 6 70
0.5
1
1.5
2
2.5
3
Prepared by:Hayat WaliIqra University Page 23
0 0.5 10
0.2
0.4
0.6
0.8
1
(3,3,3)
0 0.5 10
0.5
1(3,3,1)
0 0.5 10
0.5
1(3,3,2)
0 0.5 10
0.5
1
(3,3,4)
0 0.5 10
0.5
1(3,3,5)
0 0.5 10
0.5
1
SUBPLOTTINGFor plotting many Figures in a single figure
x=linspace(0,2*pi); (linspace is used for equal spacing b/w each number)subplot(2,2,1) (2Rows, 2columns & 1st fig)
x=linspace(0,2*pi);
subplot(3,3,1) (3Rows, 3columns & 1st fig)subplot(3,3,2) (3Rows, 3columns & 2st fig) subplot(3,3,3) (3Rows, 3columns & 3rd fig) subplot(3,3,4) (3Rows, 3columns & 4th fig) subplot(3,3,5) (3Rows, 3columns & 5th fig)
Prepared by:Hayat WaliIqra University Page 24
x=linspace(0,2*pi);
subplot(3,3,1) (3Rows, 3columns & 1st fig)subplot(3,3,2) (3Rows, 3columns & 2st fig) subplot(3,3,3) (3Rows, 3columns & 3rd fig) subplot(3,3,4) (3Rows, 3columns & 4th fig) subplot(3,3,5) (3Rows, 3columns & 5th fig)
plot(x,sin(x))
0 2 4 6 8-1
-0.5
0
0.5
1
0 0.5 10
0.2
0.4
0.6
0.8
1
0 0.5 10
0.2
0.4
0.6
0.8
1
0 0.5 10
0.2
0.4
0.6
0.8
1
x=[-10:0.01:10];plot(x,exp(x))grid on (grid on is used for lining in graph)hold on (hold on is used for holding a figure for all graphs)plot(x,exp(0.95*x))plot(x,exp(0.85*x))
-10 -8 -6 -4 -2 0 2 4 6 8 100
0.5
1
1.5
2
2.5x 10
4
Prepared by:Hayat WaliIqra University Page 25
PLOTTING WITH COLOURS, TITLES, & LABELS
x=[-10:0.01:10];plot(x,sin(x))hold ongrid onplot(x,sin(2*x),'r--')title('Multi sine plot') (‘title’ is used for assigning a Title)ylabel('y-axis') (‘ylabel’ is used for assigning Y-Label) xlabel('x-axis') (‘xlabel’ is used for assigning X-Label) legend('SinX','Sine2X') (‘legend’ is used for assigning separate notations for graphs)
-10 -8 -6 -4 -2 0 2 4 6 8 10-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1Multi sine plot
y-ax
is
x-axis
SinXSine2X
Prepared by:Hayat WaliIqra University Page 26
SEMI-LOG PLOTTING
x=[1000 10000 100000];y=[2 4 6];semilogx(x,y)
x=[10000,10000];y=[1000,1000];loglog(x,y)
103
104
105
102
103
104
Prepared by:Hayat WaliIqra University Page 27
103
104
105
2
2.5
3
3.5
4
4.5
5
5.5
6
AXIS DEFINING
axis([0 10 0 10]) (xlim 0 10; ylim 0 10)
0 1 2 3 4 5 6 7 8 9 100
1
2
3
4
5
6
7
8
9
10
axis([0 4 0 1]) (xlim 0 4; ylim 0 1)
0 0.5 1 1.5 2 2.5 3 3.5 40
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
axis([-10 10 0 10]) (xlim -10 10; ylim -10 10)
-10 -8 -6 -4 -2 0 2 4 6 8 100
1
2
3
4
5
6
7
8
9
10
Prepared by:Hayat WaliIqra University Page 28
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 WaliIqra University Page 29
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 leftlighting phong
01
23
4
0
2
4
6
8-1
-0.5
0
0.5
1
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))
01
23
4
0
2
4
6
8-1
-0.5
0
0.5
1
Prepared by:Hayat WaliIqra University Page 30
x=pi*(0:0.05:1);y=2*x;[X,Y]=meshgrid(x,y);plot(X(:),Y(:),'k.')surf(x,y,sin(X))
01
23
4
02
4
6
80
0.2
0.4
0.6
0.8
1
x=pi*(0:0.05:1);y=2*x;[X,Y]=meshgrid(x,y);plot(X(:),Y(:),'k.')surf(x,y,cos(X.^2))
01
23
4
0
2
4
68
-1
-0.5
0
0.5
1
Prepared by:Hayat WaliIqra University Page 31
[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
-10-5
05
10
-10
-5
0
5
10-0.5
0
0.5
1
[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
-10-5
05
10
-10
-5
0
5
10-0.5
0
0.5
1
Prepared by:Hayat WaliIqra University Page 32
[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
-10-5
05
10
-10
-5
0
5
10-0.5
0
0.5
1
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)
0.2
0.4
0.6
0.8
1
30
210
60
240
90
270
120
300
150
330
180 0
Prepared by:Hayat WaliIqra University Page 33
x=rand([1 100]);hist(x,10);
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
5
10
15
Prepared by:Hayat WaliIqra University Page 34
NUMERICAL ANALYSIS
syms t f=@(t,y)2.*y-1
f =
@(t,y)2.*y-1ode45(f,[0,1],1)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 11
1.5
2
2.5
3
3.5
4
4.5
f=@(t,y)2.*y^2-1ode45(f,[0,1],1)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.70
0.5
1
1.5
2
2.5x 10
14
Prepared by:Hayat WaliIqra University Page 35
f=@(t,y)2.*y^3-1;ode45(f,[-1,1],-1)
-1 -0.95 -0.9 -0.85 -0.8 -0.75-14
-12
-10
-8
-6
-4
-2
0x 10
6
f=@(t,y)2.*y-23;ode45(f,[0,1],1)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-70
-60
-50
-40
-30
-20
-10
0
10
f=@(t,y)2.*y-2;ode45(f,[0,1],1)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
Prepared by:Hayat WaliIqra University Page 36
f=@(t,y)2.*y-2;ode45(f,[-1,1],-1)
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1-120
-100
-80
-60
-40
-20
0
f=@(t,y)2.*y-23;ode45(f,[-1,1],1)
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1-600
-500
-400
-300
-200
-100
0
100
f=@(t,y)2.*y-3;ode45(f,[-1,1],1)
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1-30
-25
-20
-15
-10
-5
0
5
Prepared by:Hayat WaliIqra University Page 37
DIFFERENTIATION
Single Derivative:
syms xg=sin(x);
diff(g) d(g)/dx=d(sinx)/dx=Cosx ans = cos(x) diff(x) d(x)/dx=1 ans = 1
Single Derivative:
syms x g=sin(x); g=sin(x) diff(g,x) d(g)/dx=d(sinx)/dx=Cosx
ans = cos(x)
g=cos(x); diff(g,x) ans = -sin(x)
Substitute Values (In Radian):
syms xg=sin(x);diff(g,x) ans =
cos(x) subs(ans,x,2.1) cosx=cos(2.1)ans =
-0.5048
Prepared by:Hayat WaliIqra University Page 38
Double Derivative:
syms x g=sin(x); g=sin(x) diff(g,x,2) 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)
Prepared by:Hayat WaliIqra University Page 39
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 xdiff(tan(x)) (Differentiate Tanx) ans = 1+tan(x)^2 subs(ans,x,2) (Putting x=2 in the answer) ans = 5.7744
syms xsubs(diff(tan(x)),x,2) (Putting x=2 after differentiate Tanx)ans = 5.7744
syms xsubs(diff(sin(x)),x,1) (Putting x=1 after differentiate Sinx)ans = 0.5403
syms xsubs(diff(cos(x)),x,36) (Putting x=36 after differentiate Cosx)ans = 0.9918
syms xdiff(tan(x^6-3*x+5)) (Differentiate Tan(x^6-3*x+5))ans = (1+tan(x^6-3*x+5)^2)*(6*x^5-3) subs(ans,x,3/2) (Putting x=3/2 in the answer)ans = 69.9149
subs(diff(tan(x^6-3*x+5)),x,3/2) (Putting x=3/2 after differentiating Tan(x^6-3*x+5) ) ans = 69.9149
Prepared by:Hayat WaliIqra University Page 40
INTEGERATION
syms x int(sin(x),x) (Integrate sin(x) ) ans =
-cos(x)
syms x int(x*sin(x),x) (Integrate xsin(x)) ans = sin(x)-x*cos(x)
DOUBLE INTEGERATION:
double(int(sin(x^5+x^3),x,0,pi/2)) (Integrate sin(x^5+x^3) ) & (0-π/2) is limit ans =
0.2910
quad8(inline(sin(x^5+x^3)'),0,pi/2) (Integrate sin(x^5+x^3) ) & (0-π/2) is limit ans =
0.2910
quad8(inline(sin(x^5+x^3)'),0,pi/2) (Integrate sin(x^5+x^3) ) & (0-π/2) is limit ans =
0.2910
Prepared by:Hayat WaliIqra University Page 41
RELATIONAL OPERATION
x=[1 2;2 3;5 6]
x =
1 2 2 3 5 6
x>2 (x>2 shows (1) the areas where x is greater than 2 otherwise 0)ans =
0 0 0 1 1 1
x>1 (x>1 shows (1) the areas where x is greater than 1 otherwise 0)ans =
0 1 1 1 1 1
3>1 ans =
1
3<1ans =
0
3<=6ans =
1
3==4ans =
0
3<=3ans =
1
Prepared by:Hayat WaliIqra University Page 42
POSITION/REFERENCE LOCATORCOLOUMN WISE
x=[1 2;2 3;5 6]
x = 1 2 2 3 5 6 x([2]) (Locating position at 2 in column)ans =
2 x([2 3 4]) (Locating position at 2,3,4 in column)ans =
2 5 2
x(2) (Locating position at 2 in column)ans =
2
x(3) (Locating position at 3 in column)ans =
5
x(x>2) (Shows all values that are greater than 2)ans =
5 3 6
x(x>1) (Shows all values that are greater than 1)ans =
2 5 2 3
Prepared by:Hayat WaliIqra University Page 43
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))
0 2 4 6 8 10 12 14 16 18 202
4
6
8
10
12
14
16
18
20
22
Finding Polynomial Equation by Roots:
If roots are:x= +3x= -1
We use the command POLY to converts the roots into polynomial.
Manually: In MATLAB
E.g.: (x-3)(x+1) a=[3;-1]x2 + x - 3x - 3= 0 poly(a)x2 -2x -3 = 0 ans = 1 -2 -3 x2 -2x -3 = 0
Prepared by:Hayat WaliIqra University Page 44
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: In MATLAB
x2-2x-3 = 0 p=[1 -2 -3];x2+x -3x-3 = 0 roots(p) (x-3)(x+1)=0 ans = x= +3 +3 x= -1 -1 OR roots([1 -2 -3]) ans = +3 -1
Evaluate the Polynomial:
If the Polynomial Equation is:
F(x)=x3+6x-3=0F(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
Prepared by:Hayat WaliIqra University Page 45
MATRICES
Manual IN MATLABA =[2 3;1 4]A =
2 3 1 4
Inverse of A:
A-1 ¿ Adj A¿ A∨¿¿
Adj A = 4 -3 -1 2
|A| = (4x2)-(-1x-3)|A| = 8-3|A| = 5
A-1 = 4−3−12
5
A-1 = 0.8000 -0.6000
-0.2000 0.4000
A=[2 3;1 4];A^-1
ans =
0.8000 -0.6000 -0.2000 0.4000
Prepared by:Hayat WaliIqra University Page 46
Manual IN MATLAB A=[2 3;1 4]
A =
2 3 1 4
B=[9;3] B =
9 3
X =[X1;X2] X = X1 X2
AX=B X=A-1 B X =
X1= 5.4000 X2= -0.6000
A=[2 3;1 4]; B=[9;3]; X=A^-1*B
X =
5.4000-0.6000
Prepared by:Hayat WaliIqra University Page 47
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.
Prepared by:Hayat WaliIqra University Page 48
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).
Function [x1, x2] =quadratic (a,b,c) [x1,x2] Output Arguments (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:
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Prepared by:Hayat WaliIqra University Page 49
Program 2: (Quadratic Equation Solver):
x=−b±√b2−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
Prepared by:Hayat WaliIqra University Page 50
46 56
Program 4: (Displaying ‘a’ using “for-loop”):
Go to M-file & write algorithm of program. for i=1:10;
a(i)=i*iend
Save it by assigning file name (a4.m). Go to the Matlab command window and type the file name (a4). Solution is:
a1
a =1a =1 4a =1 4 9
a =1 4 9 16a =1 4 9 16 25a =1 4 9 16 25 36a =1 4 9 16 25 36 49a =
1 4 9 16 25 36 49 64a =1 4 9 16 25 36 49 64 81a =1 4 9 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:
a1enter frequency3 (No. of Frequencies are 3)
Prepared by:Hayat WaliIqra University Page 51
enter n.o of cycles3 (No. of Cycles are 3)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
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) breakendend
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:
Prepared by:Hayat WaliIqra University Page 52
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
By pressing 2:
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
By pressing 3:
By pressing 4:
Prepared by:Hayat WaliIqra University Page 53
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
By pressing 5: (Program ended)
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Program 7: (Displaying random numbers):
Go to M-file & write algorithm of program. t=rand(1);
if t>0.75; s=0elseif t<0.25; s=1
Prepared by:Hayat WaliIqra University Page 54
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):
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:10z=1*s;disp('1X');disp(s);disp('=');disp(z);endendif(a==2)for s=1:10z=2*s;disp('2x');disp(s);disp('=');disp(z);endendif(a==3)for s=1:10z=3*s;disp('3x');disp(s);disp('=');disp(z);endendif(a==4)for s=1:10z=4*s;disp('4x');disp(s);disp('=');disp(z);endendif(a==5)breakendend
Save it by assigning file name (a8.m). Go to the Matlab command window and type the file name (a8).
Prepared by:Hayat WaliIqra University Page 55
SIGNALS & SYSTEMSUSING MATLAB
CREATING SIGNALS ( IN DISCREATE TIME ):
Impulse function:
x=[1:11];y=[1 zeros(1,10)];stem(x,y)
1 2 3 4 5 6 7 8 9 10 110
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Unit step function:
x=[1:11];y=[ones(1,11)];stem(x,y)
Prepared by:Hayat WaliIqra University Page 56
1 2 3 4 5 6 7 8 9 10 110
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
x=[1:10];y=[0,0 ones(1,8)];stem(x,y)
1 2 3 4 5 6 7 8 9 100
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Exponential function (Decaying):n=1:10;x=0.5.^n;stem(n,x)
Prepared by:Hayat WaliIqra University Page 57
1 2 3 4 5 6 7 8 9 100
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
0.5
Exponential function (Increasing):n=0:10;x=2.^n;stem(n,x)
0 1 2 3 4 5 6 7 8 9 100
200
400
600
800
1000
1200
CREATING SIGNALS ( IN CONTINUOUS TIME ):
Unit step function:t=ones(1,100);plot(t)
0 10 20 30 40 50 60 70 80 90 1000
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
Exponential function (Increasing):t=1:0.001:10;
Prepared by:Hayat WaliIqra University Page 58
y=exp(t);plot(t,y)
1 2 3 4 5 6 7 8 9 100
0.5
1
1.5
2
2.5x 10
4
Exponential function (Decaying):t=1:0.001:10;y=exp(-t);plot(t,y)
1 2 3 4 5 6 7 8 9 100
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
CONVOLUTION
Method #1 (On command window)
X[n] =0.5 n u[n] H[n]=1 0≤n≤4Y[n]=?
Input:
n=0:10;x=0.5.^n;stem(n,x)
Prepared by:Hayat WaliIqra University Page 59
Input x[n]
0 1 2 3 4 5 6 7 8 9 100
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Impulse Response h[n]
1 2 3 4 5 6 7 8 9 10 110
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
Impulse Response:
h=ones(1,5);h=[h ones(1,5)];stem(h)
Convolution:
y=conv(x,h);stem(y)
Prepared by:Hayat WaliIqra University Page 60
Y[n]
0 2 4 6 8 10 12 14 16 18 200
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
Method #2 (By programming)
LAPLACE TRANSFORMThe laplace transform of a signal x(t),
Prepared by:Hayat WaliIqra University Page 61
X(t) →X(s)=∫−∞
∞
x ( t ) e-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=0S2+3S−S−3 = 0 S (S +3)-1(S +3) = 0 (S -1)(S +3) = 0 S=1; S=−3
IN MATLAB:
For Equation: For Roots:
o=[1 0 -1]; Z1=roots(o) i=[1 2 -3]; Z1 = 1 -1h=tf(o,i) P1=roots(i) Transfer function: P1 = -3 1 s^2 - 1-------------s^2 + 2 s - 3
For plotting S-plane:Prepared by:Hayat WaliIqra University Page 62
o=[1 0 -1]; Output i=[1 2 -3]; Inputh=tf(o,i); Transfer functionZ1=roots(o); ZerosP1=roots(i); Polespzmap(Z1,P1) S-plane Map
-3 -2.5 -2 -1.5 -1 -0.5 0 0.5 1-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1Pole-Zero Map
Real Axis
Imag
inar
y Ax
is
Prepared by:Hayat WaliIqra University Page 63
For plotting S-plane using Sgrid :
o=[1 0 -1]; Output i=[1 2 -3]; Inputh=tf(o,i); Transfer functionZ1=roots(o); ZerosP1=roots(i); Polespzmap(Z1,P1) S-plane Mapsgrid
-3 -2.5 -2 -1.5 -1 -0.5 0 0.5 1-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
10.350.580.760.860.92
0.96
0.984
0.996
0.350.580.760.860.920.96
0.984
0.996
0.511.522.53
Pole-Zero Map
Real Axis
Imag
inar
y Ax
is
For viewing samples of audio file:
Prepared by:Hayat WaliIqra University Page 64
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)
For Noise:
t=0:1/8192:1;x=cos(2*pi*400*t);soundsc(x,8000)noise=randn(8192,1);soundsc(noise,8000)
SIMULINKPrepared by:Hayat WaliIqra University Page 65
X(t)=Acos(wt+θ) A → Gain Cos → Trigonometric function W → Frequency T → Time θ → Phase difference
dxdy
=∫(−2 x+1)dx
∫ dx=∫ (−2 x+1 )dx
X=-2 x2
2+x+c
X=-x2+ x+c
X(k)=e(k-3)+2.2x(k-1)-1.57x(k-2)+0.3x(k-3)For 0≤k≤8
Prepared by:Hayat WaliIqra University Page 66
Prepared by:Hayat WaliIqra University Page 67