basic matlab operations

Upload: anjuakhss3864

Post on 07-Apr-2018

244 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/6/2019 Basic Matlab Operations

    1/69

    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*bc=

    8 1010 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 00.5000 1.0000 0.5000

    FOR ELEMENTS DIVISION:

    a=[1 2 4 7];b=[2 4 7 5 ];

    c=b\ac =

    0 0 0 00 0 0 0

    0.1429 0.2857 0.5714 1.00000 0 0 0

    Prepared by:Hayat WaliIqra University Page 1

  • 8/6/2019 Basic Matlab Operations

    2/69

    c=a\bc =

    0 0 0 00 0 0 0

    0 0 0 00.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 =

    000

    0.2000

    COLUMNS ADDITION:

    Prepared by:Hayat WaliIqra University Page 2

  • 8/6/2019 Basic Matlab Operations

    3/69

    a=[2 4;5 6];sum(a)

    ans =

    7 10

    ROWS ADDITION:

    a=[2 4;5 6];sum(a,2)

    ans =

    611

    ALL ELEMENTS ADDITION:

    a=[2 4;5 6];sum(sum(a))

    ans =

    17INVERSE:

    a=[2 4;5 6];

    inv(a)ans =

    -0.7500 0.50000.6250 -0.2500

    DETERMINATE:

    a=[2 4;5 6];

    det(a)ans =

    -8

    MEAN:

    a=[2 4;5 6];

    mean(a)

    Prepared by:Hayat WaliIqra University Page 3

  • 8/6/2019 Basic Matlab Operations

    4/69

    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

    FOR MAXIMUM ELEMENT:

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

    max(max(a))ans =

    9

    Prepared by:Hayat WaliIqra University Page 4

  • 8/6/2019 Basic Matlab Operations

    5/69

    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 =

    787

    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 4

    SIZE OF ROWS:

    Prepared by:Hayat WaliIqra University Page 5

  • 8/6/2019 Basic Matlab Operations

    6/69

    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 =

    4ALL 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 00 0 0 00 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 74 4

    PLOTING IN MATLAB

    Asin(wt+ ) (w=2f)A=Amplitude (f=w/2)w=Angular frequencyt=Time=Phase Differences

    x=[1 2 4 5 7];

    Prepared by:Hayat WaliIqra University Page 6

  • 8/6/2019 Basic Matlab Operations

    7/69

    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-Axis

    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-Axis

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

    Prepared by:Hayat WaliIqra University Page 7

  • 8/6/2019 Basic Matlab Operations

    8/69

    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

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

    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

  • 8/6/2019 Basic Matlab Operations

    9/69

    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

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

    Prepared by:Hayat WaliIqra University Page 9

  • 8/6/2019 Basic Matlab Operations

    10/69

    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

  • 8/6/2019 Basic Matlab Operations

    11/69

    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))

    Prepared by:Hayat WaliIqra University Page 11

  • 8/6/2019 Basic Matlab Operations

    12/69

    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

    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);

    Prepared by:Hayat WaliIqra University Page 12

  • 8/6/2019 Basic Matlab Operations

    13/69

    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

    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

    1

    With Black

    plot(t,sin(t),'g')

    Prepared by:Hayat WaliIqra University Page 13

  • 8/6/2019 Basic Matlab Operations

    14/69

    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

    With Green

    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

    1

    With Blue

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

    Prepared by:Hayat WaliIqra University Page 14

  • 8/6/2019 Basic Matlab Operations

    15/69

    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

    With Red

    PLOTINGWITH DESIGNS & COLOURS

    x=[0:0.1:2*pi];

    plot(x,sin(x),'o-')

    plot(x,sin(x),'g+')

    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

  • 8/6/2019 Basic Matlab Operations

    16/69

    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^')

    Prepared by:Hayat WaliIqra University Page 16

  • 8/6/2019 Basic Matlab Operations

    17/69

    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),'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+')

    Prepared by:Hayat WaliIqra University Page 17

  • 8/6/2019 Basic Matlab Operations

    18/69

    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

    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]';

    Prepared by:Hayat WaliIqra University Page 18

  • 8/6/2019 Basic Matlab Operations

    19/69

    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

    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')

    Prepared by:Hayat WaliIqra University Page 19

  • 8/6/2019 Basic Matlab Operations

    20/69

    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

    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;

    Prepared by:Hayat WaliIqra University Page 20

  • 8/6/2019 Basic Matlab Operations

    21/69

    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

    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

    Prepared by:Hayat WaliIqra University Page 21

  • 8/6/2019 Basic Matlab Operations

    22/69

    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

    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')

    Prepared by:Hayat WaliIqra University Page 22

  • 8/6/2019 Basic Matlab Operations

    23/69

    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

    PLOTING WITH (Sine & Exponential)

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

    Prepared by:Hayat WaliIqra University Page 23

  • 8/6/2019 Basic Matlab Operations

    24/69

    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

    SUBPLOTTING

    For plotting many Figures in a single figure

    Prepared by:Hayat WaliIqra University Page 24

  • 8/6/2019 Basic Matlab Operations

    25/69

    00

    .

    5

    100

    .

    2

    0

    .

    4

    0

    .

    6

    0

    .

    8

    1

    (3,3,3)00

    .

    5

    100

    .

    5

    1(3,3,1)00

    .

    5

    100

    .

    5

    1(3,3,2)00

    .

    5

    100

    .

    5

    1(3,3,4)00

    .

    5

    100

    .

    5

    1(3,3,5)00

    .

    5

    100

    .

    5

    1

    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)

    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)

    Prepared by:Hayat WaliIqra University Page 25

  • 8/6/2019 Basic Matlab Operations

    26/69

    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 1

    0

    0.2

    0.4

    0.6

    0.8

    1

    0 0.5 1

    0

    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

    PLOTTING WITHCOLOURS, TITLES, & LABELS

    Prepared by:Hayat WaliIqra University Page 26

  • 8/6/2019 Basic Matlab Operations

    27/69

    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-axis

    x-axis

    SinX

    Sine2X

    SEMI-LOG PLOTTING

    Prepared by:Hayat WaliIqra University Page 27

  • 8/6/2019 Basic Matlab Operations

    28/69

    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

    AXIS DEFINING

    axis([0 10 0 10]) (xlim 0 10; ylim 0 10)

    Prepared by:Hayat WaliIqra University Page 28

    103

    104

    105

    2

    2.5

    3

    3.5

    4

    4.5

    5

    5.5

    6

  • 8/6/2019 Basic Matlab Operations

    29/69

    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

    PLOT TOOLS

    Prepared by:Hayat WaliIqra University Page 29

  • 8/6/2019 Basic Matlab Operations

    30/69

    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

    3D PLOTTING

    Prepared by:Hayat WaliIqra University Page 30

  • 8/6/2019 Basic Matlab Operations

    31/69

    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

    0

    1

    2

    3

    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))

    0

    1

    2

    34

    0

    2

    46

    8-1

    -0.5

    0

    0.5

    1

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

    Prepared by:Hayat WaliIqra University Page 31

  • 8/6/2019 Basic Matlab Operations

    32/69

    plot(X(:),Y(:),'k.')surf(x,y,sin(X))

    0

    1

    2

    3

    4

    0

    2

    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))

    0

    1

    2

    3

    4

    0

    2

    4

    6

    8-1

    -0.5

    0

    0.5

    1

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

    Prepared by:Hayat WaliIqra University Page 32

  • 8/6/2019 Basic Matlab Operations

    33/69

    Z=sin(R)./R;mesh(X,Y,Z)surf(X,Y,Z)colormap gray

    -10

    -5

    0

    5

    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

    0

    5

    10

    -10

    -5

    0

    5

    10

    -0.5

    0

    0.5

    1

    [X,Y]=meshgrid(-8:0.5:8);

    Prepared by:Hayat WaliIqra University Page 33

  • 8/6/2019 Basic Matlab Operations

    34/69

    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

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

    Prepared by:Hayat WaliIqra University Page 34

  • 8/6/2019 Basic Matlab Operations

    35/69

    0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

    5

    10

    15

    NUMERICAL ANALYSISPrepared by:Hayat WaliIqra University Page 35

  • 8/6/2019 Basic Matlab Operations

    36/69

  • 8/6/2019 Basic Matlab Operations

    37/69

    -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 37

  • 8/6/2019 Basic Matlab Operations

    38/69

    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)

    Prepared by:Hayat WaliIqra University Page 38

  • 8/6/2019 Basic Matlab Operations

    39/69

    -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

    DIFFERENTIATION

    Single Derivative:

    syms xg=sin(x);

    diff(g) d(g)/dx=d(sinx)/dx=Cosxans =

    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) :

    Prepared by:Hayat WaliIqra University Page 39

  • 8/6/2019 Basic Matlab Operations

    40/69

    syms xg=sin(x);diff(g,x)

    ans =cos(x)subs(ans,x,2.1) cosx=cos(2.1)ans =

    -0.5048

    Double Derivative:

    syms xg=sin(x); g=sin(x)diff(g,x,2) d(g)/dx=d(sinx)/dx

    ans =-sin(x)

    Higher Order Derivatives :

    syms xdiff(sin(x),x,1) (1 Represents for 1st Derivative)

    ans =

    cos(x)

    syms xdiff(sin(x),x,2) (2 Represents for 2nd Derivative)

    ans =

    -sin(x)

    syms xdiff(sin(x),x,3) (3 Represents for 3rd Derivative)

    ans =

    -cos(x)

    Prepared by:Hayat WaliIqra University Page 40

  • 8/6/2019 Basic Matlab Operations

    41/69

    syms xdiff(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 xdiff(tan(x)) (Differentiate Tanx)ans =

    1+tan(x)^2subs(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 x

    subs(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)

    Prepared by:Hayat WaliIqra University Page 41

  • 8/6/2019 Basic Matlab Operations

    42/69

    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

    INTEGERATION

    syms xint(sin(x),x) (Integrate sin(x) )ans =

    -cos(x)

    syms xint(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)) (Integratesin(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 42

  • 8/6/2019 Basic Matlab Operations

    43/69

    RELATIONAL OPERATION

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

    x =

    1 2

    2 35 6

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

    0 00 11 1

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

    0 11 11 1

    3>1ans =

    1

    3

  • 8/6/2019 Basic Matlab Operations

    44/69

    ans =

    0

    32) (Shows all values that are greater than 2)

    ans =

    536

    x(x>1) (Shows all values that are greater than 1)ans =

    Prepared by:Hayat WaliIqra University Page 44

  • 8/6/2019 Basic Matlab Operations

    45/69

    25236

    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

    Prepared by:Hayat WaliIqra University Page 45

  • 8/6/2019 Basic Matlab Operations

    46/69

    E.g.: (x-3)(x+1) a=[3;-1]x2 + x - 3x - 3= 0 poly(a)x2 -2x -3 = 0 ans =

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

    F inding 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 +3x= -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 46

  • 8/6/2019 Basic Matlab Operations

    47/69

  • 8/6/2019 Basic Matlab Operations

    48/69

  • 8/6/2019 Basic Matlab Operations

    49/69

    B =

    9

    3

    X =[X1;X2]

    X =

    X1

    X2

    AX=B

    X=A-1 B

    X =

    X1= 5.4000

    X2= -0.6000

    5.4000

    -0.6000

    Prepared by:Hayat WaliIqra University Page 49

  • 8/6/2019 Basic Matlab Operations

    50/69

    PROGRAM M ING 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 50

  • 8/6/2019 Basic Matlab Operations

    51/69

    Example: Go to M-file & write program.

    clcx=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

    Prepared by:Hayat WaliIqra University Page 51

  • 8/6/2019 Basic Matlab Operations

    52/69

    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

    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:

    Prepared by:Hayat WaliIqra University Page 52

  • 8/6/2019 Basic Matlab Operations

    53/69

    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

    24711162229374656

    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 4

    a =1 4 9a =1 4 9 16a =1 4 9 16 25a =1 4 9 16 25 36a =

    Prepared by:Hayat WaliIqra University Page 53

  • 8/6/2019 Basic Matlab Operations

    54/69

    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 are3)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))

    Prepared by:Hayat WaliIqra University Page 54

  • 8/6/2019 Basic Matlab Operations

    55/69

    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:

    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 55

    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

  • 8/6/2019 Basic Matlab Operations

    56/69

    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 r andom numbers ):

    Go to M-file & write algorithm of program. t=rand(1);

    if t>0.75;s=0

    elseif t

  • 8/6/2019 Basic Matlab Operations

    57/69

    s=1else

    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);endend

    if(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 57

  • 8/6/2019 Basic Matlab Operations

    58/69

    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 58

  • 8/6/2019 Basic Matlab Operations

    59/69

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

    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 59

  • 8/6/2019 Basic Matlab Operations

    60/69

    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

    Prepared by:Hayat WaliIqra University Page 60

  • 8/6/2019 Basic Matlab Operations

    61/69

    Exponential function (Increasing) :t=1:0.001:10;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.5n u[n]

    H[n]=1 0n4

    Y[n]=?

    Input :

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

    Prepared by:Hayat WaliIqra University Page 61

  • 8/6/2019 Basic Matlab Operations

    62/69

  • 8/6/2019 Basic Matlab Operations

    63/69

    Y[n]0246810

    1

    2

    1

    4

    1

    6

    1

    8

    2

    0

    00

    .

    2

    0

    .

    4

    0

    .

    6

    0

    .

    8

    11

    .

    2

    1

    .

    4

    1

    .

    6

    1

    .

    8

    2

    Method #2( By programming )

    LAPLACE TRANSFORM

    The laplace transform of a signal x(t),

    Prepared by:Hayat WaliIqra University Page 63

  • 8/6/2019 Basic Matlab Operations

    64/69

    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

    (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-1

    h=tf(o,i) P1=roots(i)Transfer function: P1 =-31

    s^2 - 1-------------s^2 + 2 s - 3

    For plotting S-plane:

    o=[1 0 -1]; Outputi=[1 2 -3]; Inputh=tf(o,i); Transfer functionZ1=roots(o); Zeros

    Prepared by:Hayat WaliIqra University Page 64

  • 8/6/2019 Basic Matlab Operations

    65/69

    P1=roots(i); Poles pzmap(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

    Rea l A x is

    ImaginaryAxis

    For plotting S-plane using Sgrid:

    Prepared by:Hayat WaliIqra University Page 65

  • 8/6/2019 Basic Matlab Operations

    66/69

    o=[1 0 -1]; Outputi=[1 2 -3]; Inputh=tf(o,i); Transfer functionZ1=roots(o); ZerosP1=roots(i); Poles

    pzmap(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.5

    Pole-Zero Map

    Rea l A x is

    ImaginaryAxis

    For viewing samples of audio file:

    a=wavread(file location',samples)E.g.: a=wavread('C:\WINDOWS\Media\tada',2000)

    MATLAB SOUND:

    Prepared by:Hayat WaliIqra University Page 66

  • 8/6/2019 Basic Matlab Operations

    67/69

    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)

    SIMULINKX(t)=Acos(wt+)

    A GainCos Trigonometric functionW FrequencyT Time

    Prepared by:Hayat WaliIqra University Page 67

  • 8/6/2019 Basic Matlab Operations

    68/69

    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

    Prepared by:Hayat WaliIqra University Page 68

  • 8/6/2019 Basic Matlab Operations

    69/69