Flow of Control - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Flow of Control

Description:

Minimize sum of SQUARES of errors. Called 'least squares fitting' ... Provides a UNIQUE best-fit line. Corresponds to the 'short error vector' ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 37
Provided by: JFH
Category:
Tags: control | flow

less

Transcript and Presenter's Notes

Title: Flow of Control


1
Lecture 6
  • Flow of Control
  • Regression

2
Control flow
  • Telling what should happen next in a program
  • Invoking a function is an example
  • for loops are another example
  • ifthen is the other important one

3
ifthen
  • Two-sided, unfair, coin
  • x rand(1)
  • if (x gt 0.6)
  • red
  • else
  • blue
  • end

4
Three-sided coin
  • Three-sided, unfair, coin
  • x rand(1)
  • if (x lt 0.3)
  • red
  • else if (x lt 0.7)
  • blue
  • else
  • green
  • end
  • end

5
Better
  • x rand(1)
  • if (x lt 0.3)
  • red
  • elseif (x lt 0.7)
  • blue
  • else
  • green
  • end
  • Avoids excess indentation

6
Concrete example
  • function res countRoots(a,b,c)
  • how many real roots does ax2 bx c 0
    have?
  • disc bb 4ac
  • if (disc gt 0))
  • res 2
  • elseif (disc 0)
  • res 1
  • else
  • res 0
  • end

7
For loops, again
  • What does this program do?
  • array
  • for i 140000
  • array array,i
  • end

8
Confusion
  • During class, students were puzzled about array
    construction.
  • Note 3, 4 concatenates the 1x1 array 3
    with the 1x1 array 4 to get
  • 3 4
  • Remember that EVERYTHING in matlab is an array,
    even if it doesnt look like one

9
Again
  • array
  • for i 140000
  • array array,i
  • end
  • Compare with
  • array 140000
  • Second form is MUCH faster!

10
Gauss-in-a-box model
  • Suppose that size(a) 1 n
  • a a, i
  • The right hand side evaluates to a 1 x (n1)
    array
  • Gauss asks the operating system for room to write
    it
  • He writes it down, and then labels it a
  • He crosses out the previous value of a.

11
How many steps?
  • array
  • for i 140000
  • array array,i
  • end
  • First time 1 number must be copied
  • Second time 2 numbers must be copied
  • 40000th time 40,000 numbers must be copied
  • Total 1 2 3 40,000 .8 billion steps!

12
While loops
  • Repeats a set of steps while some condition is
    true
  • function powerTwo(n)
  • find a number k with 2k gt n
  • k 0
  • while (2k lt n)
  • k k1
  • end

13
Vectors and If
  • Suppose you write a procedure to implement this
  • if x gt 10, y 4
  • if x lt 10, y 1

14
Code
  • function y foo(x)
  • if x gt 10
  • y 4
  • else
  • y 1
  • end

15
Now vectorize
  • Youd like to be able to use foo on vectors,
    too, so that
  • foo(1 15 6) gt 1 4 1
  • Wont work

16
Solution
  • function y foo(x)
  • vectorized version of foo
  • y 4(x gt 10) 1(x lt 10)
  • x 1 15 6
  • (xgt10) 0 1 0
  • 4(xgt10) 0 4 0
  • (x lt 10) 1 0 1
  • 1 (x lt 10) 1 0 1
  • y 1 4 1

17
Improved code
  • function y foo(x)
  • vectorized version of foo
  • select x gt 10
  • y 4select 1(select)

18
Ive got some data
  • Fifty x-y pairs, from an experiment I ran
  • I THINK they all fit on a line

19
Given these xs and ys, find line
  • Lets start with a simpler case
  • x 1 2 3
  • y 2 6 7

20
Red lines errors
21
Best line?
  • Solution 1 minimize the sum of the errors.
  • Sum of blues sum of reds!

22
Best line, version 2
  • Minimize sum of SQUARES of errors.
  • Called least squares fitting
  • If you think of the list of errors as entries in
    a vector, youre making this vector as short as
    possible
  • Rationale
  • Corresponds to what we do instinctively
  • Provides a UNIQUE best-fit line
  • Corresponds to the short error vector

23
A little math (in matlab notation)
  • Weve got points x(13) and y(13). (n 3)
  • We hope to find numbers A and B withAx(i) B
    y(i) for each i
  • Or better let e(i) (Ax(i) B) y(i)
  • Find values of A and B to make the vector e
    short.

24
Setup
  • minimize E(A, B) sum(e . 2)
  • where e(i) (Ax(i) B) y(i)
  • We get to vary A and B and see how E changes
  • We could plot E as a function of A and B

25
Minimization
  • To minimize E, we take derivatives
  • dE/dA 2SUM( (Ax B y) . x )
  • dE/dB 2SUM( (Ax B y) )
  • and set to zero
  • 2SUM( (Ax B y) ) 0
  • gt Asum(x) Bn sum(y) 0

X
Y
26
  • 2SUM( (Ax B y) . x ) 0 gt
  • A sum(x . x) B sum(x) sum(y . x) 0

XY
X2
27
Summary
  • X2 A X B XY 0
  • X A n B Y 0
  • In matrix form
  • X2 XA XY
  • X nB Y

28
Convert to Matlab
  • function A, B findline(x, y)
  • find the coeffs of best-fit line for data
  • X2 dot(x, x)
  • XY dot(x, y)
  • X sum(x)
  • Y SUM(y)
  • n numel(x)
  • M X2 X X n
  • v XY Y
  • res M\v
  • a res(1) b res(2)

29
Different formulation
  • Want
  • Ax(i) B y(i) (i 1, , n)
  • Write as
  • x(i) A 1 B y(i)
  • In matrix form
  • x(1) 1A y(1)
  • x(2) 1B y(2)
  • x(n) 1 y(n)
  • Mv y, where M and y are known, and v is A B.

30
Solving Mv y
  • M is n x 2.
  • If there was a solution, then it would also solve
  • MM v M y (multiplied both sides by M)
  • i.e.
  • v (MM)-1 M y
  • MM is 2x2, so its cheap to compute its inverse

31
Findline2
  • function A, B findline2(x, y)
  • find the coeffs of best-fit line for data
  • x x() convert x to column vector
  • y y()
  • M x, ones(size(x))
  • U inv(MM) M
  • v U \ y
  • A v(1) B v(2)

32
  • In fact, (MM)-1 M is a builtin for Matlab
    pinv(M).

33
Findline3
  • function A, B findline3(x, y)
  • find the coeffs of best-fit line for data
  • x x() convert x to column vector
  • y y()
  • M x, ones(size(x))
  • v pinv(M) y
  • A v(1) B v(2)

34
4th way
  • Better still, just writing v M\y gets right
    answer!
  • function A, B findline4(x, y)
  • find the coeffs of best-fit line for data
  • x x() convert x to column vector
  • y y()
  • M x, ones(size(x))
  • v M\y
  • A v(1) B v(2)

35
Finally
  • The folks in Natick wrapped it up in a package
    for you
  • p polyfit(x, y, 1)
  • Fits a first-degree polynomial to x and y.

36
Try this
  • x 12100
  • y 4.5 x.2 420 . x 12 30 rand(1,
    numel(x) 0.5)
  • plot(x, y, o)
  • How can you recover 4.5, -420, and 12 from this?
Write a Comment
User Comments (0)
About PowerShow.com