MY S & S MANUAL

download MY S & S MANUAL

of 18

Transcript of MY S & S MANUAL

  • 8/3/2019 MY S & S MANUAL

    1/18

    LAB REPORT(01)

    SUBMITTED TOEngineer IMRAN

    SUBMITTED BY

    OSAMA TAHIR

    09-EE-88

    UNIVERSITY OF ENGINEERING AND TECHNOLOGYTAXILA

  • 8/3/2019 MY S & S MANUAL

    2/18

    09-EE-88 2

    BASIC TERMS:

    >> help It will display list of all toolboxes included in MATLAB.

    >> help functionname This command displays a description of the function and generally also includes a list of relatedfunctions

    >> lookfor keyword This command will display a list of functions that include the keyword in their descriptions.Other help commands that you may find useful are info, what, and which

    MATLAB variables

    MATLAB stores variables in the form of matrices, which are M x N , where M is thenumber of rows and N the number of columns.. A matrix is written with a square bracket [ ]with spaces separating adjacent columns and semicolons separating adjacent rows. For example,consider the following assignments of the variable x Real scalar >> x = 5Complex scalar >> x = 5+10j (or >> x = 5+10i)Row vector >> x = [1 2 3] (or x = [1, 2, 3])Column vector >> x = [1; 2; 3]

    3 x 3matrix >> x = [1 2 3; 4 5 6; 7 8 9]

    Complex number operations

    Some of the important operations on complex numbers are illustrated below:Complex scalar >> x = 3+4jReal part of x >> real(x) = 3Imaginary part of x >> imag(x) = 4

    Magnitude of x >> abs(x) = 5Angle of x >> angle(x) = 0.9273Complex conjugate of x >> conj(x) = 3 - 4i

  • 8/3/2019 MY S & S MANUAL

    3/18

    09-EE-88 3

    Generating Vectors

    Vectors can be generated using the : command. For example , to generate a vector x thattakes on the values 0 to 10 in increments of 0.5, type the following, which generates a 1 x 21matrix?

    >> x = [0:0.5:10];

    Matrix Operations

    Addition and subtraction involve element-by-element arithmetic operations; matrixmultiplication and division do not. However, MATLAB provides for element-by-elementoperations as well by prep ending a . before the operator as f ollows:

    .* Multiplication

    ./ Right division

    .\ Left division

    .^ Exponentiation (power)

    . Conjugate transpose

    The difference between matrix multiplication and element-by-element multiplication is seen inthe following example

    >>A = [1 2; 3 4]A =

    1 23 4

    >>B=A*A B =

    7 1015 22

    >>C=A.*A C =

    1 4

    9 16

  • 8/3/2019 MY S & S MANUAL

    4/18

    09-EE-88 4

    Relational Operations

    The following relational operations are defined:

    < Less than Greater than

    >= Greater than or equal to== Equal to~= Not equal to

    PREPARED BYOSAMA TAHIR

    09-EE-88

  • 8/3/2019 MY S & S MANUAL

    5/18

    LAB REPORT(02)

    SUBMITTED TOEngineer IMRAN

    SUBMITTED BY

    OSAMA TAHIR

    09-EE-88

    UNIVERSITY OF ENGINEERING AND TECHNOLOGYTAXILA

  • 8/3/2019 MY S & S MANUAL

    6/18

    09-EE-88 2

    HOW TO DRAW MATRICES

    We can generate 2*3 MATRIX simply

    >> A = [5 7 9;1 -3 -7]

    A = 5 7 91 -3 -7

    SIZE

    We can get the size (dimensions) of a matrix with the command size>> size (A),

    ans =2 3

    Transpose

    Transposing a vector changes it from a row to a column vector and vice versa.

    >> D, D D =

    1 2 3 4 56 7 8 9 1011 13 15 17 19

    ans =

    1 6 112 7 133 8 154 9 175 10 19

    >> size (D), size(D) ans =

    3 5ans =

    5 3

    Special Matrices

    MATLAB provides a number of useful built in matrices of any desired size. Ones (m, n) give anmxn matrix of 1s,

    >> P =ones (2, 3)

  • 8/3/2019 MY S & S MANUAL

    7/18

    09-EE-88 3

    P =1 1 11 1 1

    zeros (m,n) gives an m n matrix of 0s,

    Dot Product of Matrices (.*)

    The dot product works as for vectors: corresponding elements are multiplied together so thematrices involved must have the same size.

    >> A, BA =

    5 7 91 -3 -7

    B =-1 2 59 0 5

    >> A.*Bans =

    -5 14 459 0 -35

    Matrix Matrix ProductsTo form the product of an m n matrix A and a np matrix B, written as AB, we

    visualize the first matrix (A) as being composed of m row vectors of length n stacked on top of each other while the second (B) is visualized as being made up of p column vectors of length n:

    (m n) times (n p) = (m p). Check that you understand what is meant by working out thefollowing examples by hand and comparing with the MATLAB answers.

    >> A = [5 7 9; 1 -3 -7]A =

    5 7 91 -3 -7

    >> B = [0, 1; 3, -2; 4, 2]

    B =0 1

    3 -24 2

    >> C = A * BC =

    57 9-37 -7

  • 8/3/2019 MY S & S MANUAL

    8/18

    09-EE-88 4

    >> D = B * AD =

    1 -3 -713 27 4122 22 22

    SOME O THER O PERATIONS

    fliplr Flip matrices left-rightflipud Flip matrices up-downrepmat Replicate and tile an arrayreshape Reshape array

    WHOS:whos lists the current variables and their sizes and types. It also reports the totals for sizes.EXAMPLE: Given the following arrays

    A=rand(10,20,30,40);B=rand(5,10,15,20,25,'single')*i;

    using "whos" displays the following:

    >> whosName Size Bytes Class Attributes

    A 4-D 1920000 doubleB 5-D 3000000 single complex

    PREPARED BYOSAMA TAHIR

    09-EE-88

  • 8/3/2019 MY S & S MANUAL

    9/18

    LAB REPORT(03)

    SUBMITTED TOSir IMRAN

    SUBMITTED BY

    OSAMA TAHIR

    09-EE-88

    UNIVERSITY OF ENGINEERING AND TECHNOLOGYTAXILA

  • 8/3/2019 MY S & S MANUAL

    10/18

    09-EE-88 2

    How to plot???

    Suppose we wish to plot a graph of y = sin3x for 0 x 1. We do this by sampling the

    function at a sufficiently large number of points and then joining up the points (x, y) by straightlines. Suppose we take N+1 points equally spaced a distance h apart:

    >> N =10; h =1/N; t = 0: h: 1;defines the set of points x = 0, h, 2h, . . ., 9h, 1. The corresponding y values are computed by>> y =sin(3*pi*t);and finally, we can plot the points with>> plot (t, y)The result is shown in Figure 1, where it is clear that the value of N is too small.

    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

    Plotting Titles & Labels:

    To put a title and label the axes, we use>> title (Graph of y =sin(3pi t))

    >> xlabel (t -axis (time)) >> ylabel (y -axis)

    Grids:A dotted grid may be added by>> grid

  • 8/3/2019 MY S & S MANUAL

    11/18

    09-EE-88 3

    Line Styles & Colors:

    The default is to plot solid lines. A solid white line is produced by>> plot (t, y, w -)

    Multi plotsSeveral graphs can be drawn on the same figure as:>> plot (t, y, w -,t, cos (2*pi*t), g --)

    legendA descriptive legend may be included with>> legend (Sin curve, Cos curve)

    EXAMPLE>> plot (t, y, w -, t, cos (2*pi*t), g --) >> legend (Sin curve, Cos curve) >> title (Multi - plot ) >> xlabel (x axis(time)), ylabel (y axis) >> grid

    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

    x axis

    y axis

    Multi-plot

    Hold

    A call to plot clears the graphics window before plotting the current graph.. To stop thewindow being cleared:>> plot (t, y, w -), hold >> plot (t, y, gx), hold off hold on holds the current picture; hold off releases it (but does not clear the window, which

  • 8/3/2019 MY S & S MANUAL

    12/18

    09-EE-88 4

    can be done with clf). hold on its own toggles the hold state.

    EXAMPLE

    The graphics window may be split into an m n array of smaller windows into which we

    may plot one or more graphs. The windows are counted 1 to mn row wise, starting from thetop left. Both hold and grid work on the current subplot.>> subplot (221), plot (t,y)>> xlabel (x), ylabel (sin 3 pi t) >> subplot (222), plot (t, cos (3*pi*t))>> xlabel (x), ylabel (cos 3 pi t) >> subplot (223), plot (t, sin (6*pi*t))>> xlabel (x), ylabel (sin 6 pi t) >> subplot (224), plot (t, cos (6*pi*t))>> xlabel (x), ylabel (cos 6 pi t)

    subplot(221) (or subplot(2,2,1)) specifies that the window should be split into a 2 2 array andwe select the first sub-window.

    0 0.5 1-1

    -0.5

    0

    0.5

    1

    x

    sin 3 pi x

    0 0.5 1-1

    -0.5

    0

    0.5

    1

    x

    cos 3 pi x

    0 0.5 1-1

    -0.5

    0

    0.5

    1

    x

    sin 6 pi x

    0 0.5 1-1

    -0.5

    0

    0.5

    1

    x

    cos 6 pi x

    PREPARED BYOSAMA TAHIR

    09-EE-88

  • 8/3/2019 MY S & S MANUAL

    13/18

    09-EE-88 5

  • 8/3/2019 MY S & S MANUAL

    14/18

    LAB REPORT(04)

    SUBMITTED TO

    Sir IMRAN

    SUBMITTED BY

    OSAMA TAHIR

    09-EE-88

    UNIVERSITY OF ENGINEERING AND TECHNOLOGYTAXILA

  • 8/3/2019 MY S & S MANUAL

    15/18

    09-EE-88 2

    LoopS

    FOR LOOPThe for loop allows us to repeat certain commands. If you want to repeat some action in apredetermined way, you can use the for loop . All of the loop structures in matlab are started with

    a keyword such as "for", or "while" and they all end with the word "end". Another deep thought,eh.

    Conditional Statements

    Conditional statements enable you to select at run time which block of code to execute.There aretimes when you want certain parts of your program to be executed only in limited circumstances.The way to do that is to put the code within an "if" statement.

    if (condition statement)

    (matlab commands)end

    elseif and else are optional, and execute statements only when previous expressions in the if block are false. An if block can include multiple elseif statements.

    if (condition statement) (matlab commands)

    elseif (condition statement)

    (matlab commands)elseif (condition statement)

    (matlab commands)...else

    (matlab commands)end

    EVEN , ODD FUNCTIONS/SIGNALS

    Even Signal: A signal is referred to as an even if it is identical to its time-reversed counterparts; x(t) = x(-t).

  • 8/3/2019 MY S & S MANUAL

    16/18

    09-EE-88 3

    Odd Signal: A signal is odd if x(t) = -x(-t).

    Three possible time transformations:

    Time Flip (or reverse) : x(-t), x[-n]

    Flips the signal over the vertical axis.

    Time Shift : x(t+a), x[n+a]

    On horizontal axis, shifts to the right when a0.

    Time Scale : x(at), x[an] for a>0.

    On horizontal axis, scales the signal length down when a>1, scales it up whena

  • 8/3/2019 MY S & S MANUAL

    17/18

    09-EE-88 4

    end end

    Output

    EXAMPLES

    function [even,odd]=evenoddfunc(signal,time)rev_sig=fliplr(signal);rev_time=fliplr(time);revtime= -1*rev_time;even=0.5*(signal+rev_sig);odd=0.5*(signal-rev_sig);

    subplot(5,1,1)stem(time,signal, 'r' );title( 'original' );

    subplot(5,1,2);stem(revtime,rev_sig);title ( 'reversal' );

    subplot(5,1,3);stem(time,odd);title( 'odd' );

    subplot(5,1,4);stem(time,even);

    title( 'even' );

    subplot(5,1,5);stem(time,(even+odd));title( 'restored signal' );

  • 8/3/2019 MY S & S MANUAL

    18/18

    09-EE-88 5

    save it by the name of the function (evenoddfunc) & then open a new m-file

    and write the code below for to simulate the code and to show output .

    inc=0.1;

    t=0:inc:10;f=0.3;omega=2*pi*f;x=cos(omega*t);[even,odd]=evenoddfunc(x,t);

    Output

    PREPARED BYOSAMA TAHIR

    09-EE-88