Class Prep - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Class Prep

Description:

Calculate % of cars going over the speed limit. limit = 65; ... format compact Suppresses excess line feeds. format loose Add line feeds. CSc-070 Week02-b ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 37
Provided by: davidh67
Category:
Tags: class | prep

less

Transcript and Presenter's Notes

Title: Class Prep


1
Class Prep
  • Bring to Class
  • In-Class Exercise
  • Paper for Classroom Printer
  • Check HannayD folder under My Documents(or
    H/csc070)
  • Run MATLAB
  • Set Current Directory

2
Start up MATLAB
  • Run MATLAB
  • Change MATLAB Current Directory
  • h/csc070/70week02

3
Week 02-b(3.1-3.4)
  • MATLAB Vectors
  • Finding Values within an Array
  • Elementary Math Functions

4
Main Collection Type Array
  • A Collection is a data structure into which you
    put stuff, and from which you get stuff.
  • MATLABs basic type of collection is called an
    Array.
  • Arrays are homogeneous collection of things.
  • All "things" are the same "type"
  • Arrays are indexable
  • c.f. Arrays in Machine/Assembly Language
  • Example a text string is an array of characters
  • If ch 'ABC' then ch1 is 'A', ch2 is 'B', ch3
    is 'C'
  • Recall that ch1 is stored as 65 (see Appendix B)

5
Array Processing
  • Big part of MATLAB's power is operation on entire
    Arrays, both on a per element basis and in a
    summary basis.
  • Per Element y sin(x) 2x
  • Summary p prod(x) sum(x)
  • Each element of an array has a value and a
    position (sometimes called index ).

6
Vectors
  • The simplest array type is called a vector.
  • Simple example (using MATLAB syntax)
  • X 82 7 42 16 8 127 33
  • To index into a vector needs to know the position
    within the vector
  • First element has position 1
  • e.g. X(1) which happens to equal 82
  • Last element has position either the length of
    the array or the special label end
  • e.g. X(length(X)), or X(end) equals 33

7
Row Column Vectors
  • A row vector in MATLAB can be created by an
    explicit list, starting with a left bracket,
    entering the values separated by spaces (or
    commas) and closing the vector with a right
    bracket.
  • gtgt x 0 0.25pi 0.5pi 0.75pi pi
  • x
  • 0 0.7854 1.5708 2.3562
    3.1416
  • A column vector can be created in a similar way,
    with the rows are separated by semicolons.
  • gtgt y 0 pi 0.5pi 2pi
  • y
  • 0
  • 3.1416
  • 1.5708
  • 6.2832

x is a row vector. y is a column vector.
11/10/2009
7
8
Simple Vector Commands
11/10/2009
8
9
Hands-On DEMO Creating Vectors
  • Leave off semi-colon to see what you get each
    time
  • gtgt a 110
  • gtgt b 00.11
  • gtgt c 7 8 9
  • gtgt d 10 11 12
  • gtgt length(b) NOTE length is 11 not 10
  • gtgt linspace(0,100,21)

11/10/2009
9
10
Hands-On DEMO "Average" of 5 numbers
  • Store list of 5 values in variable x
  • e.g. x 56 57 33 98 45
  • Display arithmetic mean and median
  • mean(x)
  • median(x)
  • NOTE No "average" function (ambiguous)
  • median() gives you the MIDDLE value (keep this in
    mind for class exercise)

11/10/2009
10
11
Hands-On DEMO Basic Operations on Vectors
  • gtgt v 028 v 0 2 4 6 8
  • gtgt u 0-1-4
  • u 0 -1 -2 -3 -4
  • gtgt uv
  • ans 0 1 2 3 4
  • Additionally, scalar multiplication is defined in
    the standard way. Also note that scalar division
    is defined in a way that is consistent with
    scalar multiplication
  • gtgt -2u
  • ans 0 2 4 6 8
  • gtgt v/3
  • ans 0 0.6667 1.3333 2.0000 2.6667

