Chapter1_Cơ Sở Matlab

Embed Size (px)

Citation preview

  • 8/11/2019 Chapter1_C S Matlab

    1/69

    Lecture 1: Introduction

    Lecture series based on the text:

    Essential MATLABfor Engineers and ScientistsBy

    Hahn & Valentine

    2007 Daniel Valentine. All rights reserved. Published by

    Elsevier.

    http://www.mediafire.com/?y5dz4zjxrj0z4

    Email: [email protected]

    1

    http://www.mediafire.com/?y5dz4zjxrj0z4http://www.mediafire.com/?y5dz4zjxrj0z4
  • 8/11/2019 Chapter1_C S Matlab

    2/69

    MATLAB desktop

    Command Window

    Command History

    Window

    Workspace Window

    Current Directory

    Window

    Start Button

    2

  • 8/11/2019 Chapter1_C S Matlab

    3/69

    Command Window

    The Command Windowon the right is the mainpanel where you interact with MATLAB.

    You key (or type) and commands after

    the prompt >>; MATLAB executes the

    commands and displays results (if requested).

    Some commonly used tools and commands:

    (up arrow) returns last command input, can be

    repeated clcclears the screen

    whosshows list of variables

    clearclears variables

    3

  • 8/11/2019 Chapter1_C S Matlab

    4/69

    Evaluation of MATLAB

    HANDS-ON with MATLAB

    Type

    >>

    2+3

    into the Command Window

    >> clc

    >>whos

    Throughout the lecture, yellow text indicateswhat you should type into MATLAB.

    4

  • 8/11/2019 Chapter1_C S Matlab

    5/69

    Command History Window

    The Command History Windowlogs all of thecommands you enter in MATLAB.

    It should have logged 2+3.

    Use the Command History Window to reenter2+3in the command window (use copy-and-paste or double click on 2+3).

    This is useful to retrieve past commands.

    Use Shiftkey to select multiple lines.

    5

  • 8/11/2019 Chapter1_C S Matlab

    6/69

    Arithmetic with MATLAB

    Let us explore by doing exercises:

    >> 32

    >> 3*2 >> 3/2

    >> 3\2

    >> 3^2

    >> 2/0

    >> 0/2

    >> 3*Inf

    6

  • 8/11/2019 Chapter1_C S Matlab

    7/69

    Algebraic-numeric computations

    Let us explore by doing exercises:

    >> a = 3

    >>b = 2

    >> a b

    >> a / b

    >> a^2

    >> c = a * b >> d = c^(b+1)

    >>who

    7

  • 8/11/2019 Chapter1_C S Matlab

    8/69

    Hiding Output

    Let us explore by doing exercises:>> clear; clc

    >>whos

    >> a = 3; >>b = 2;

    >> c = a * b;

    >> d = c^(b+1);

    >>who

    >> % a, b, c, d are in workspace

    >>a, b, c, d

    8

  • 8/11/2019 Chapter1_C S Matlab

    9/69

    Plotyversusx

    Introduction to plotting & displaying data:

    >> clear; clc

    >> x = 0:0.1:1;

    >> y = x.^2; >> whos

    >> plot(x,y,x,y,o)

    >> disp(' '),disp('...... x ........ y .....'),disp([x y'])

    >> x

    >>y

    >> % x and y are 1-by-11 arrays of numbers!

    9

  • 8/11/2019 Chapter1_C S Matlab

    10/69

    Write a Simple Program

    Consider computing the volume of a cone:Volume = (pi.*r.^2.*h)./3

    radius = 6 inches

    height = 12 inches

    In the command window key in:>> clear; clc

    >>r = 6

    >>h = 12 >>v = (pi.*r.^2.*h)./3

    >>whos

    10

  • 8/11/2019 Chapter1_C S Matlab

    11/69

    Editor & M-Files

    An M-filein MATLAB is analogous to a txt-file in Microsoft Notepad.

    An M-file is created in MATLAB text editor.

    M-files: You can save your programs (i.e., list of

    executable commands) as M-files.

    You can reopen and modify your program. They are useful for debugging (correcting

    errors) as you develop your programs (yourtechnical computing tools).

    11

  • 8/11/2019 Chapter1_C S Matlab

    12/69

    Comments in programs

    Every time you write a program to be saved, it ishelpful for you to comment(i.e., describe)it well.

    To insert a comment on a line in the editor or inthe Command Window, use the comment

    operator %, then type your comment. MATLAB:

    will not run lines that begin with the comment operator(in the editor comments appear in green).

    Comments Comments allow you (and others) to more easily

    understand your program.

    When your lines of code are easy to understand, yourcode will be easier to use later.

    12

  • 8/11/2019 Chapter1_C S Matlab

    13/69

    Art of well-written code

    A well-written program is like literature; itcontains comments explaining:

    what your program requires as input.

    what the variables in the program represent. what your program computes and displays.

    It is useful for you to add a set of headercomments that include the name of theprogram, your name (as the programmer),and the date the program was created ormodified.

    13

  • 8/11/2019 Chapter1_C S Matlab

    14/69

    Saving code in an M-File

    Open the editor by: Entering the command editin the command window.

    Or click the white-sheet-of-paper icon in the upper left

    hand corner directly below file.

    Now enter the lines of code to find the volume of a cone:rr = 4

    h = 12

    v = (pi.*r.^2.*h)./3

    REMARK: If you save it, add header comments and commentsexplaining what the program does.

    After you have typed in the code, save it as cone.m.

    14

  • 8/11/2019 Chapter1_C S Matlab

    15/69

    This is cone.m in the editor

    %% Tool to compute the volume of a cone.% A simple sample for a first lecture.% B.H.& Daniel........... January 2007%rr = 4; % radius of the coneh = 12; % height of the conev = (pi.*r.^2.*h)./3 % Volume of the cone

    15

  • 8/11/2019 Chapter1_C S Matlab

    16/69

    Execute an M-file as a Command

    Now execute (or run) the program by pushingF5, or by typing on the command line>> cone

    or by clicking the run button. (Note that the run buttonlookslike a page with a down arrow to its left. It can be found below help on

    the toolbar of the edit window.) If you entered the code as written on the

    previous slide you will get an error!

    What went wrong?

    Repair your program (Changerr = 4to r = 4.), save it,and run it again.

    Now change the height to 24, save and run yourprogram again.

    16

  • 8/11/2019 Chapter1_C S Matlab

    17/69

    Summary

    MATLAB can be used like a hand calculator todo arithmetic.

    You can define (or assign) variables with

    numbers and expressions to do calculations asillustrated by the volume-of-cone example.

    The advantage of saving programs as M-filesisthat you open it, make changes and/or execute it

    again without having to type it all over again. This concludes our overview of MATLAB and a

    taste of things to come!

    17

  • 8/11/2019 Chapter1_C S Matlab

    18/69

    Lecture 2

    MATLAB fundamentalsVariables, Naming Rules,

    Arrays (numbers, scalars, vectors, matrices),

    Arithmetical Operations,

    Defining and manipulating arrays

    2007 Daniel Valentine. All rights reserved. Published by

    Elsevier.

    18

  • 8/11/2019 Chapter1_C S Matlab

    19/69

    Variables

    What are variables?You name the variables(as the programmer)

    and assign them numerical values.

    19

  • 8/11/2019 Chapter1_C S Matlab

    20/69

    Variable Naming Rules

    Must begin with a LETTER

    May only contain letters, numbers andunderscores ( _ )

    No spaces or punctuation marks allowed!

    Only the first 63 characters are significant;

    beyond that the names are truncated. Case sensitive(e.g. the variables aandA

    are not the same)

    20

  • 8/11/2019 Chapter1_C S Matlab

    21/69

    Which variable names are valid?

    12oclockRock

    tertiarySector

    blue cows

    Eiffel65

    red_bananas

    This_Variable_Name_Is_Quite_Possibly_Too_Lo

    ng_To_Be_Considered_Good_Practice_However_It_Will_Work % (the green part is not part ofthe recognized name)

    21

  • 8/11/2019 Chapter1_C S Matlab

    22/69

    Variable Naming Conventions

    There are different ways to name variables. Thefollowing illustrate some of the conventions used:

    lowerCamelCase

    UpperCamelCase

    underscore_convention

    If a variable is a constant, some programmers use allcaps:

    CONSTANT

    It does not matter which convention you choose to workwith; it is up to you.

    22

  • 8/11/2019 Chapter1_C S Matlab

    23/69

    23

  • 8/11/2019 Chapter1_C S Matlab

    24/69

    In MATLAB, a variableis stored as an array of

    numbers. When appropriate, it is interpreted as a

    scalar, vectoror matrix.

    The size of an array is specified by the number of

    rows and the number of columns in the array, with

    the number of rows indicated first.

    Variables as Arrays

    scalar1 1

    vectorn 1 or 1 n

    matrixn m

    24

  • 8/11/2019 Chapter1_C S Matlab

    25/69

    Scalarsare 11 arrays.

    They contain a single value, for example:

    r = 6r = 6

    width = 9.07width = 9.07

    height = 5.3height = 5.3

    Scalars

    25

  • 8/11/2019 Chapter1_C S Matlab

    26/69

    Vectors

    A vectoris a list of numbers expressed as a 1

    dimensional array.

    A vector can be n1 or 1n.

    Columns are separated by commas (or spaces):

    Rows are separated by semicolons:

    v = [1; 2; 3]v = [1; 2; 3]

    h = [1, 2, 3]h = [1, 2, 3]

    26

  • 8/11/2019 Chapter1_C S Matlab

    27/69

    m = [3.0, 1.8, 3.6; 4.6,m = [3.0, 1.8, 3.6; 4.6, --2.0, 21.3; 0.0,2.0, 21.3; 0.0,

    --6.1, 12.8; 2.3, 0.3,6.1, 12.8; 2.3, 0.3, --6.1]6.1]

    Matrices

    A matrixis a twodimensional array ofnumbers.

    For example, this is a43 matrix:

    1 2 3

    1 3.0 1.8 3.6

    2 4.6 -2.0 21.3

    3 0.0 -6.1 12.8

    4 2.3 0.3 -6.1

    Columns

    Row

    s

    27

  • 8/11/2019 Chapter1_C S Matlab

    28/69

    m(2,3)m(2,3)

    Indexed-location of numbers in anarray

    Each item in an arrayis located in the

    (row, column).

    1 2 3

    1 3.0 1.8 3.6

    2 4.6 -2.0 21.3

    3 0.0 -6.1 12.8

    4 2.3 0.3 -6.1

    Columns

    Rows

    ans =21.3000

    28

  • 8/11/2019 Chapter1_C S Matlab

    29/69

    Enter the following into MATLAB:

    Scalar:

    Vectors:

    Matrix:d = [5, 4, 3; 0, 2, 8]d = [5, 4, 3; 0, 2, 8]

    b = [1, 0, 2]b = [1, 0, 2]

    c = [1 0 2]c = [1 0 2]

    a = 1a = 1

    Examples

    29

  • 8/11/2019 Chapter1_C S Matlab

    30/69

    Examples

    Enter (input) the following matrix into MATLAB:

    -7 21 6

    2 32 0

    -5 0 -18.5

    whiteRabbit =

    30

  • 8/11/2019 Chapter1_C S Matlab

    31/69

    Scalar Operations

    Operation AlgebraicSyntax

    MATLAB Syntax

    Addition a + b a + b

    Subtraction a - b a b

    Multiplication a b a .* b

    Division a b a ./ b ora.\ b

    Exponentiation ab a .^ b

    31

  • 8/11/2019 Chapter1_C S Matlab

    32/69

    Array Operations

    Arrays of numbers in MATLAB can be interpreted asvectors and matrices if vector or matrix algebra is to beapplied. Recall that matrices are mathematical objectsthat can be multiplied by the rules of matrices. To do

    matrix multiplication, you need to use the standard *, /,and ^ operators [without the preceding .(dot)]. They arenotfor array multiplication, division and exponentiation.

    To deal with arrays on an element-by-elementlevel we

    need to use the following arrayor dot-operators:

    .* ,./ and.^

    32

  • 8/11/2019 Chapter1_C S Matlab

    33/69

    Array operations & dot-operators

    Because scalars are equivalent to a 11array, you can either use the standard or

    the dot-operatorswhen doingmultiplication, division and exponentiationof scalars (i.e., of single numbers).

    It is okay for you to always use the dot-operators, unless you intend to performvector or matrix multiplication or division.

    .*, ./ and.^

    33

  • 8/11/2019 Chapter1_C S Matlab

    34/69

    Example:

    z = x .* yresults in [10, 6; 21, 32]; this is array multiplication

    z = x * y

    results in [17, 20; 43, 50]; this is matrix multiplication

    So, do NOT forget the dot when doing arrayoperations! (.* ./ .^)

    x = [2, 1; 3, 4]x = [2, 1; 3, 4]

    y = [5, 6; 7, 8]y = [5, 6; 7, 8]

    Array vs. Matrix Operations

    34

  • 8/11/2019 Chapter1_C S Matlab

    35/69

    Hierarchy of Operations

    Just like in mathematics the operations are done in thefollowing order: Left to rightdoing what is in

    Parentheses & Exponents first, followed by

    Multiplication & Division, and thenAddition & Subtraction last.

    An example:

    c = 2+3^2+1/(1+2) 1st c = 2+3^2+1/3

    c = 2+3^2+1/(1+2) 2nd

    c = 2+9+1/3c = 2+3^2+1/(1+2) 3rd c = 2+9+0.33333

    c = 2+3^2+1/(1+2) 4th c = 11+0.33333

    c = 2+3^2+1/(1+2) 5th c = 11.33333

    35

  • 8/11/2019 Chapter1_C S Matlab

    36/69

    Hands-on

    Enter these two arrays into MATLAB:

    Multiply, element-by-element, a b.

    Since this is an array operation, the .*multiplication operation is implied by therequest.

    a =10 5 52 9 0

    6 8 8

    b =1 0 20 0 0

    1 1 0

    36

  • 8/11/2019 Chapter1_C S Matlab

    37/69

    Defining & manipulating arrays

    All variables in MATLAB are arrays!

    Single number array & scalar: 1 1

    Row array & row vector: 1 n

    Column array & column vector: n x 1Array of n rows x m columns & Matrix: n m

    Naming rules

    Indexed by (row, column) Remark:vectors and matrices are special

    mathematical objects, arrays are lists ortables of numbers.

    37

  • 8/11/2019 Chapter1_C S Matlab

    38/69

    The equal sign assigns

    Consider the command lines:>> ax = 5;

    >> bx = [1 2];

    >> by = [3 4];>> b = bx + by;

    The equal sign (=) commands that the

    number computed on the right of it isinput to the variable named on the left;thus, it is an assignment operation.

    38

  • 8/11/2019 Chapter1_C S Matlab

    39/69

    An arraycan be defined by typing in a list of numbersenclosed in square brackets:

    Commasor spacesseparate numbers.

    Semicolonsindicate a new row.

    A = [12, 18,A = [12, 18, --3]3] oror A = [12 18A = [12 18 --3]3]

    B = [2, 5, 2; 1 , 1, 2; 0,B = [2, 5, 2; 1 , 1, 2; 0, --2, 6]2, 6]

    Defining (or assigning) arrays

    A =12 18 -3

    B =2 5 21 1 20 -2 6

    39

  • 8/11/2019 Chapter1_C S Matlab

    40/69

    D =12 18 -3 12 18 -3

    2 5 2 2 5 2

    1 1 2 1 1 2

    0 -2 6 0 -2 6

    C = [A; B]C = [A; B]

    D = [C, C]D = [C, C]

    Defining arrays continued

    You can define an array in terms of another array:

    C =

    12 18 -3

    2 5 21 1 2

    0 -2 6

    40

  • 8/11/2019 Chapter1_C S Matlab

    41/69

    Create an array of zeros:

    Create an array of ones:

    Note: Placing a single number inside either function will return an n narray.e.g. ones(4)will return a 4 4 array filled with ones.

    E = zeros(3,5)E = zeros(3,5)

    F = ones(2,3)F = ones(2,3)

    Creating Zeros & Ones arrays

    E =0 0 0 0 0

    0 0 0 0 00 0 0 0 0

    F =

    1 1 11 1 1

    41

  • 8/11/2019 Chapter1_C S Matlab

    42/69

    Indexa number used to identify elements in an array

    Retrieving a value from an array:

    G(3,2)G(3,2)G(2,1)G(2,1)

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

    Retrieving Values in an Array

    ans = 4 ans = 8

    G =1 2 34 5 67 8 9

    42

  • 8/11/2019 Chapter1_C S Matlab

    43/69

    You can change a value in an element in an array with indexing:

    You can extend an array by defining a new element:

    Notice how undefined values of the array are filled with zeros

    A(2) = 5A(2) = 5

    A(6) = 8A(6) = 8

    Changing Values in an Array

    A =

    12 5 -3

    A =12 5 -3 0 0 8

    43

  • 8/11/2019 Chapter1_C S Matlab

    44/69

    Colon notationcan be used to define evenly spaced vectors in theform:

    first : last

    The default spacing is 1, so to use a different increment, use the form:

    first : increment : last

    The numbers now increment by 2

    I = 1:2:11I = 1:2:11

    H = 1:6H = 1:6

    Colon Operator

    H =1 2 3 4 5 6

    I =1 3 5 7 9 11

    44

    h h l

  • 8/11/2019 Chapter1_C S Matlab

    45/69

    G(2,:)G(2,:)G(:,1)G(:,1) G(:,3)G(:,3)

    Extracting Data with the ColonOperator

    The colon represents an entire row or column when usedin as an array index in place of a particular number.

    G =1 2 3

    4 5 67 8 9

    ans =

    1

    4

    7

    ans =

    3

    6

    9

    ans =

    4 5 6

    45

    E i D i h h C l

  • 8/11/2019 Chapter1_C S Matlab

    46/69

    G(1,2:3)G(1,2:3)G(2:3,:)G(2:3,:)

    Extracting Data with the ColonOperator Continued

    The colon operator can also be used to extract a rangeof rows or columns:

    G =1 2 3

    4 5 67 8 9

    ans =2 3

    G =4 5 67 8 9

    46

  • 8/11/2019 Chapter1_C S Matlab

    47/69

    J'J'J = [1 , 3, 7]J = [1 , 3, 7]

    Manipulating Arrays

    The transpose operator, an apostrophe,changes all of an arrays rows to columnsand columns to rows.

    J =1 3 7

    ans =

    137

    47

  • 8/11/2019 Chapter1_C S Matlab

    48/69

    Manipulating Matrices Continued

    The functions fliplr()and flipud()flip amatrix left-to-right and top-to-bottom,respectively.

    Experiment with these functions to see howthey work.

    48

  • 8/11/2019 Chapter1_C S Matlab

    49/69

    W = [1:5; 10:2:18; 6:W = [1:5; 10:2:18; 6:--1:2]1:2]

    Hands-on exercise:

    Create the following matrix using colon notation:W =

    1 2 3 4 510 12 14 16 186 5 4 3 2

    All three rows are evenly spaced The first row ranges from 1 to 5 in increments of 1

    1:5

    The second row ranges from 10 to 18 in increments of 2

    10:2:18 The third row ranges from 6 to 2 in increments of -1 6:-1:2

    All together:

    49

  • 8/11/2019 Chapter1_C S Matlab

    50/69

    Hands-on continued

    Create the following matrix using colon notation:

    X =

    1.2 2.3 3.4 4.5 5.6

    1.9 3.8 5.7 7.6 9.5

    0 -3 -6 -9 -12

    Transpose this matrix and assign it to variable Y.

    >> Y = x

    Extract the 2nd row from Y and assign it to variable Z.

    >> Z = Y(2,:)

    50

  • 8/11/2019 Chapter1_C S Matlab

    51/69

    Summary (1 of 2)

    Naming a variable: Start with letterfollowed by any combination of letters,numbers and underscores (up to 63 of

    these objects are recognized).Arrays are rows and columns of numbers.

    Array operations (element-by-element

    operations with the dot-operators)

    Hierarchy of arithmetic operations.

    51

  • 8/11/2019 Chapter1_C S Matlab

    52/69

    Summary (2 of 2)

    Command lines that assign variablesnumerical values start with the variablename followed by = and then the defining

    expressionAn array of numbers is the structure of

    variables in MATLAB. Within one variablename, a set of numbers can be stored.

    Array, vector and matrix operations areefficient MATLAB computational tools.

    52

  • 8/11/2019 Chapter1_C S Matlab

    53/69

    Lecture 3

    Creating M-filesProgramming tools:

    Input/output(assign/graph-&-display)

    Repetition (for)

    Decision (if)

    2007 Daniel Valentine. All rights reserved. Published by

    Elsevier.

    53

    T c

  • 8/11/2019 Chapter1_C S Matlab

    54/69

    Review

    Arrays List of numbers in brackets

    A comma or space separates numbers (columns) A semicolon separates row

    Zeros and ones Matrices:

    zeros() ones()

    Indexing (row,column)

    Colon Operator: Range of Data first:last or first:increment:last

    Manipulating Arrays & Matrices Transpose

    54

  • 8/11/2019 Chapter1_C S Matlab

    55/69

    Input

    Examples of input to arrays:

    Single number array & scalar: 1 1

    >> a = 2

    Row array & row vector: 1 n

    >> b = [1 3 2 5]

    Column array & column vector: n x 1

    >> b = b % This an application of the transpose.Array of n rows x m columns & Matrix: n m

    >> c = [1 2 3; 4 5 6; 7 6 9] % This example is 3 x 3.

    55

  • 8/11/2019 Chapter1_C S Matlab

    56/69

    Basic elements of a program

    Input

    Initialize, define or assign numerical values tovariables.

    Set of command expressions Operations applied to input variables that lead

    to the desired result.

    Output Display (graphically or numerically) result.

    56

  • 8/11/2019 Chapter1_C S Matlab

    57/69

    An example of technical computing

    Let us consider using the hyperbolic tangentto model a down-hill section of a snowboardor snow ski facility.

    Let us first examine the hyperbolic tangentfunction by executing the command:

    >> ezplot(tanh(x))

    We get the following graph:

    57

  • 8/11/2019 Chapter1_C S Matlab

    58/69

    Ezplot(tanh(x))

    58

  • 8/11/2019 Chapter1_C S Matlab

    59/69

  • 8/11/2019 Chapter1_C S Matlab

    60/69

    Problem background

    Let us consider the design of a slope thatis to be modeled by tanh(X). Consider therange -3

  • 8/11/2019 Chapter1_C S Matlab

    61/69

    Formula for snow hill shape

    61

  • 8/11/2019 Chapter1_C S Matlab

    62/69

    Problem statement

    Find the altitude of the hill at 0.5 meterintervals from -5 meters to 5 meters usingthe shape described and illustrated in theprevious two slides.

    Tabulate the results.

    62

  • 8/11/2019 Chapter1_C S Matlab

    63/69

    Structure plan

    Structure plan Initialize the range of X in 0.5 meter intervals.

    Compute y, the altitude, with the formula:

    y = 1tanh(3*X/5). Display the results in a table.

    Implementation of the structure plan Open the editor by typing editin the

    command window.

    Translate plan into the M-file language.

    63

  • 8/11/2019 Chapter1_C S Matlab

    64/69

    This is from editor: It is lec3 m

    %% Ski slope offsets for the% HYPERBOLIC TANGENT DESIGN% by D.T. Valentine...January 2007%% Points at which the function

    % is to be evaluated:% Step 1: Input xx = -5:0.5:5;

    % Step 2: Compute the y offset,% that is, the altitude:

    y = 1 - tanh(3.*x./5);

    %% Step 3: Display results in a table%disp(' ') % Skips a linedisp(' X Y')disp([x' y'])

    Command window OUTPUT

    X Y

    -5.0000 1.9951

    -4.5000 1.9910

    -4.0000 1.9837

    -3.5000 1.9705

    -3.0000 1.9468

    -2.5000 1.9051

    -2.0000 1.8337

    -1.5000 1.7163

    -1.0000 1.5370

    -0.5000 1.2913

    0 1.0000

    0.5000 0.7087

    1.0000 0.4630

    1.5000 0.2837

    2.0000 0.16632.5000 0.0949

    3.0000 0.0532

    3.5000 0.0295

    4.0000 0.0163

    4.5000 0.0090

    5.0000 0.0049

    SOLUTION TO IN-CLASS EXERCISE

    64

  • 8/11/2019 Chapter1_C S Matlab

    65/69

    Use of repetition (for)

    Repetition: In the previous example, thefastest way to compute y was used. Analternative way is as follows:

    Replace:y = 1 - tanh(3.*x./5); % This is vectorized approach.

    With:

    for n=1:21

    y(n) = 1 - tanh(3*x(n)/5);

    end

    Remark: Of course, the output is the same.

    65

    SOLUTION TO IN CLASS EXERCISE

  • 8/11/2019 Chapter1_C S Matlab

    66/69

    This is from editor: It is lec3_2 m

    %% Ski slope offsets for the% HYPERBOLIC TANGENT DESIGN% by D.T. Valentine...January 2007%% Points at which the function

    % is to be evaluated:% Step 1: Input xx = -5:0.5:5;

    % Step 2: Compute the y offsetfor n=1:21

    y(n) = 1 - tanh(3*x(n)/5);

    end%% Step 3: Display results in a tabledisp(' ') % Skips a linedisp(' X Y')disp([x' y'])

    Command window OUTPUT

    X Y

    -5.0000 1.9951

    -4.5000 1.9910

    -4.0000 1.9837

    -3.5000 1.9705

    -3.0000 1.9468

    -2.5000 1.9051

    -2.0000 1.8337

    -1.5000 1.7163

    -1.0000 1.5370

    -0.5000 1.2913

    0 1.0000

    0.5000 0.7087

    1.0000 0.4630

    1.5000 0.2837

    2.0000 0.16632.5000 0.0949

    3.0000 0.0532

    3.5000 0.0295

    4.0000 0.0163

    4.5000 0.0090

    5.0000 0.0049

    SOLUTION TO IN-CLASS EXERCISE

    66

  • 8/11/2019 Chapter1_C S Matlab

    67/69

    Decision: Application of if

    Temperature conversion problem:

    Convert C to F or F to C.

    67

    Decision: Application of if

  • 8/11/2019 Chapter1_C S Matlab

    68/69

    %% Temperature conversion from C to F% or F to C as requested by the user%

    Dec = input(' Which way?: 1 => C to F? 0 => F to C: ');Temp = input(' What is the temperature you want to convert? ');%% Note the logical equals sgn (==)if Dec == 1

    TF = (9/5)*Temp + 32;disp(' Temperature in F: ')disp(TF)

    elseTC = (5/9)*(Temp-32);disp(' Temperature in C: ')disp(TC)

    end

    Decision: Application of if

    Temperature conversion problem:

    Convert C to F or F to C. SOLUTION:

    68

  • 8/11/2019 Chapter1_C S Matlab

    69/69

    Summary

    Introduced, by an example, the structureplan approach to design approaches tosolve technical problems.

    Input: Assignment with and without inpututility.

    Output: Graphical & tabular were shown.

    Illustrated array dot-operation, repetition(for) and decision (if) programming tools.