28
Computational Computational Methods in Physics Methods in Physics PHYS 3437 PHYS 3437 Dr Rob Thacker Dr Rob Thacker Dept of Astronomy & Physics Dept of Astronomy & Physics (MM-301C) (MM-301C) [email protected] [email protected]

Computational Methods in Physics PHYS 3437

Embed Size (px)

DESCRIPTION

Computational Methods in Physics PHYS 3437. Dr Rob Thacker Dept of Astronomy & Physics (MM-301C) [email protected]. Today’s Lecture. Functions and roots: Part II Global bracket finding techniques Müller-Brent root finder Example. Global Bracket finding. - PowerPoint PPT Presentation

Citation preview

Page 1: Computational Methods in Physics  PHYS 3437

Computational Computational Methods in PhysicsMethods in Physics

PHYS 3437 PHYS 3437Dr Rob ThackerDr Rob Thacker

Dept of Astronomy & Physics Dept of Astronomy & Physics (MM-301C)(MM-301C)

[email protected]@ap.smu.ca

Page 2: Computational Methods in Physics  PHYS 3437

Today’s LectureToday’s Lecture

Functions and roots: Part IIFunctions and roots: Part II Global bracket finding techniquesGlobal bracket finding techniques Müller-Brent root finderMüller-Brent root finder Example Example

Page 3: Computational Methods in Physics  PHYS 3437

Global Bracket findingGlobal Bracket finding Dr Clarke’s Global Bracket Finding Algorithm!Dr Clarke’s Global Bracket Finding Algorithm!

The explanation is somewhat lengthy, don’t be too The explanation is somewhat lengthy, don’t be too concerned if you don’t “get it all” first time out – however concerned if you don’t “get it all” first time out – however the fundamental concepts are simplethe fundamental concepts are simple

Broad-brushBroad-brush Calculate array x(n) Calculate array x(n)

Values of independent variable x at which f(x) will be Values of independent variable x at which f(x) will be evaluatedevaluated

These values won’t necessarily be equally spaced, in fact we These values won’t necessarily be equally spaced, in fact we might expect them not to bemight expect them not to be

Store all values of f(x(n))Store all values of f(x(n)) Evaluate for all n, although this may need to be a large Evaluate for all n, although this may need to be a large

numbernumber Apply root finder to each pair of values of f(x(i))Apply root finder to each pair of values of f(x(i))

