matlab lab

Upload: zubair136

Post on 08-Apr-2018

234 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 matlab lab

    1/38

    Matlab Basics

  • 8/7/2019 matlab lab

    2/38

    Workspace, Windows

    Command window

    Enter commands anddata, display results

    Prompt >> Graphics (Figure) window

    Display plots and graphs

    Created

    in

    respon

    se to graphics command

    s M-file editor/debugger window

    Create and edit scripts of commands called M-files

  • 8/7/2019 matlab lab

    3/38

  • 8/7/2019 matlab lab

    4/38

  • 8/7/2019 matlab lab

    5/38

  • 8/7/2019 matlab lab

    6/38

    Scalar Mathematics

    Fixed point: Decimal form, with an optional

    decimal point. For example:

    2.6349 -381 0.00023 Floating point: Scientific notation,

    representing m 10e

    For example: 2

    .6349 105 is represented as2.6349e5

    Operators

  • 8/7/2019 matlab lab

    7/38

  • 8/7/2019 matlab lab

    8/38

    >>3 + 4

    ans =

    7

    >>3 - 3ans =

    0

    >>4/5

    ans =

    0.8000

  • 8/7/2019 matlab lab

    9/38

    >>4^3

    ans =

    64

    >>56/8ans =

    7

    >>8\56

    ans =

    7

  • 8/7/2019 matlab lab

    10/38

    Precedence of operations (order of

    evaluation)

    1. Parentheses, innermost first

    2.Exponentiation (^), left to right

    3. Multiplication (*) anddivision (/ or \) withequal precedence, left to right

    4. Addition (+) and subtraction () with equal

    precedence, left to right

  • 8/7/2019 matlab lab

    11/38

    Variables and Assignment Statements

    variable = numbervariable = expression

    >> screws = 32

    screws =

    32

    >> bolts = 18bolts =

    18

    >> rivets = 40;

    >> items = screws + bolts + rivets

    items =90

    >> cost = screws * 0.12 + bolts * 0.18 + rivets * 0.08

    cost =

    10.2800

  • 8/7/2019 matlab lab

    12/38

    Special variables

    ans: default variable name

    pi: ratio of circle circumference to its diameter, = 3.1415926...

    eps: smallest amount by which two numbers candiffer

    inf or Inf : infinity, e.g.1/0

    nan or NaN : not-a-number, e.g. 0/0

    date: current date in a character string format,such as 19-Mar-1998.

    flops: count of floating-point operations

  • 8/7/2019 matlab lab

    13/38

    Example 2.2 Listing andclearing variables

    >> who

    Your variables are:

    21

    average_cost cost rivets

    bolts items screws

    >> whos

    Name Size Bytes Class

    average_cost 1x1 8 double array

    bolts 1x1 8 double array

    cost 1x1 8 double arrayitems 1x1 8 double array

    rivets 1x1 8 double array

    screws 1x1 8 double array

    Grand total is 6 elements using 48 bytes

  • 8/7/2019 matlab lab

    14/38

    Command reuse and editing

    Press the up arrow cursor key() to scrolls backwardthrough previous commands.

    Press Enter to execute the selected command.

    The down arrow cursor key() scrolls forward through

    commands The left() and right arrow() cursor keys movewithin a command at the Matlab prompt, allowing thecommand to be edited.

    The mouse can also be usedto reposition the command

    cursor, by positioning the mouse cursor and pressingthe left mouse button.

    Other standard editing keys, such as delete Del,backspace BkSp

  • 8/7/2019 matlab lab

    15/38

    Copy

    Ctrl+C

    PasteCtrl+V

  • 8/7/2019 matlab lab

    16/38

    Punctuation and Comments

    Semicolon (;) at the end of a command suppresses thedisplay of the result

    Commas and semicolons can be used to place multiple

    commands on one line, with commasproducing display of results, semicolons suppressing

    Percent sign (%) begins a comment, with all text up tothe next line ignored by Matlab

    Three periods (...) at the end of a command indicatesthat the command continues on the next line. Acontinuation cannot be given in the middle of avariable name.

  • 8/7/2019 matlab lab

    17/38

    Use of punctuation

    >> screws = 36

    screws =

    36>> items

    items =

    90

    >>items = screws + bolts + rivetsitems =

    94

  • 8/7/2019 matlab lab

    18/38

    >> screws=32, bolts=18; rivets=40; % multiplecommands

    screws =

    32>> items = screws + bolts + rivets;

    >> cost = screws*0.12 + bolts*0.18 + rivets*0.08;

    >> average_cost = cost/... % command continuation

    itemsaverage_cost =

    0.1142

  • 8/7/2019 matlab lab

    19/38

    Basic Mathematical Functions

    abs(x) Absolute value |x|

    sign(x) Sign, returns 1 if x < 0, 0 if x = 0, 1 if x> 0

    exp(x) Exponential ex

    log(x) Natural logarithm ln x

    log10(x) Common (base 10) logarithm log10 x

    sqrt(x) Square root x

    rem(x,y) Remainder ofx/y. For example,rem(100,21) is 16. Also called the modulusfunction.

  • 8/7/2019 matlab lab

    20/38

    x =2/2

    y =1/2*e(x2/2)

    z = 20log10 y

    >> x = sqrt(2)/2

    x =0.7071

    >> y = exp(-(x^2)/2)/sqrt(2*pi)

    y =

    0.3107

    >> z = 20*log10(y)z =

    -10.1533

  • 8/7/2019 matlab lab

    21/38

    Arrays: vectors and matrices

    A matrix is a rectangular object (e.g. a table)consisting of rows and columns

    Enter a statement like

    x = [13 0 -15] You can put commas instead of spaces between

    vector elements if you like. Try this:

    a = [5,6,7]

    And what about this?a = [13 7];

    a = [a 0 -1];

  • 8/7/2019 matlab lab

    22/38

    Elements in the list must be enclosed in

    square not round brackets.

    Eleme

    nts i

    nthe list must be separate

    deitherby spaces or by commas.

  • 8/7/2019 matlab lab

    23/38

    Initializing vectors: the colon operator

    x = 1:10

    x =

    1 2 3 4 5 6 7 8 9 10

    x = 1:0.5:4x =

    1.0000 1.5000 2.0000 2.5000 3.00003.5000 4.0000

    x = 10:-1:1x =

    10 9 8 7 6 5 4 3 2 1

  • 8/7/2019 matlab lab

    24/38

    linspace

    The function linspace can be used to initialize

    a vector of equally spaced

    values, e.g. linspace(0, pi/2, 10)

    creates a vector of10 equally spaced points

    from 0 to /2 (inclusive).

  • 8/7/2019 matlab lab

    25/38

    Transposing vectors

    Enter x = 1:5 and then enter x to display the

    transpose of x. Note that x itself remains a

    row vector.

    you can create a column vector directly:

    y = [148 0 -1]

  • 8/7/2019 matlab lab

    26/38

    Subscripts

    1.Enter r = rand(1,7) This gives you a row vector of sevenrandom numbers.

    r =

    0.9501 0.2311 0.6068 0.4860 0.8913 0.7621 0.4565

    2. Now enter r(3)

    This will display the third element of r. The number3 is thesubscript.

    3. Now enter r(2:4)

    This should give you the second, third and fourth elements.

    4. What about r(1:2:7)?

    5. And r([1 7 26])?6. You can use an empty vector to remove elements from a vector, e.g.

    r([1 7 2]) = [ ]

    will remove elements 1, 7 and2.

  • 8/7/2019 matlab lab

    27/38

  • 8/7/2019 matlab lab

    28/38

  • 8/7/2019 matlab lab

    29/38

    a = [248];

    b = [322];

    a .* b

    a ./ b

  • 8/7/2019 matlab lab

    30/38

    Assignment

    Evaluate the following MATLAB expressionsyourself (before you use MATLAB to check!).

    2/2 * 3

    2/3 2

    (2/3) 2

    2 + 3 * 4 4

    2 2 * 3/4 + 3

    2 (2 * 3)/ (4 + 3) 2 * 3 + 4

    2 3 2

  • 8/7/2019 matlab lab

    31/38

    Assignment

    Use MATLAB to evaluate the followingexpressions. The answers are in round

    brackets again.

    a. 2

    b.3 + 4/5 + 6

    c.Find the sum of5 and3divided by theirproduct

    d.232 e.Find the square of2

    f.22

  • 8/7/2019 matlab lab

    32/38

    Assignment

    g.1/2

    h.1/2

    i.Find the cube root of the product of2.3 and4.5

    j.(1 2/(3+2))/(1 + 2/(32))

    k.1000(1 + 0.15/12)60 l. (0.0000123+5.678103)0.4567104

  • 8/7/2019 matlab lab

    33/38

    Assignment

    Water freezes at 32 and boils at 212 on the

    Fahrenheit scale. IfC and F are Celsius and

    Fahrenheit temperatures, the formula

    F = 9C/5 +32

    converts from Celsius to Fahrenheit.

    Use the MATLAB command line to convert

    temperature of37C (normal human

    temperature) to Fahrenheit (98.6).

  • 8/7/2019 matlab lab

    34/38

    Try to avoid using unnecessary brackets in an

    expression. Can you spot the errors in the

    following expression (test your corrected

    version with MATLAB):

    (2(3+4)/(5*(6+1))2

  • 8/7/2019 matlab lab

    35/38

    Set up a vector n with elements 1, 2, 3, 4, 5.

    Use MATLAB array operations on the vector n

    to set up the following four vectors, each with

    five elements:

    a.2, 4, 6, 8, 10

    b.1/2, 1, 3/2, 2, 5/2

    c.1, 1/2, 1/3, 1/4, 1/5

    d.1, 1/22, 1/32, 1/42, 1/52

  • 8/7/2019 matlab lab

    36/38

    Suppose a and b are defined as follows:

    a = [2 -15 0];

    b = [32 -14];

    Evaluate by hand the vector c in the following statements. Check youranswers with MATLAB.

    a. c = a - b;b. c = b + a - 3;

    c. c = 2 * a + a . b;

    d. c = b ./ a;

    e. c = b .\ a;

    f. c = a . b;g. c = 2. b+a;

    h. c = 2*b/3.*a;

    i. c = b*2.*a;

  • 8/7/2019 matlab lab

    37/38

    Assignment

    Use MATLAB array operations to do the

    following:

    1. Add

    1 to each elemen

    t of the vector [23 -1].2. Multiply each element of the vector [148]by

    3.

    3.Find the array product of the two vectors [12

    3]and [0 -11].

    4. Square each element of the vector [231].

  • 8/7/2019 matlab lab

    38/38

    Write a program to calculate x, where

    x = (b +(b2 4ac))/2a

    anda=2, b=10, c=12 (Answer3.0)