75
ENGG1811 © UNSW, CRICOS Provider No: 00098G1 W9 slide 1 Week 9 Matlab: Algorithms, numerical precision, animation, debugging ENGG1811 Computing for Engineers

Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

  • Upload
    ngotram

  • View
    222

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

ENGG1811 © UNSW, CRICOS Provider No: 00098G1 W9 slide 1

Week 9 Matlab: Algorithms, numerical precision, animation, debugging

ENGG1811 Computing for Engineers

Page 2: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

An engineering challenge

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 2

http://www.zdnet.com/sydneys-harbour-bridge-gets-sensor-tech-7000000296/ http://www.engineeringchallenges.org/cms/8996/9136.aspx

Page 3: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Maintenance and monitoring aging infrastructure is a grand research challenge

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 3

http://www.zdnet.com/sydneys-harbour-bridge-gets-sensor-tech-7000000296/

Page 4: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Limitations of computation

•  Your computer can do almost 100 billion multiplications in one second

•  Tiny computers can do far less –  Need efficient or new algorithms

•  In any case, we want efficient algorithms

•  Computers also have finite precision

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 4

Page 5: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

This week

•  Three topics –  Algorithms –  Numerical precision –  Animation

•  Matlab programming –  While –  Animation –  More Matlab functions

•  Computer science concepts –  Efficient algorithms/computational complexity –  Finite precision

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 5

Page 6: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Algorithms •  A sequence of instructions for the computation

•  Two important criteria –  Correctness –  Efficiency

•  Example: An algorithm for multiplying 2 integers –  Correctness means the algorithm returns the correct

answer all the time –  Efficiency: How many multiplications the algorithm can do

in a given amount of time

•  An efficient algorithm takes a shorter time to arrive at the correct outcome

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 6

Page 7: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Challenge: Locate a name in a sorted list of names

•  You are given: –  A list of names arranged in alphabetical order –  Names are indexed with 1, 2, 3 etc. in their order

•  Rules –  You are not allowed to see the list –  You can choose an index and query what the name at that

index is

•  The challenge: –  Given a name, what is the minimum number of indices that

you need to query to locate that name?

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 7

1.  Abraham

2.  Adam

3.  Eve

4.  Sarah

Page 8: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Example: Simple Scan

1.  Henry

2.  John

3.  Michael

4.  Peter

5.  Tinker

6.  Wendy

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 8

•  Example: –  There is a list with 6 names on the left –  Given Peter is one of the names, you

want to find which index it is at

•  A simple algorithm is to scan the name one by one from the beginning until you have found the name

•  Quiz: If the name that you want to locate is at the k-th position, how many queries do you need to locate the name using simple scan? k

•  Which type of loop will you use to implement a simple scan? while

Page 9: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Other possible algorithms

•  Make a guess of where the name is and then start from there –  Example: 6 names on the left. The

name to locate is Yvonne. Since this name is near the end of the alphabet so we scan from the last name.

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 9

1.  Ava

2.  Yvonne

3.  Zac

4.  Zoe

5.  Zorina

6.  Zwi •  Comment: This algorithm assumes that names are distributed in the usual way but sometimes you can get an unusual data set

•  What will be an efficient algorithms?

Page 10: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Towards a general principle •  Consider this game:

–  I think of a living person in this world –  To win this game, you need to guess who this person is in as

few questions as possible

•  Consider two sets of questions below, which one will you ask and why?

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 10

•  Is the person from Zambia?

•  Is the person from Fiji?

•  Is the person a current student of UNSW?

•  Is the person a he or she?

•  Is the person from Asia?

•  Is the person from South America?

Question set 1 Question set 2

•  Narrow down the possibilities as quickly as possible

•  How can you use this principle to locate a name quickly in a sorted list?

Page 11: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Name search using binary search

•  The purpose of the query is narrow down the possibilities as much as possible –  Idea: Eliminate half of the possibilities with each query

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 11

•  Binary search: –  Initialization: Query the name in the middle of the list –  Eliminate nearly half of the possibilities with each

additional query –  Stop when the name is found

Page 12: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Binary search example (1)

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 12

1.  Ava

2.  Ben

3.  Campbell

4.  Jessica

5.  John

6.  Liam

7.  Logan

8.  Peter

9.  Quentin

10. Sammy

11. Timothy