11/10/2009
11
12
Partial Vectors
  • a 210
  • a(46) 18
  • a(46) 8 6 4
  • a(46) 4 5 6 7 - error
  • b 12 3 4 5 6 7
  • b(13) a(57) "called slicing"

13
Hands-On DEMO Extracting Partial Vector
  • gtgt x linspace(31,40,10)
  • gtgt y x(37)
  • y 33 34 35 36 37
  • gtgt y(3)
  • ans 35
  • The expression, y x(37), copies the third
    through seventh elements of x into the first
    through fourth elements of y. If y did not
    already exist it is created by the assignment.
  • gtgt v 028 v 0 2 4 6 8
  • gtgt v(13)-v(24)
  • ans -2 -2 -2

14
Hands-On DEMO Modifying Vectors
  • x -2 0 9 1 4
  • x(2) 5
  • x -2 5 9 1 4
  • x(4) x(1)
  • x -2 5 9 -2 4
  • x(8) -1
  • x -2 5 9 -2 4 0 0
    -1

15
Arrays as Indexes
  • fred 25100
  • fred(56)
  • Remove the odd indexed values
  • fred(12end)
  • The important use of arrays as indices has to do
    with picking out the elements of a vector (or
    array) that have a certain property or
    characteristic.
  • To do this requires two things
  • definition of the characteristic
  • "Finding" the indices of the vector with that
    property

16
find Function
  • dates 16 17 18 19 20
  • rain 0 0.2 0.1 0 1.7
  • Save indices where array has a certain property
  • index find(rain gt 0)
  • Now use index twice
  • goodRain rain(index)
  • goodDates dates(index)
  • We could plot only days with rainfall
  • plot(goodDates, goodRain)

17
Hands-On DEMO Speed Demon
  • Work in command window
  • Assign 65 to limit
  • Enter vector of speeds 60 70 65 80 66
  • Use find to list those over the speed limit
  • Indices (subscripts)
  • Speeds at those indices
  • Calculate of cars going over the speed limit
  • limit 65
  • indicesfind(speedsgtlimit)
  • speeds(indices)
  • percent_over_limit ... length(indices)/length(sp
    eeds)100

18
Predefined (Built-In) Functions
  • To call a built-in function, include its name
    followed by any arguments enclosed in parentheses
    following the name
  • Arguments may be constants, variables, or
    expressions (scalars, vectors, or matrices)
  • ysin(x/2)
  • Use a function in an expression to calculate a
    value
  • w2sin(a)

19
Hands-On DEMO Using Built-In Functions
gtgt p sin (pi/4) p 0.7071 gtgt q
log (4) q 1.3863
  • Trigonometric functions
  • sin ( ) asin ( ) sind (
    )
  • cos ( ) acos ( ) cosd ( )
  • tan ( ) atan ( ) tand ( )
  • Angle is in radians Angle in
    degrees

20
Degrees vs. Radians for Measuring Angles
  • http//www.teacherschoice.com.au/Maths_Library/Ang
    les/Angles.htm

21
Help on Predefined Functions
  • help elfun ? categorized list of elementary math
    functions
  • Trigonometric
  • Exponential
  • Complex
  • Rounding and remainder
  • Follow link for sin (same as typing "help sin")
  • Follow link to doc sin (same as typing "doc sin")
  • Can find through "Help" menu also
  • What do we need to know from documentation?
  • List of functions available types of arguments
    values

22
(No Transcript)
23
Hands-On DEMO Functions of Vectors
  • Most MATLAB functions will work equally well with
    both scalars and arrays (of any dimension)

gtgt A1 2 3 4 5 gtgt sin(A) ans 0.8415
0.9093 0.1411 -0.7568 -0.9589 gtgt
sqrt(A) ans 1.0000 1.4142 1.7321
2.0000 2.2361
11/10/2009
23
24
Hands-On DEMO Trig Functions(remember that sin,
cos, tan use radians sind, cosd, tand use
degrees)
  • Calculate twice the sine of 45 degrees
  • 2sind(45) should equal sqrt(2)
  • Generate a vector of angles from 0 to 360 in
    steps of 10 degrees
  • Generate a vector of corresponding sine values
  • plot(angles,sine) more on plot later
  • Display a 2-column table with angle and sine
    values
  • angles010360
  • sinesind(angles)
  • angles' sine'

