lecture 4 - rootfinding: newton's method in higher

44
Lecture 4 Rootfinding: Newton’s Method in higher dimensions, secant method,fractals, Matlab - fzero T. Gambill Department of Computer Science University of Illinois at Urbana-Champaign 6/16/2013 T. Gambill (UIUC) CS 357 6/16/2013 1 / 44

Upload: others

Post on 26-Feb-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 4 - Rootfinding: Newton's Method in higher

Lecture 4Rootfinding: Newton’s Method in higher dimensions, secant method,fractals,

Matlab - fzero

T. Gambill

Department of Computer ScienceUniversity of Illinois at Urbana-Champaign

6/16/2013

T. Gambill (UIUC) CS 357 6/16/2013 1 / 44

Page 2: Lecture 4 - Rootfinding: Newton's Method in higher

Newton’s Method in higher dimensions

Given f : Rm → Rm then we can consider f as a vector of m functions.

f =

f1f2...

fm

where fi : Rm → R. We can write the Taylor Series for each fi as follows.

fi(xk+1) = fi(xk) + [∇fi(xk)]T ∗ (xk+1 − xk) + . . .

Combining these in a columnar vector gives,f1(xk+1)f2(xk+1)

...fm(xk+1)

=

f1(xk)f2(xk)

...fm(xk)

+ J ∗ (xk+1 − xk) + . . .

T. Gambill (UIUC) CS 357 6/16/2013 2 / 44

Page 3: Lecture 4 - Rootfinding: Newton's Method in higher

Newton’s Method in higher dimensions

f1(xk+1)f2(xk+1)

...fm(xk+1)

=

f1(xk)f2(xk)

...fm(xk)

+ J ∗ (xk+1 − xk) + . . .

The matrix J is called the Jacobian,

Jij =∂fi(xk)

∂xj.

In the case with one dimension, to obtain Newton’s method we ignored higherorder terms and set f (xk+1) = 0 and then solved for xk+1. We do the same forhigher dimensions to obtain the formula,

xk+1 = xk − J−1 ∗ f(xk)

Although, we will later see why we should (and can) avoid computing theinverse of the Jacobian and instead solve the system of equations,

J ∗ (xk+1 − xk) = −f(xk)

T. Gambill (UIUC) CS 357 6/16/2013 3 / 44

Page 4: Lecture 4 - Rootfinding: Newton's Method in higher

Newton’s Method Example 1

Using the formula,xk+1 = xk − J−1 ∗ f(xk)

find a root for the system of equations defined by,

f1(x) = x1 + 2x2 − 2 = 0f2(x) = x2

1 + 4x22 − 4 = 0

(The solution of this system is [0, 1]T.) The Jacobian is given by,

J(x) =[

1 22x1 8x2

]If we choose [1, 1]T as a starting guess then we generate the following valuesfor each iteration as shown on the next slide.

T. Gambill (UIUC) CS 357 6/16/2013 4 / 44

Page 5: Lecture 4 - Rootfinding: Newton's Method in higher

Newton’s Method Example 1

T. Gambill (UIUC) CS 357 6/16/2013 5 / 44

Page 6: Lecture 4 - Rootfinding: Newton's Method in higher

Newton’s Method Example 2

Using the formula,xk+1 = xk − J−1 ∗ f(xk)

find a root for the system of equations defined by,

f1(x) = x1 + 2x2 − 2 = 0f2(x) = −2x1 + x2 − 4 = 0

(The solution of this system is [−1.2, 1.6]T.) The Jacobian is given by,

J(x) =[

1 2−2 1

]Why?If we choose [1, 1]T as a starting guess then we generate the following valuesfor each iteration shown on the next slide.

T. Gambill (UIUC) CS 357 6/16/2013 6 / 44

Page 7: Lecture 4 - Rootfinding: Newton's Method in higher

Newton’s Method Example 2

T. Gambill (UIUC) CS 357 6/16/2013 7 / 44

Page 8: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: What?

DefinitionFractal A mathematical pattern (geometric object) that is reproducible at anylevel of magnification or reduction.

DefinitionFractal A term used by Benoit Mandelbrot to refer to geometric objects withfractional dimensions rather than integer dimensions. Also used ”fractal” torefer to shapes that are self-similar: they look the same at any zoom level.

T. Gambill (UIUC) CS 357 6/16/2013 8 / 44

Page 9: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: Application

Scientifically used to describe highly irregular objectsfractal image compressionSeismologyCosmologylife sciences:

I clouds and fluid turbulenceI treesI coastlines

More interesting observations:New music/New artVideo games/graphicsChaos theorythe Butterfly effect: small changes produces large effects

T. Gambill (UIUC) CS 357 6/16/2013 9 / 44

Page 10: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: Air PressureAir channels between two glued pieces of acrylic

T. Gambill (UIUC) CS 357 6/16/2013 10 / 44

Page 11: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: high voltage dielectric breakdownLichtenberg: Branching discharges decrease to hairlike then to molecular

T. Gambill (UIUC) CS 357 6/16/2013 11 / 44

Page 12: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: Microwaving a CDHeat vaporizes the aluminum leaving fractal metallic islands