•  To eliminate half of the possibilities, pick the name in the middle

•  Middle of 1 and 11 = (1+11)/2 = 6

•  Initialisation: Query 6

1.  Ava

2.  Ben

3.  Campbell

4.  Jessica

5.  John

6.  Liam

7.  Logan

8.  Peter

9.  Quentin

10. Sammy

11. Timothy

Page 13: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Binary search example (2)

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 13

1.  Ava

2.  Ben

3.  Campbell

4.  Jessica

5.  John

6.  Liam

7.  Logan

8.  Peter

9.  Quentin

10. Sammy

11. Timothy

•  Where should we look next?

•  Can forget indices 1-5

1.  Ava

2.  Ben

3.  Campbell

4.  Jessica

5.  John

6.  Liam

7.  Logan

8.  Peter

9.  Quentin

10. Sammy

11. Timothy

Page 14: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Binary search example (3)

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 14

1.  Ava

2.  Ben

3.  Campbell

4.  Jessica

5.  John

6.  Liam

7.  Logan

8.  Peter

9.  Quentin

10. Sammy

11. Timothy

•  Search between 6-11

•  Middle of 6 and 11 = (6+11)/2 = 8.5

•  Let us round up

•  Query index 9

•  Which section can we forget now?

1.  Ava

2.  Ben

3.  Campbell

4.  Jessica

5.  John

6.  Liam

7.  Logan

8.  Peter

9.  Quentin

10. Sammy

11. Timothy

Page 15: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Binary search example (4)

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 15

1.  Ava

2.  Ben

3.  Campbell

4.  Jessica

5.  John

6.  Liam

7.  Logan

8.  Peter

9.  Quentin

10. Sammy

11. Timothy

•  Can forget 10-11

•  Quiz: What is the next index to query? –  (9+6)/2 = 7.5 –  Round up to 8 –  Query index 8

1.  Ava

2.  Ben

3.  Campbell

4.  Jessica

5.  John

6.  Liam

7.  Logan

8.  Peter

9.  Quentin

10. Sammy

11. Timothy

Page 16: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Binary search: A detail

•  Note that when we select the mid-point, we have chosen to round up

•  We can also choose to round down

•  As long as one consistent rounding method is used throughout the algorithm, that’s fine

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 16

Page 17: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Algorithmic complexity

•  Computer scientists are very interested in how the complexity of algorithms –  Roughly speaking, higher complexity

translates to a longer running time on the same computer

•  Computer scientists like to derive efficient algorithms

•  For the name search example earlier: –  Binary search needs 3 queries –  Simple scan needs 8 queries

•  The difference does not appear to be a lot for this example, but let us increase the size of the list

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 17

1.  Ava

2.  Ben

3.  Campbell

4.  Jessica

5.  John

6.  Liam

7.  Logan

8.  Peter

9.  Quentin

10. Sammy

11. Timothy

?

Page 18: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Demo

•  I took all the first names of the all students enrolled in ENGG1811 in 15s2

•  Remove all duplicates and sort the names

•  There are 412 unique names

•  A Matlab function –  Will randomly pick 10 names –  Uses simple scan and binary search to locate those 10

names –  The function will also report the number of queries

made by each method

•  There are a number of points that I’d like you to think about when you watch the demo (next slide)

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 18

Page 19: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

A number of questions

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 19

•  Is binary search always better?

•  What is the largest number of queries required by –  simple scan –  binary search

Page 20: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Number of queries required by binary search

•  Each query reduces the number of possibilities by half

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 20

# queries Remaining # possibilities after the query 1 412 * (1/2) 2 3

412 * (1/2) * (1/2)

412 * (1/2) * (1/2) * (1/2)

•  After n additional queries, # possibilities = 412 * (1/2)n

•  Finished when only one possibility left

412 * (1/2)n ≤ 1 ! n ≥ log2(412) ! n ≥ 9

•  Maximum queries needed = 9

Page 21: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Worst case complexity

•  A way to measure the efficiency of an algorithm is to look at its worst case complexity

•  If there are n names in a sorted list –  Worst case complexity for

•  Simple scan is n

•  Binary search log2(n)

•  Computer scientists also use other ways to measure complexity, such as average complexity

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 21

Page 22: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Quiz: Which is more efficient?

Binary: query one name and eliminate half of the names at a time

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 22

