ma/cs375 fall 2002 1 ma/cs 375 fall 2002 lecture 7

38
MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

Upload: marilyn-kimberly-mathews

Post on 17-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 1

MA/CS 375

Fall 2002

Lecture 7

Page 2: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 2

Explanations of Team Examples

Page 3: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 3

• Consider:

• What should its behavior be as:

• Plot this function at 1000 points in:

• Plot this function at 1000 points in:

• Explain what is going on.

1 1xf x

x

0x

1,1x

4 ,4

Recall Monster #1

Page 4: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 4

Monster #1((large+small)-large)/small

Page 5: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 5

when we zoom in we see that the large+small operation is introducing order eps errors which we then divide with eps to get O(1) errors !.

Monster #1((large+small)-large)/small

Page 6: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 6

when we zoom in we see that the large+small operation is introducing order eps errors which we then divide with eps to get O(1) errors !.

Each stripe is a regionwhere 1+ x is a constant(think about the gaps between

numbers in finite precision)

Then we divide by x and the stripes look like hyperbola.

The formula looks like (c-1)/x with a new c for each stripe.

Monster #1((large+small)-large)/small

Page 7: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 7

Recall Monster #2

• Consider:

• What should its behavior be as:

• Plot this function at 1000 points in:

• Explain what is going on in a text box, label everything, print it out and hand it in.

log(1 )x xf x e e

x

0,50x

Page 8: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 8

Limit of log(1 )x xf x e e

log 1lim lim log 1 lim

1lim by l'Hopital's rule

1lim 1

1

x

x xxx x x

x

x

xx

xx

ef x e e

e

eee

e

Page 9: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 9

Monster #2(finite precision effects from large*small)

1 as f x x

As x increases past 30 we seethat f deviates from 1 !!

Page 10: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 10

As x increases past ~=36 we see that f drops to 0 !!

Monster #2 cont(finite precision effects from large*small)

Page 11: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 11

• Consider:

• What should its behavior be as:

• Plot this function at 1000 points in:

• Explain what is going on. What happens at x=54?

22 log (1 2 )x xf x

x

0,60x

Recall Monster #3

Page 12: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 12

Monster 3(finite precision large*small with binary stripes)

Page 13: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 13

As we require more than 52 bits to represent 1+2^(-x) we see that the log term drops to 0.

Monster 3(finite precision large*small with binary stripes)

Page 14: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 14

• Consider:

• What should its behavior be as:

• Plot four subplots of the function at 1000 points in: for

• Now fix x=0.5 and plot this as a function of for

• Explain what is going on, print out and hand in.

sin sincos

x xg x x

0

0,2*x 1 4,1 7,1 12,1 15e e e e

0.1,0.01,0.001,...,1 20e

RecallMonster #4

Page 15: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 15

Monster 4 cont

0 0

0

sin sin sin cos cos( )sin sinlim lim

sin cos 1 cos( )sinlim

cos

x x x x x

x x

x

Behavior as delta 0 :

or if you are feeling lazy use the definition of derivative, and remember: d(sin(x))/dx = cos(x)

Page 16: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 16

Monster 4 cont(parameter differentiation, delta=1e-4)

OK

Page 17: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 17

Monster 4 cont (parameter differentiation, delta=1e-7)

OK

Page 18: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 18

Monster 4 cont (parameter differentiation, delta=1e-12)

Worse

Page 19: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 19

Monster 4 cont (parameter differentiation, delta=1e-15)

When we make the delta around about machine precision we see O(1) errors !.

Bad

Page 20: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 20

Monster 4 cont (numerical instablitiy of parameter differentiation)

As delta gets smaller we see that the approximation improves, until delta ~= 1e-8 when it gets worse and eventually the approximate derivate becomes zero.

Page 21: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 21

Approximate Explanation of Monster #4

2

2

'

ˆˆ ˆ

( )

ˆ ˆ ˆ ( )

( )

ˆ ˆˆ ˆ

'

f x f x f x O

y x

x O

f y f y O

f x O

f y f x f y f x O

f x O O

1) Taylor’s thm:

2) Round off errors

3) Round off in computation of f and x+delta

4) Put this together:

Page 22: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 22

2ˆ ˆˆ ˆ '

ˆ ˆˆ ˆ

f y f x f x O O

df x f y f xO O

dx

i.e. for or equivalently approximation error decreases as delta decrease in size.

BUT for round off dominates!.

810

810

Page 23: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 23

Ok – so these were extreme cases

• Reiteration: the numerical errors were decreasing as delta decreased until delta was approximately 1e-8

Page 24: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 24

Matlab Built-in Derivative Routines

• diff takes the derivative of a function of one variable sampled at a set of discrete points

• gradient takes the x and y derivatives of a function of two variables

Page 25: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 25

diffdemo.m

Using diff on

F = x^3

diff

Page 26: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 26

Page 27: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 27

diff

diffdemo.m

Using diff on

F = sin(x)

Page 28: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 28

Page 29: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 29

gradient

gradientdemo.m

Using gradient on

F = x^2

Page 30: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 30

Page 31: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 31

Using gradient on

F = x^2+y^2

gradientdemo1.m

Page 32: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 32

Page 33: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 33

Using gradient on

F = (x^2)*(y^2)

gradientdemo2.m

Page 34: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 34

Page 35: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 35

Using gradient on

F = (sin(pi*x))*(cos(pi*y))

gradientdemo3.m

Page 36: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 36

Page 37: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 37

Comments

• Next time we will revisit the accuracy of taking derivatives…

Page 38: MA/CS375 Fall 2002 1 MA/CS 375 Fall 2002 Lecture 7

MA/CS375 Fall 2002 38

Summary

• Ok – so in the far limits of the range of finite precision really dodgy things happen.

• But recall, the formula for the derivative of sine worked pretty well for a large range of numbers.

• Try to avoid working with two widely separated numbers.