T. Gambill (UIUC) CS 357 6/16/2013 12 / 44

Page 13: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: Romanesco Broccoligrowth follows fractal pattern

T. Gambill (UIUC) CS 357 6/16/2013 13 / 44

Page 14: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: Treesstructure follows fractal pattern

T. Gambill (UIUC) CS 357 6/16/2013 14 / 44

Page 15: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: JupiterAtmosphere modeled with fractals

T. Gambill (UIUC) CS 357 6/16/2013 15 / 44

Page 16: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: CavesStalactite/Stalagmite formation

T. Gambill (UIUC) CS 357 6/16/2013 16 / 44

Page 17: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: CanyonsErosion patter

T. Gambill (UIUC) CS 357 6/16/2013 17 / 44

Page 18: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: Cloudsvisualization

T. Gambill (UIUC) CS 357 6/16/2013 18 / 44

Page 19: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: Fernsgrowth

T. Gambill (UIUC) CS 357 6/16/2013 19 / 44

Page 20: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: Big Treesgrowth

T. Gambill (UIUC) CS 357 6/16/2013 20 / 44

Page 21: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: leavesstructure

T. Gambill (UIUC) CS 357 6/16/2013 21 / 44

Page 22: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: lightningformation

T. Gambill (UIUC) CS 357 6/16/2013 22 / 44

Page 23: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: cauliflowerstructure

T. Gambill (UIUC) CS 357 6/16/2013 23 / 44

Page 24: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: mountainformation

T. Gambill (UIUC) CS 357 6/16/2013 24 / 44

Page 25: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: mountainvisualization

T. Gambill (UIUC) CS 357 6/16/2013 25 / 44

Page 26: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: Norwegian riversstructure

T. Gambill (UIUC) CS 357 6/16/2013 26 / 44

Page 27: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: waterfallspattern

T. Gambill (UIUC) CS 357 6/16/2013 27 / 44

Page 28: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: coastlinesstructure

T. Gambill (UIUC) CS 357 6/16/2013 28 / 44

Page 29: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: Math

Recall Complex Numbers: z ∈ C means

z = x + iy,

where i =√−1

Things to notice:still think of the x-y plane, but now it’s in C1 instead of R2

f (z) = z2 + 1 has two roots: z1,2 = ±i

f (z) = z3 + 1 has three roots: z1 = −1, z2,3 = −1±i√

32

f (z) = z4 + 1 has four roots: z1,2 = ±√

2+i√

22 , z3,4 = ±

√2−i√

22

T. Gambill (UIUC) CS 357 6/16/2013 29 / 44

Page 30: Lecture 4 - Rootfinding: Newton's Method in higher

Fractals: Newton’s AlgorithmThe big idea:

Take a complex function like f (z) = z3 + 1Pick a bunch of initial guesses z1 as the rootsRun Newton’s MethodThe initial guesses z1 will each converge to one of n = 3 rootsColor each guess in the plane depending on the root to which itconverged

T. Gambill (UIUC) CS 357 6/16/2013 30 / 44

Page 31: Lecture 4 - Rootfinding: Newton's Method in higher

Secant Method

Given two guesses xk−1 and xk, the next guess at the root is where the linethrough f (xk−1) and f (xk) crosses the x axis.

T. Gambill (UIUC) CS 357 6/16/2013 31 / 44

Page 32: Lecture 4 - Rootfinding: Newton's Method in higher

Secant Method

Given

xk = current guess at the rootxk−1 = previous guess at the root

Approximate the first derivative with

f ′(xk) ≈f (xk) − f (xk−1)

xk − xk−1

Substitute approximate f ′(xk) into formula for Newton’s method

xk+1 = xk −f (xk)

f ′(xk)

to get

xk+1 = xk − f (xk)

[xk − xk−1

f (xk) − f (xk−1)

]T. Gambill (UIUC) CS 357 6/16/2013 32 / 44

Page 33: Lecture 4 - Rootfinding: Newton's Method in higher

Secant Method

Two versions of this formula are (equivalent in exact math)

xk+1 = xk − f (xk)

[xk − xk−1

f (xk) − f (xk−1)

](?)

andxk+1 =

f (xk)xk−1 − f (xk−1)xk

f (xk) − f (xk−1)(??)

Equation (?) is better since it is of the form xk+1 = xk − f (xk)∆. Even if ∆ isinaccurate the change in the estimate of the root will be small at convergencebecause f (xk) will also be small.Equation (??) is susceptible to catastrophic cancellation:

f (xk)→ f (xk−1) as convergence approaches, so cancellation error indenominator can be large.|f (x)|→ 0 as convergence approaches, so underflow is possible

T. Gambill (UIUC) CS 357 6/16/2013 33 / 44

Page 34: Lecture 4 - Rootfinding: Newton's Method in higher

Secant Algorithm

1 initialize: x1 = . . ., x2 = . . .2 for k = 2, 3 . . .3 xk+1 = xk − f (xk)(xk − xk−1)/(f (xk) − f (xk−1))4 if converged , stop

5 end

T. Gambill (UIUC) CS 357 6/16/2013 34 / 44