“Quad”-nary: query three names and eliminate 3 quarters of the names at a time

Page 23: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 23

Data load/save

•  Data can be saved in two simple formats –  .mat, a Matlab-specific format that preserves names,

types and values from one or more variables –  ascii (text) format, data values only but readable with

other applications

>>  save  mydata.mat  image  h  vec          %  .mat  format  >>  save  export.dat  image  –ascii        %  data  only,  space  separated    >>  load  mydata.mat    %  reloads  image,  h  and  vec  >>  load  export.dat      %  stores  result  in  variable  export  

•  Loading from a mat file restores the saved variables, overwriting them if necessary

•  Loading from a text file (any extension except .mat) stores into a variable named after the file

Page 24: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 24

round / ceil / floor

•  Matlab has 3 functions for rounding –  round(x): round to the nearest integer of x –  ceil(x): round to the nearest integer bigger than or

equal to x –  floor(x): : round to the nearest integer smaller than

or equal to x

>>  round(1.4)        %  =  1  >>  round(1.5)        %  =  2    >>  ceil(1.4)    %  2    >>  ceil(1.5)      %  2  >>  ceil(1)            %  1  

>>  floor(1.4)        %  =  1  >>  floor(1.5)        %  =  1    

Page 25: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 25

rand()

•  Matlab function rand() generates random numbers in the interval (0,1) –  Always between 0 and 1. Will never be 0 nor 1.

>>  rand              %  one  random  number  >>  rand(2,3)    %  a  2-­‐by-­‐3  random  matrix,  6  random  numbers    

>>  ceil(n*rand(10,1))  %  10  random  integers  in  [1,n]        

•  If you want random integers in [1,n] (assuming n has been defined) –  rand is between (0,1) –  n*rand is between (0,n) –  Round up to get a random integer in [1,n]

Page 26: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 26

Iteration – while

•  Same principle as the while statement in OOB

while  BooleanExpression        statements  

end  

Page 27: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Finding roots

•  It’s easy to find the roots of polynomial equations in Matlab

•  Example: To find the roots of the polynomial equation 4x3 + 12 x2– 64 x + 16

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 27

>>  roots([4  12  -­‐64  16])  

•  For the sake of learning algorithms, we look at two different ways to find roots in Matlab –  By simple scan –  By bisection search

Page 28: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Polynomial 4x3 + 12 x2– 64 x + 16

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 28

Page 29: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Finding a root by simple scan •  Evaluate the polynomial y(x) at many values of x:

–  f(x1), f(x2), f(x3), f(x4), f(x5), …

•  Find the value of xk such that f(xk) is closest to zero

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 29

•  How to measure distance from 0? –  Distance of 0.2 to 0 is 0.2 –  Distance of -0.1 to 0 is 0.1

•  What function can you use to compute distance from 0?

Absolute value abs()

•  Equivalent problem: Find the value of xk such that |f(xk)| is smallest

Page 30: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Matlab code

•  Matlab code for finding roots by simple scan is in the file findRootByScan.m

•  It uses the function min()

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 30

>>  [minvalue,  index]  =  min([21  4  12  64  16])    minvalue  =            4      index  =          2    

         

Page 31: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Another application of bisection search

•  Bisection search can be used to find the root of a continuous function f(x)

•  Assuming you know two numbers x1 and x2 such that f(x1) < 0 and f(x2) > 0 then there is a root of f(x) = 0 between x1 and x2

•  We don’t know where the root is, we find the mid-point

of x1 and x2 , and eliminate half of the interval

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 31

221 xxxmid

+= x1

x2

xmid

f

Page 32: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Bisection search: updating, case 1

ENGG1811 © UNSW, CRICOS Provider No: 00098G W7 slide 32

x

f (x)

x1

x2

xmid

•  Compute xmid = (x1+x2)/2

•  If f(xmid) < 0, then replace x1 by xmid

Page 33: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Bisection search: updating, case 2

ENGG1811 © UNSW, CRICOS Provider No: 00098G W7 slide 33

x

f (x)

x1

x2 xmid

•  Compute xmid = (x1+x2)/2

•  If f(xmid) > 0, then replace x2 by xmid

Page 34: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Stopping criterion

ENGG1811 © UNSW, CRICOS Provider No: 00098G W7 slide 34

x

f (x)

x1

x2

•  Stop when x1 and x2 are close enough