25
  Selected Exponential Functions
  • exp Exponential ex.
  • log Natural logarithm (often called ln(...)).
  • log10 Common (base 10) logarithm.
  • log2 Base 2 logarithm.
  • pow2 Base 2 power.
  • sqrt Square root.

26
Hands-On DEMO Exponential Functions
  • Create a single MATLAB command to display a
    2-column table listing the first 10 powers of 2.
  • The table should show values of "p" from 0 to 9
    in the first column an 2p in the second.
  • 09' pow2(09)'

27
Rounding and Remainder Functions
  • fix Round towards zero.
  • floor Round towards minus infinity.
  • ceil Round towards plus infinity.
  • round Round towards nearest integer.
  • mod Modulus (signed remainder after division).
  • rem Remainder after division.
  • sign -1, 0, or 1 based on sign of number.

28
Hands-On DEMO Rounding Functions
  • Create a 5-column table that illustrates the
    differences between the rounding functions round,
    fix, floor, and ceil for positive and negative
    values.
  • Apply functions to a vector of numbers (saved as
    variable x) ranging from -2 to 2 in increments
    of 0.1
  • x-2.12'
  • x round(x) fix(x) floor(x) ceil(x)

29
Additional format Specifications(MATLAB performs
all computations in double precision)
  •   The format command switches among different
    display formats.
  •  Command      Result                   Example
    format short      5 digit scaled fixed
    point      3.1416 format long       15 digit
    scaled fixed point   3.14159265358979 format
    short e   5 digit floating-point          3.1416e
    00 format long e    15 digit floating-point  
    3.141592653589793e00
  • format short g   general purpose           
    5 or 1.25 or 3.0e-12
  • format bank       Fixed dollars and
    cents         7.95 format rat        Ratio of
    small integers         355/113   format
    compact Suppresses excess line feeds. format
    loose     Add line feeds.

11/10/2009
29
30
Hands-On DEMO Formatting
  • p020
  • format short e exponential
  • p' pow2(p)' pow2(-p)'
  • format short g general purpose
  • p' pow2(p)' pow2(-p)'

11/10/2009
30
31
Hands-On DEMO Operations on Transposes
11/10/2009
31
32
Hands-On DEMO Element by Element Operations
Note the . operator for "Element by Element"
Multiplication
Plotting a function using vector math x
linspace(0, 20, 100) define 100 x values (from
0 to 20) y 5exp(-0.3x).sin(x) compute y
vector plot(x,y), xlabel('X'), ylabel('Y'),
title('Vector calc')
  • linspace( ) function can be very effective for
    creating the x vector

11/10/2009
32
33
Question
  • Given x 1 2 3
  • Why does
  • gtgt x2
  • Yield an ERROR,
  • but
  • gtgt x.2
  • is fine?
  • We are asking for xx but dimensions are wrong.
  • Squaring individual elements of x is fine

11/10/2009
33
34
Hands-On DEMO "Dot" Division by Vector
  • gtgt x1 2 3 4 6 12
  • gtgt 12./x
  • What about x./12?
  • How about x/12?
  • Finally 12/x?
  • "Dot" division ( multiplication) operate element
    by element

35
In-Class Exercise 2b Height of a Building
  • One day, the barometric pressure at ground level
    was measured at 30.28 inches of mercury, and on
    the roof of a building it was measured at 30.16
    inches of mercury.
  • You are given the formula
  • height in feet 25,000 ln(ground pressure /
    roof pressure)
  • Calculate heights of building based on pressure
    measurements over 5 days. Then display the
    "middle" value to nearest foot.
  • Write a MATLAB script to solve the problem. Use
    the appropriate predefined functions for natural
    log (ln in problem description, but not in
    MATLAB) and rounding
  • Comment all Units of Measure.
  • Add your name, comments and print script output

36
END
Write a Comment
User Comments (0)
About PowerShow.com