If (f(x(i))*f(x(i+1))<0 -> root between this pair If (f(x(i))*f(x(i+1))<0 -> root between this pair If no root, skip to next pairIf no root, skip to next pair

Report all roots foundReport all roots found

Page 4: Computational Methods in Physics  PHYS 3437

Issues to addressIssues to address

How do we know how large to make the How do we know how large to make the domain of x we consider?domain of x we consider? We need limits on “xWe need limits on “xminmin”, “x”, “xmaxmax”” Some problems have natural limits (see example Some problems have natural limits (see example

later)later) Roots may be all positive in which case only need Roots may be all positive in which case only need

upper boundupper bound For polynomials (∑aFor polynomials (∑annxxnn) for large x, x) for large x, xnn term term

dominatesdominates aan-1n-1+a+annx≈0 so xx≈0 so xmaxmax≥-a≥-an-1n-1/a/ann

For smallest root, aFor smallest root, a00+a+a11x ≈0 so xx ≈0 so xminmin≤-a≤-a00/a1/a1

However, in many cases we may just have to guess!However, in many cases we may just have to guess!

Page 5: Computational Methods in Physics  PHYS 3437

How many points to we How many points to we need?need?

This is equivalent to answering “how This is equivalent to answering “how big does the spacing between points big does the spacing between points need to be?”need to be?”

x(1)=xmin

A

xB

B

xC

C DxD

Page 6: Computational Methods in Physics  PHYS 3437

Selecting the step size Selecting the step size xx To be conservative we want to pick our first To be conservative we want to pick our first

step x(2)=x(1)+dx(1) to be such that there step x(2)=x(1)+dx(1) to be such that there are no roots between x(1) and x(2)are no roots between x(1) and x(2) xxBB achieves this if A corresponds to the first point achieves this if A corresponds to the first point

At each stage we should At each stage we should adaptadapt dx(i), so that if dx(i), so that if x(i+1)=x(i)+dx(i) we want to be reasonably x(i+1)=x(i)+dx(i) we want to be reasonably sure there isn’t more than one root between sure there isn’t more than one root between the two pointsthe two points xxCC achieves this but looks slightly dangerous if we achieves this but looks slightly dangerous if we

used it generally – it could cover two roots on the used it generally – it could cover two roots on the right of the diagramright of the diagram

xxDD is just too big is just too big

Page 7: Computational Methods in Physics  PHYS 3437

Evaluating an optimal Evaluating an optimal xx Needs to be sensitive to local Needs to be sensitive to local

changes in the functionchanges in the function x needs to contain ≤ 1 rootx needs to contain ≤ 1 root Finding an optimal Finding an optimal x is a bit like x is a bit like

“groping around in the dark”“groping around in the dark” Start with small stepsStart with small steps If the function feels “straight” take If the function feels “straight” take

bigger stepsbigger steps If the function has rapid changes in If the function has rapid changes in

direction then take smaller stepsdirection then take smaller steps

Page 8: Computational Methods in Physics  PHYS 3437

Follow the function Follow the function “length” to find “length” to find xx

y

x

l

2

222

2222

)('1

)('11 Since

xfx

l

xfxx

yxyxl

x

x1/2

x

x1/2

In this part of the functionhalving the size of x doesn’t have to big an impact on l/x– the step size is OK Here halving

x producesa significantchange in l/x

Page 9: Computational Methods in Physics  PHYS 3437

Quick summary of Quick summary of conditionsconditions

If If l/l/x~x~ll1/21/2//xx1/21/2 then then x is OKx is OK If If l/l/x and x and ll1/21/2//xx1/21/2 are very close then we are very close then we

may actually be able to increase the size of may actually be able to increase the size of x x

If If l/l/x and x and ll1/21/2//xx1/21/2 are “very different” are “very different” then then x needs to be decreasedx needs to be decreased We try using small We try using small x until we find a x until we find a

separation that produces the first conditionseparation that produces the first condition Remember: we are using Remember: we are using l/l/x to tell us x to tell us

what is happening to the value of the what is happening to the value of the function locallyfunction locally

Page 10: Computational Methods in Physics  PHYS 3437

Slight modification – not Slight modification – not intuitiveintuitive

We really want to consider the change in path We really want to consider the change in path length as a function of the relative change in length as a function of the relative change in x x and xand x

So rather than using f’(x)=So rather than using f’(x)=f/f/x we usex we use

This scales the derivative in terms of the x and y This scales the derivative in terms of the x and y valuesvalues Although for numerical stability reasons we don’t Although for numerical stability reasons we don’t

quite use x and f(x) (see next slide)quite use x and f(x) (see next slide) The numerical value of the derivative is given by The numerical value of the derivative is given by

x

f

xf

x

xx

xfxfxf

)(~

~

~/

)(~

/)()('

x

ixfxixf

x

f

))(())((

Page 11: Computational Methods in Physics  PHYS 3437

Efficiency & stability Efficiency & stability concernsconcerns

Using Using

Reduces the number of function calls Reduces the number of function calls significantly near x=0significantly near x=0 This was found by trial and errorThis was found by trial and error

UsingUsing

Doesn’t have a huge advantage except Doesn’t have a huge advantage except making divide by zero less likelymaking divide by zero less likely

))(,0.1max(~ ixx

|)))((||,))((max(|)(~

ixfxixfxf

Page 12: Computational Methods in Physics  PHYS 3437

Final part of algorithmFinal part of algorithm We have the mechanisms to find x(n) and the We have the mechanisms to find x(n) and the

function evaluations f(n)function evaluations f(n) Apply root finding to each pair using following Apply root finding to each pair using following

algorithmalgorithm

do i=1,n-1do i=1,n-1if (f(i)*f(i+1) .lt. 0.0) thenif (f(i)*f(i+1) .lt. 0.0) then

find root with preferred method and reportfind root with preferred method and report end ifend if end doend do

I think I will set a homework question with a I think I will set a homework question with a detailed explanation of the algorithm detailed explanation of the algorithm

Page 13: Computational Methods in Physics  PHYS 3437

Müller-Brent methodMüller-Brent method Most expensive part of root finding Most expensive part of root finding

algorithms is frequently the function algorithms is frequently the function evaluationevaluation

In Secant method n+1 guess is based upon In Secant method n+1 guess is based upon function evaluations from n and n-1 stepsfunction evaluations from n and n-1 steps We throw away the n-2 stepWe throw away the n-2 step With only two function evaluations the fitting With only two function evaluations the fitting

method must be linearmethod must be linear We could use n-2 step to give us three We could use n-2 step to give us three

points that we use for quadratic points that we use for quadratic interpolationinterpolation

Page 14: Computational Methods in Physics  PHYS 3437

Secant method compared to Secant method compared to Müller-BrentMüller-Brent

Secant method fits a straight line Secant method fits a straight line between (xbetween (x(n)(n),f(x,f(x(n)(n)) and (x) and (x(n-1)(n-1),f(x,f(x(n-1)(n-1))) Corresponds to dashed lineCorresponds to dashed line

f(x)

x(n)

x(n-1)

x(n-2)

For Müller-Brent we rely on threefunction evaluations to fit a quadraticas given by the dot-dashed-line

Page 15: Computational Methods in Physics  PHYS 3437

Fitting a quadratic Fitting a quadratic

Three points is enough data to fit a function Three points is enough data to fit a function of the formof the form

Since we know q(x) passes through all Since we know q(x) passes through all three points we havethree points we have

Which gives two equations for two Which gives two equations for two unknowns, a & bunknowns, a & b

)()()()()(

)()()()()(

)()()(

)()()(

)()()2(2)()2()2()2(

)()()1(2)()1()1()1(

)()()(

)(2)(

nnnnnnn

nnnnnnn

nnn

nn

xfxxbxxaxfxq

xfxxbxxaxfxq

xfcxfxq

cxxbxxaxq

Page 16: Computational Methods in Physics  PHYS 3437

Solving for a & bSolving for a & b

Given the two equations we can eliminate a Given the two equations we can eliminate a or b to get equations for the two constantsor b to get equations for the two constants

We now have a,b,c so we can use the We now have a,b,c so we can use the general formula to calculate the zeros for general formula to calculate the zeros for xx(n+1)(n+1)-x-x(n)(n)

))()((

)]()([)()]()([)(

))()((

)]()()[()]()()[(

)()1()()2()1()2(

)()2(2)()1()()1(2)()2(

)()1()()2()1()2(

)()1()()2()()2()()1(

nnnnnn

nnnnnnnn

nnnnnn

nnnnnnnn

xxxxxx

xfxfxxxfxfxxb

xxxxxx

xfxfxxxfxfxxa

a

acbbxx nn

2

42)()1(

Page 17: Computational Methods in Physics  PHYS 3437

General quadratic General quadratic solution issuessolution issues

Recall we talked about the problems of Recall we talked about the problems of differencing very similar numbersdifferencing very similar numbers Precision is lost due to the fixed number of Precision is lost due to the fixed number of

digits in the mantissadigits in the mantissa We use the same method discussed in We use the same method discussed in

the previous lectures for improving the previous lectures for improving stabilitystability

If b≥0 then take + rootIf b≥0 then take + root

acbb

cx

acbb

acbb

a

acbbxx

bacbb

cx

acbb

acbb

a

acbbxx

nnn

nnn

4

2

4

4

2

4

then04

2

4

4

2

4

2

)(

2

22)()1(

2

)(

2

22)()1(

Page 18: Computational Methods in Physics  PHYS 3437

Full Müller-BrentFull Müller-Brent

The previous two equations for xThe previous two equations for x(n+1)(n+1) give the guesses for the rootgive the guesses for the root Can suffer from similar problems to NR Can suffer from similar problems to NR

if too close to extremum thoughif too close to extremum though (This part of algorithm is due to Muller)(This part of algorithm is due to Muller)

Brent added the option of a bisection Brent added the option of a bisection step to improve behaviourstep to improve behaviour Same idea as the hybrid NR-bisection Same idea as the hybrid NR-bisection

algorithm we discussed algorithm we discussed

Page 19: Computational Methods in Physics  PHYS 3437

Higher order fitting Higher order fitting methodsmethods

In practice quadratic is as high as we need to goIn practice quadratic is as high as we need to go Firstly, if the quadratic fit guesses a root Firstly, if the quadratic fit guesses a root

outside the bracket then the cubic fit will not do outside the bracket then the cubic fit will not do any betterany better

Secondly, if it converges the quadratic fits Secondly, if it converges the quadratic fits usually takes about 3 stepsusually takes about 3 steps The cubic fit would require 4 steps just to calculate The cubic fit would require 4 steps just to calculate

the initial points required for the guess (xthe initial points required for the guess (x(n-3)(n-3),x,x(n-2)(n-2),x,x(n-(n-

1)1),x,x(n)(n))) You can think of this the following wayYou can think of this the following way

By the time we can fit a cubic a quadratic method will By the time we can fit a cubic a quadratic method will likely have the root anyway…likely have the root anyway…

Page 20: Computational Methods in Physics  PHYS 3437

Example of root finding Example of root finding from QMfrom QM

Consider the Consider the problem of a problem of a square well square well potentialpotential

elsewhere

0)(

0V

axaxVV0

-a 0 a0

Time independent Schrödinger equation isTime independent Schrödinger equation is

EV

xm

h

2

2

2

Page 21: Computational Methods in Physics  PHYS 3437

Substitute potentialSubstitute potential

Solving the pair of equations (1) involves fitting a Solving the pair of equations (1) involves fitting a sinusoidal solution inside the square well and a sinusoidal solution inside the square well and a decaying exponential solution outside it. The two decaying exponential solution outside it. The two solutions solutions ψψ and and ψψ’ must be continuous at x=±a.’ must be continuous at x=±a.

We won’t go through the details of the solution We won’t go through the details of the solution process, but two types of solution can be found, process, but two types of solution can be found, the parameters of which are given by the parameters of which are given by transcendental equationstranscendental equations

1equation elsewhere )(

2

- 2

02

2

2

2

EVh

m

axaEh

m

x

Page 22: Computational Methods in Physics  PHYS 3437

The solution classesThe solution classes

Class IClass I

This gives even This gives even solutions for solutions for ψψ

Note, Note,

Class IIClass II

This gives odd This gives odd solutions for solutions for ψψ

22tan 22cot

h

amV

h

amE

02

2

We thus solve these equations to find – thiswill give the energy levels of the solutions, E.However, we do not know a priori where theroots will be, so we need to use a bracket Finder.

Page 23: Computational Methods in Physics  PHYS 3437

Root findingRoot finding

We recast the two solutions classes We recast the two solutions classes as equations f(as equations f(tan tan √(√( and and

g(g(cot cot √(√( What problems might we expect?What problems might we expect?

Both tan and cot have odd ordered polesthat change sign. As we discussed in the lastlecture this can lead to singularities beinginterpreted as roots – we need to look out forthis.

tan

Page 24: Computational Methods in Physics  PHYS 3437

Strategies for dealing Strategies for dealing with poleswith poles

f(1)

f(2)

f(3)

f(4)

For the root:f(1)<f(2)->f’(1)>0

For the odd pole:f(3)<f(4)->f’(3)<0

So the difference in signs of thederivatives can be used to distinguish between a root andan odd-order pole.

Page 25: Computational Methods in Physics  PHYS 3437

Strategy 2: multiply by new Strategy 2: multiply by new functionfunction

We can multiply by cos We can multiply by cos to giveto give

Multiply by sin Multiply by sin to give to give

These functions will share the same roots as g These functions will share the same roots as g & f& f

WARNING: watch out for any spurious roots WARNING: watch out for any spurious roots that are introduced by multiplying by the new that are introduced by multiplying by the new function! function!

22tan)( f

22cossin)(~ f

22cot)( g

22sincos)(~ g

Make sure you check the roots explicitly

Page 26: Computational Methods in Physics  PHYS 3437

Consider g(Consider g()) By inspection we can see that By inspection we can see that =0 is a =0 is a

root of this equation root of this equation However, if we examine g(However, if we examine g() as ) as →→00

01

limcoslimsin

lim

sin

coslim)(lim

22

000

22

00

g

Always be aware that multiplying by a new function may induce spurious roots.

~~

Page 27: Computational Methods in Physics  PHYS 3437

SummarySummary

Global bracket finding is difficultGlobal bracket finding is difficult Bracket finder given here is a sound first Bracket finder given here is a sound first

approach, but it isn’t perfectapproach, but it isn’t perfect Key step is locally adaptive evaluation of the Key step is locally adaptive evaluation of the

step between evaluation pointsstep between evaluation points The Muller-Brent method improves upon The Muller-Brent method improves upon

Newton-Raphson by using a quadratic fitNewton-Raphson by using a quadratic fit Distinguishing between poles and roots is Distinguishing between poles and roots is

possible provided one is prepared to look possible provided one is prepared to look at derivativesat derivatives

Be careful we multiplying by additional Be careful we multiplying by additional functions to remove poles – this can functions to remove poles – this can induce spurious rootsinduce spurious roots

Page 28: Computational Methods in Physics  PHYS 3437

Next LectureNext Lecture

InterpolationInterpolation