•  Stop if abs(x1 – x2) <= TOL where TOL is a pre-defined tolerance

•  Question: What is the loop-guard for the while statement?

while abs(x1 – x2) > TOL

% continue to iterate

end

A)  abs(x1 – x2) <= TOL

B)  abs(x1 – x2) > TOL

Page 35: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

ENGG1811 © UNSW, CRICOS Provider No: 00098G W7 slide 35

Bisection Pseudocode:

choose  x1  and  x2  such  that  f(x1)  <  0  and  f(x2)  >  0  

while  Abs(x1-­‐x2)  >  TOL  

 xmid  =  (x1  +  x2)  /  2  

 if  f(xmid)  <  0  

   x1  =  xmid  

 else  

   x2  =  xmid  

 end  

end  

             

Page 36: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Matlab implementation

•  A Matlab implementation is in findRootByBisection_prelim.m

•  We will complete it in the lecture

•  It uses the Matlab function polyval which calculates the value of the polynomial y(x) at given values of x

•  Example: Want to know the value of y(x) = 4x3 + 12 x2– 64 x + 16 at x = 2

•  Specify the polynomial with a vector containing the coefficients in descending power

•  polyval([4 12 -64 16],2)

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 36

Page 37: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Comments on

•  The program has a limitation –  The script assumes that f(x1)  <  0  and  f(x2)  >  0  

but  it  is  possible  to  write  code  so  that  it  can  work  for  either  of  the  following:  •  f(x1)  <  0  and  f(x2)  >  0    •  f(x1)  >  0  and  f(x2)  <  0    

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 37

Page 38: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Comments on root finding

•  Using bisection search to find a root requires you to specify two points which bound the root

•  If you recall Goal Seek from OpenOffice Calc, you specify only one starting point

•  There are algorithms which require you to specify one starting point and find the “closest” root. These algorithms require advance calculus to understand you will learn them later years

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 38

Page 39: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Iterations

•  Iteration is a very important part of computation and algorithms

•  OpenOffice Calc functions Goal Seek and Optimisation are based on iterations

•  Computation of square roots, logarithms, exponentials, trigonometric functions, matrix inverse (i.e. / or \ in Matlab) and many others are all based on iterations

•  Many iteration methods require advanced knowledge of calculus, we present a method to compute square root without calculus

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 39

Page 40: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Computing square root

•  Assume that you want to compute √A

•  Consider a rectangle with length A and width 1 –  What is the area?

•  Consider a square with length √A –  What is the area?

•  The idea is to iteratively reshape a rectangle into a square of the same area

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 40

1

A

√A

√A Same area A

Page 41: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Reshaping a rectangle into a square

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 41

Starting rectangle L

W

L1 =L +W2

W1 =LWL1

Reshape into a rectangle which looks more like a square

Both rectangles have area LW

Page 42: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Example: Computing square root of 9

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 42

L = 9.000

W = 1.000

L = 5.000

W = 1.800

L = 3.400

W = 2.647

L = 3.024

W = 2.977

Page 43: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

ENGG1811 © UNSW, CRICOS Provider No: 00098G W7 slide 43

Computing square root Pseudocode

% Computing square root of A

% Initialisation

L = A

W = 1  

while  abs(L-­‐W)  >  TOL    L  =  (L  +  W)  /  2    W  =  A/L  

end  Output  L  as  the  square  root  of  A                  

Page 44: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Writing the Matlab code

•  We will write a Matlab function called mySqrt in the lecture

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 44

•  It’s amazing that you can find the square root with just 7 lines of code!

