Today - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Today

Description:

We will cover 1.3 in more detail later. Do Exercises 3.1, -2, -3, -4, -7, -8, -9 ... Number 'Formats' What about signs, decimal points, exponents? ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 22
Provided by: ricks6
Category:
Tags: cover | formats | letter | today

less

Transcript and Presenter's Notes

Title: Today


1
Todays Class
  • Recap
  • Homework
  • Number Formats
  • Text Strings and Vectors
  • Arithmetic on vectors
  • Creating vectors with colon
  • Sums and products
  • Drawing Plots
  • Booleans and Logical Operators

2
Recap
  • Matlab evaluates arithmetic expressions
  • 2 different styles infix and function-oriented
  • Infix follows typical algebraic rules
  • Variables
  • Naming similar but different from naming
    files
  • Gives names to locations in RAM for calculated
    results
  • Assignment Statements
  • Format variable expression
  • Can use the same variable name on both sides
  • Other notational styles prefix, postfix
  • Parsing the process of analyzing text

3
Quick Quiz
  • Variables X, Y
  • Some arithmetic with them

Calculation X Value Y Value
X 8 8 ?
Y 3 8 3
X X Y 2 13 3
X Y - 3 0 3
Y Y X 0 3
Y X 0 0
4
Chapter 3 Homework
  • Read Chapters 1 through 3.
  • We will cover 1.3 in more detail later
  • Do Exercises 3.1, -2, -3, -4, -7, -8, -9
  • Answer all of 3.3
  • Due at the start of class, Wednesday

5
Storing Numbers in RAM
  • Numbers stored in bits binary numbers
  • 8 bits byte
  • (4 bits nibble, 2 bits ?)
  • Number of bits -gt power of 2 -gt number range
  • Calculators
  • Number of digits -gt power of 10 -gt number range

6
Number Formats
  • What about signs, decimal points, exponents?
  • Signed integers 1 bit sign, rest magnitude
  • 32 bits, 1 bit removed for sign, range is 231
  • Floating point numbers
  • Part set aside for mantissa (value part)
  • Part set aside for exponent (power part)
  • In Matlab typical numbers are floating point
  • In C, the integer/float distinction MATTERS

7
Text Strings and Vectors
  • We create text strings with a pair of quotes
  • We create vectors with a pair of braces
  • Both are implemented as a sequence of numbers
    with common properties
  • One dimension of length
  • Example length(Hello) 5
  • We can do arithmetic on both, element by element
  • We can do other operations that combine all
    elements

8
Arithmetic on vectors
  • Add and subtract are always applied pairwise
  • 1 2 3 3 gt 4 5 6
  • 1 2 3 5 6 7 gt 6 8 10
  • Multiply, divide, power may be cross or dot
  • In this class, we generally use the dot
  • 1 2 3 . 3 gt 3 6 9
  • 1 2 3 . 1 2 3 gt 1 4 9
  • Always use the dotted version . ./ .

9
Creating vectors with colon
  • The colon() function (infix ) creates vectors
  • 2 arguments starting value, ending value
  • A list of values between that range (increment of
    1)
  • 3 arguments starting value, increment, ending
    value
  • A list of values incremented as given
  • Function form
  • colon(1,10) colon(1,.1,2) colon(0,.01,.1)
  • Infix form
  • 110 1.12 0.01.1

10
Sums and Products
  • sum(vector) gt adds up the elements
  • prod(vector) gt multiplies elements together
  • 5
  • ? i
  • i1
  • How do we write this in Matlab?

11
Taking Sums Apart
  • First, is it taking elements from a vector?
  • Look at the term indexing the element (i, on
    previous slide)
  • If it is subbed to another variable, you have a
    vector
  • Refer to the vector in the calculation
  • Second, are we doing arithmetic on the index?
  • On the previous slide?
  • If so, refer to the index variable in the
    calculation

12
Questions?
  • Continue..

13
Sum the contents of a Vector
  • n
  • ? n ai
  • i1
  • Given a vector named a,
  • How do we calculate this in Matlab?

14
Power Series
  • n
  • ? xi
  • i1
  • Given a value for x, like 2 or 14,
  • How do we calculate this in Matlab?

15
Polynomial Expansion
  • n
  • ? aixi
  • i1
  • Given a vector named a, and a value for x,
  • How do we calculate this in Matlab?

16
Drawing Plots
  • The plot function
  • does not return any value, but it has the
    useful side-effect of drawing a graph. italics
    added
  • Functions usually return a value to be useful,
    but not always.
  • Takes 2 or 3 arguments
  • First 2 arguments are 2 vectors
  • First are the X axis values
  • Second are the corresponding Y axis values
  • Try graphing some trig functions
  • We need x values gt use the colon function
  • Third argument contains a text string with
    options
  • Omit it, or try these
  • -o . g ro - k

17
Boolean Operations
  • The basic ones are for comparison
  • List is on page 55 gt gt lt lt
  • Mostly identical for C, except for
  • Question Are you 21? Say it in Matlab
  • Lots of other specialized booleans
  • Depends on the programming environment
  • Matlab examples
  • isletter(x)
  • isspace( )
  • ispc isunix
  • issorted( 1 2 3 4 5 )
  • strcmp(abc abd)

18
Logical Operators
  • Take boolean inputs yield results (or vectors)
  • One type lets us combine Boolean operators
  • and () and or ()
  • In C, theyre and
  • and are operations on numbers
  • Example Age between 18 and 35.
  • Applied individually to vector entries
  • xor operation (why?) in C, uses infix

19
Vector Logic
  • Expressing For all values in the vector
  • Or For any value in the vector
  • First, we apply the test to the vector.
  • Then we apply the all() or any() function
  • Examples
  • Is everyone over 21?
  • Is anyone over 21?

20
Chapter 3 Homework
  • Read Chapters 1 through 3.
  • We will cover 1.3 in more detail later
  • Do Exercises 3.1, -2, -3, -4, -7, -8, -9
  • Answer all of 3.3
  • Due at the start of class, Wednesday

21
Creative Commons License
  • This work is licensed under the Creative Commons
    Attribution-Share Alike 3.0 United States
    License. To view a copy of this license, visit
    http//creativecommons.org/licenses/by-sa/3.0/us/
    or send a letter to Creative Commons, 171 Second
    Street, Suite 300, San Francisco, California,
    94105, USA.
Write a Comment
User Comments (0)
About PowerShow.com