Page 35: Lecture 4 - Rootfinding: Newton's Method in higher

Secant Example

Solve:x − x1/3 − 2 = 0

Python produces the root 3.521379706804568.

Conclusions:Converges almost as quickly as Newton’s method (r = 1+

√5

2 ≈ 1.62).There is no need to compute f ′(x).The algorithm is simple.Two initial guesses are necessaryIterations are not guaranteed to stay inside an ordinal bracket.

T. Gambill (UIUC) CS 357 6/16/2013 35 / 44

Page 36: Lecture 4 - Rootfinding: Newton's Method in higher

Divergence of Secant Method

x1 x2

f(x3)

x3

f(x2)

f (x1)

f '(x) ≈ 0

Since

xk+1 = xk − f (xk)

[xk − xk−1

f (xk) − f (xk−1)

]the new guess, xk+1, will be far from the old guess whenever f (xk) ≈ f (xk−1)and |f (x)| is not small.

T. Gambill (UIUC) CS 357 6/16/2013 36 / 44

Page 37: Lecture 4 - Rootfinding: Newton's Method in higher

Summary

Plot f (x) before searching for roots

Bracketing finds coarse interval containing roots and singularities

Bisection is robust, but converges slowly

Newton’s MethodI Requires f (x) and f ′(x).I Iterates are not confined to initial bracket.I Converges rapidly (r = 2).I Diverges if f ′(x) ≈ 0 is encountered.

Secant MethodI Uses f (x) values to approximate f ′(x).I Iterates are not confined to initial bracket.I Converges almost as rapidly as Newton’s method (r ≈ 1.62).I Diverges if f ′(x) ≈ 0 is encountered.

T. Gambill (UIUC) CS 357 6/16/2013 37 / 44

Page 38: Lecture 4 - Rootfinding: Newton's Method in higher

fzero Function

fzero is a hybrid method that combines bisection, secant and reversequadratic interpolation

1 r = fzero(’fun’,x0)

2 r = fzero(’fun’,x0,options)

3 r = fzero(’fun’,x0,options,arg1,rg2,...)

x0 can be a scalar or a two element vectorIf x0 is a scalar, fzero tries to create its own bracket.If x0 is a two element vector, fzero uses the vector as a bracket.

T. Gambill (UIUC) CS 357 6/16/2013 38 / 44

Page 39: Lecture 4 - Rootfinding: Newton's Method in higher

Reverse Quadratic Interpolation

Find the point where the x axis intersects the sideways parabola passingthrough three pairs of (x, f (x)) values.

0 0.5 1 1.5 2−5

0

5

10

15

20

T. Gambill (UIUC) CS 357 6/16/2013 39 / 44

Page 40: Lecture 4 - Rootfinding: Newton's Method in higher

fzero Function

fzero chooses next root asResult of reverse quadratic interpolation (RQI) if that result is inside thecurrent bracket.Result of secant step if RQI fails, and if the result of secant method is ininside the current bracket.Result of bisection step if both RQI and secant method fail to produceguesses inside the current bracket.

T. Gambill (UIUC) CS 357 6/16/2013 40 / 44

Page 41: Lecture 4 - Rootfinding: Newton's Method in higher

fzero Function

Optional parameters to control fzero are specified with the optimset function.

Tell fzero to display the results of each step:

1 >> options = optimset(’Display’,’iter’);

2 >> x = fzero(’myFun’,x0,options)

Tell fzero to use a relative tolerance of 5× 10−9:

1 >> options = optimset(’TolX’,5e-9);

2 >> x = fzero(’myFun’,x0,options)

Tell fzero to suppress all printed output, and use a relative tolerance of5× 10−4:

1 >> options = optimset(’Display’,’off’,’TolX’,5e-4);

2 >> x = fzero(’myFun’,x0,options)

T. Gambill (UIUC) CS 357 6/16/2013 41 / 44

Page 42: Lecture 4 - Rootfinding: Newton's Method in higher

fzero Function

Allowable options (specified via optimset):

Option type Value Effect

’Display’ ’iter’ Show results of each iteration

’final’ Show root and original bracket

’off’ Suppress all print out

’TolX’ tol Iterate until

|∆x| < max [tol, tol ∗ a, tol ∗ b]where ∆x = (b − a)/2, and [a, b] is the current bracket.

The default values of ’Display’ and ’TolX’ are equivalent to

1 options = optimset(’Display’,’iter’,’TolX’,eps)

T. Gambill (UIUC) CS 357 6/16/2013 42 / 44

Page 43: Lecture 4 - Rootfinding: Newton's Method in higher

fzero example

Takef (x) = x10 − 1

1 >> f = @(x)x.ˆ10 - 1;

2 >> options = optimset(’display’,’iter’);

3 >> [x,fx]=fzero(f,0.5,options)

T. Gambill (UIUC) CS 357 6/16/2013 43 / 44

Page 44: Lecture 4 - Rootfinding: Newton's Method in higher

Instructor Notes

Approximating df(x)dx ≈

Im(f(x+ih))h where i =

√−1 and h ∈ R where h ≈ 0

T. Gambill (UIUC) CS 357 6/16/2013 44 / 44