•  People calculated square root by long division in the past! (http://nrich.maths.org/5955)

Page 45: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

ENGG1811 © UNSW, CRICOS Provider No: 00098G W7 slide 45

Question Pseudocode for computing square root

% Computing square root of A

% Initialisation

L = A

W = 1  

while  abs(L-­‐W)  >  TOL    L  =  (L  +  W)  /  2    W  =  A/L  

end  Output  L  as  the  square  root  of  A                  

Can we have L ~= W?

Page 46: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Finite precision

•  Computers have only finite precision

•  Computers use binary (i.e. 0 and 1) but you won’t be learning binary in this course

•  We will explain everything using a hypothetical computer that uses the decimal system

•  This computer uses only a finite number of digits to represent numbers

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 46

Page 47: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Finite precision representation

•  Our hypothetical computer represents 2.41 x 10-3 as:

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 47

+ 3 1 2 4 -

Decimal point is assumed to be here but not shown

•  Each number has 6 storage places –  One for sign of the number, one for sign of the exponent –  One digit before the decimal points –  Two digits after the decimal point –  One digit for the exponent

Page 48: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Some numbers are approximated

•  The computer can represent some numbers exactly, e.g 2 x 10-7 as:

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 48

+ 7 0 2 0 -

•  And some numbers approximately, e.g 1/3 = 0.3333… becomes

+ 1 3 3 3 - Finite precision

Page 49: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Insight Through Computing

Add:

+ 3 1 2 4 -

+ 3 0 1 0 -

+ 3 1 3 4 - Result:

=1 x 10-3

Page 50: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Insight Through Computing

Add:

+ 3 1 2 4 -

+ 3 0 0 1 -

+ 3 1 2 5 - Result:

=1 x 10-4

Page 51: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Insight Through Computing

Add:

+ 3 1 2 4 -

+ 3 1 0 0 -

+ 3 2 2 4 - Result:

=1 x 10-5

Page 52: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Insight Through Computing

Add:

+ 3 1 2 4 -

+ 6 0 1 0 -

+ 3 1 2 4 - Result:

=0.001 x 10-3

=1 x 10-6

Not enough room to

represent .002411

Page 53: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Losing precision in Matlab

•  Testing whether numbers are equal

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 53

>>  3  ==  3        %  Boolean  comparison,  returns  1,  which  means  true  >>  3  ==  4        %  returns  0  which  mean  false      

•  Try the following in Matlab

>>  1+2^(-­‐52)  ==  1        %  False    >>  1+2^(-­‐53)  ==  1        %  True:  2^(-­‐53)  is  lost  if  added  to  1    

>>  1000+2^(-­‐43)  ==  1000        %  False    >>  1000+2^(-­‐44)  ==  1000        %  True:  2^(-­‐44)  is  lost      

Be careful if you add or subtract a small number from a big

number

Page 54: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Hypothetical computer: Smallest positive number possible

•  The smallest possible number represented by our hypothetical computer is 0.01 x 10-9 as follows:

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 54

+ 9 1 0 0 -

•  This computer considers any number smaller 0.01 x 10-9 as zero

Page 55: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Smallest +ve number in Matlab

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 55

•  Try the following in Matlab

>>  2^(-­‐1074)  ==  0        %  False    >>  2^(-­‐1075)  ==  0        %  True:  2^(-­‐1075)  is  considered  zero    

Page 56: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Why can’t we use (x1 == x2) in checking whether two floating point numbers are equal?

•  x = 1/3 is represented 3.33 x 10-1 in our computer:

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 56

+ 1 3 3 3 -

•  Comparing y == 1 will be false

•  Need to use abs(y-1) < TOL

x

•  What is y = 3*x?

+ 1 9 9 9 - y

Page 57: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Finite precision and software design

•  Country X has an anti-missile system Y

•  It uses our hypothetical computer

•  x = 1/60 is represented as

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 57

+ 2 7 1 6 - x

External oscillator produces 1 tick per 1/60 second

Software on missile counts the number of ticks = n Time in software in seconds = n*x

Page 58: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Finite precision and software design

•  x = 1/60 is represented as

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 58

+ 2 7 1 6 - x

•  60 ticks should be 1 seconds

•  But in software, time is 60 * 1.67x10-2 = 1.02

•  0.02s difference may seem small but pretty big error for an anti-missile system

Page 59: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Tragic consequence of finite precision

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 59

•  The missile story is true but adapted –  X = United States, Y = Patriot –  But of course not our hypothetical computer but a

computer with finite precision –  Lives were lost because the anti-missile missed the

incoming missile

•  The cause is that the software designer didn’t consider finite precision

•  Question: How can you correct the problem

Page 60: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Simulation on paper – the setup

•  An object is constrained to move along a straight line

•  Time starts at 0 unit. The initial position of the object is x(0) = 1

•  The velocity v(t) at time t is: –  v(t) = 2 if 0 ≤ t <0.4 –  v(t) = -4 if 0.4 ≤ t <0.8 –  v(t) = 1 if 0.8 ≤ t

•  Determine the position of the object at t = 0.1,0.2, …, 1

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 60

At position 1 at time 0

From Week 8

Page 61: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

simulateOneD_v2.m (simulation loop)

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 61

for k = 2:length(time) pastTime = time(k-1); velocity = getVelocity(pastTime); pos(k) = pos(k-1) + velocity * dt; end

From Week 8

Page 62: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Animation

•  We want to demonstrate how the object moves

•  The principle of animating an object is –  Draw the object at its initial position –  Calculate the object new position –  Redraw the object at the new position

• Note: Matlab automatically erases the previous object

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 62

] loop

Page 63: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Draw a rectangle

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 63

% Draw the box

rectHandle = rectangle('Position',[1 2 4 3],'FaceColor','g');

% Adjust the axes

axis([0 6 0 6])

(1,2) 4

3

Code in drawABox.m

Page 64: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

simulateOneDWithAnimation.m

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 64

% A number of lines require before this – not shown

rectHandle = ...

rectangle('Position',[x(1)-BOX_WIDTH/2 0 BOX_WIDTH BOX_HEIGHT], ...

'FaceColor','g');

% the simulation loop

for i = 2:length(t)

pastTime = t(i-1);

speed = getSpeedSimulateOneD(pastTime);

x(i) = x(i-1) + speed * dt;

% Move the box

set(rectHandle, 'Position',[x(i)-BOX_WIDTH/2 0 BOX_WIDTH BOX_HEIGHT]);

drawnow;

end

Page 65: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Object properties

•  Each object has many properties. For example, for rectangle, you can set the EdgeColor, FaceColor, Curvature and many others –  To see what properties you can set for the rectangle

object, type “rectangle” in the “Search Documentation” box in the Matlab window

–  Alternatively, you can look at the online documentation: •  http://au.mathworks.com/help/matlab/ref/

rectangle.html

•  The Matlab script rectanglePropertyDemo.m shows you how to use some of the properties

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 65

Page 66: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Colour •  You can specify colour in Matlab with a single letter, e.g.

–  ‘k’ for black

•  Alternatively, you can use a vector with 3 elements to specify the fraction of red, green and blue (RGB triplet) –  [0.7 0.3 0.4] means mixing 70% red, 30% green and 40%

blue –  [0 1 0] is pure green –  [1 1 1] is white –  [0 0 0] is black

•  The script rectangleChangingColour.m shows how you can gradually change an object from one colour to another

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 66

Page 67: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Matlab graphics objects

•  In addition to rectangles, other graphics object types are: –  patch

•  For drawing polygons, see •  http://au.mathworks.com/help/matlab/ref/patch.html

–  line •  For drawing lines, see •  http://au.mathworks.com/help/matlab/ref/line.html

•  These are powerful primitives –  You can draw a curve using many tiny lines –  You can draw a surface with many tiny patches

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 67

Page 68: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Debugging functions

•  We will use an example to show you the debugging tools available in Matlab

•  The function countNumberOfPeaks is meant to count the number of peaks (think about the heartbeat example in Week 1) but there are errors in the function

•  You can’t see the values inside the function

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 68

Page 69: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Memory spaces (1)

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 69

vec k

numberOfPeaks

Memory space for countNumberOfPeaks

Page 70: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Memory spaces (2)

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 70

vecInput peakCount

Base space (Command window)

vec k

numberOfPeaks

Memory space for countNumberOfPeaks

These variables are not visible from the command window

Page 71: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Setting break point

•  Click this column to set break point in the editor

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 71

Page 72: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Function memory space •  Function memory space can be inspected

•  The prompt “K>>” means you are using the debugger

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 72

Page 73: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Debugger functionalities (1)

•  Step, continue, inspect various memory spaces

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 73

Page 74: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Debugger functionalities (2)

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 74

•  Set other types of breakpoint

Page 75: Week 9 Matlab: Algorithms, numerical precision, animation, debuggingen1811/17s2/lectures/week09/week09... · 2017-03-01 · Matlab: Algorithms, numerical precision, animation, debugging

Summary

•  Two important concepts –  Efficiency of algorithms –  Finite precision

•  Animation

•  Debugging tools

•  Acknowledgements: Slides 50-53 are courtesy of the authors of the text “Insight Through Computing”.

ENGG1811 © UNSW, CRICOS Provider No: 00098G W9 slide 75