03 finding roots

40
0101011 0000000 1110100 0110001 0 1 Finding Roots v2011.09.20 FI3102 Computational Physics 1 Sparisoma Viridi Nuclear Physics and Biophysics Research Division Institut Teknologi Bandung, Bandung 40132, Indonesia [email protected]

Upload: sparisoma-viridi

Post on 23-Jun-2015

618 views

Category:

Education


0 download

DESCRIPTION

A lecture about Finding Root in Computational Physics course at Physics Department, Institut Teknologi Bandung, Indonesia

TRANSCRIPT

Page 1: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Finding Roots

v2011.09.20 FI3102 Computational Physics 1

Sparisoma Viridi

Nuclear Physics and Biophysics Research Division

Institut Teknologi Bandung, Bandung 40132, Indonesia

[email protected]

Page 2: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Outline

• Function and roots

• A simple physical problem and its solution

• Several methods in finding roots

• Graphical method

v2011.09.20 FI3102 Computational Physics 2

• Graphical method

• Newton’s method, secant method

• Bisection method

• References

Page 3: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Function

• Function:

– Output: f(x)

– Input: x (the argument)

• The argument can be• The argument can be

a real number or

elements of a given

set (matrix, vector, etc)

• See reference [1]v2011.09.20 FI3102 Computational Physics 3

Page 4: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Roots

• Argument that makes a function to

produce zero output [2, 3]

• Argument that make two functions to

produce same output [4]produce same output [4]

• Question 1. Is the second definition

actually the same as the first one?

v2011.09.20 FI3102 Computational Physics 4

Page 5: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Roots (cont.)

• Example

of roots for

f(x) = cos (x)

in interval

v2011.09.20 FI3102 Computational Physics 5

in interval

[-2π,2π],

see [5]

Page 6: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Roots (cont.)

• Say that there are two functions:

– First function is f(x)

– Second function is g(x)

• The second definition of roots is argument • The second definition of roots is argument

that makes two functions produce same

output

• If x is a root than f(x) = g(x)

v2011.09.20 FI3102 Computational Physics 6

Page 7: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Roots (cont.)

• Or, we can introduce a new function

– The new function is h(x) = f(x) – g(x)

• If x is a root, it makes f(x) = g(x)

• If x is a root, it also makes h(x) = 0• If x is a root, it also makes h(x) = 0

• So, both definitions are actually the same

(Answer 1)

v2011.09.20 FI3102 Computational Physics 7

Page 8: 03 finding roots

0101011

0000000

1110100

0110001

0

1

A simple physical problem

• A ball is launched from bottom of a incline

plane as illustrated below, find where and

when does it hit

the plane!

v2011.09.20 FI3102 Computational Physics 8

βα

v0

O (0, 0)

H (xH, yH)

the plane!

Page 9: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Solving the problem analytically

• The ball motions in x and y direction as

function of time t are

x(t) = v0 [cos (α + β)] t

y(t) = v [sin (α + β)] t – ½ gt2y(t) = v0 [sin (α + β)] t – ½ gt2

then y(t) = y[t(x)] = y(x) will give

y = f(x) = [tan (α + β)] x – [g / 2v02 cos2(α + β)] x2

• The equation of incline plane is

y = g(x) = [tan β] x

v2011.09.20 FI3102 Computational Physics 9

Page 10: 03 finding roots

0101011

0000000

1110100

0110001

0

1

The roots of the problem

• In this case we can use second definition

of roots, so

f(x) = g(x) → x is a root

• Or we construct a new function h(x)• Or we construct a new function h(x)

h(x) = f(x) – g(x)

= [tan (α + β) – tan β] x – [g/2v02 cos2(α + β)] x2

• The view of both definition in finding roots

will be shown in the next slide

v2011.09.20 FI3102 Computational Physics 10

Page 11: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Graphics of f(x), g(x), and h(x)

rootf(x)h(x)

v2011.09.20 FI3102 Computational Physics 11

g(x)

Page 12: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Graphics of .. (cont.)

• Even both graphics show different curve,

but they give the same value of x (the root

we want to find)

• Parameters are:• Parameters are:

α = π/6, β = π/6, v0 = 5, g = 10,

x0 = 0, y0 = 0, ∆x = 0.025

• The root should be around x = 1.4

• And vertical position around y = 0.85

•v2011.09.20 FI3102 Computational Physics 12

Page 13: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Analytical solution

• Using quadratic formula we can find the

roots

• But this case is simpler since other root

are 0are 0

• Then it can found that

x = [tan (α + β) – tan β] [2v02 cos2(α + β)] / g

= 1.443376

f(x) = f(1.443376) = 0.833333

v2011.09.20 FI3102 Computational Physics 13

Page 14: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Analytical solution (cont.)

• Question 2. If we already know the

analytical solution, then for what is it the

numerical solution?

v2011.09.20 FI3102 Computational Physics 14

Page 15: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Analytical solution (cont.)

• Answer 2. It is for testing whether the

numerical solution has the same value as

the analytical one

• You can not (or should not) use your • You can not (or should not) use your

method or algorithm or code to some

problem, before you test it

• Test it for the problem that you have

already known its solution

v2011.09.20 FI3102 Computational Physics 15

Page 16: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Several methods in finding roots

• Graphical method

• Newton method

• Secant method

• Einschlussverfahren method [4]: bisection, • Einschlussverfahren method [4]: bisection,

regula falsi, pegasus, Anderson-Björck,

Zeroin, Illinois, King, Anderson-Björck-

King, etc)

v2011.09.20 FI3102 Computational Physics 16

Page 17: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Graphical method

• It uses graphics to find the root

• It can accommodate the first and second

definition of the root

• It is rather not accurate since it depends • It is rather not accurate since it depends

on our judgment to determine whether

f(x) = g(x) or h(x) = 0

• Some software, i.e. gnuplot, has feature to

return the position of mouse pointer

v2011.09.20 FI3102 Computational Physics 17

Page 18: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Graphical method (cont.)

• We (actually) already used this method

root

analytical

v2011.09.20 FI3102 Computational Physics 18

x = 1.4

y = 0.85

x = 1.443376

y = 0.833333

analytical

solution

graphical

solution

Page 19: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Newton’s method

• Taylor’s expansion around x = a will give

f (x) ≈ f (a) + (x – a)f '(a)

with assumption that (x – a) small enough

to terminate next term in the seriesto terminate next term in the series

• By setting x = 0 (the target) we can find

that x = a – f (a) / f ' (a)

• Or, the iterative expression is

xi+1 = xi – f (xi) / f '(xi)

v2011.09.20 FI3102 Computational Physics 19

Page 20: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Newton method (cont.)

• Question 3. Write the iterative expression

for previous problem using Newton’s

method [use h(x) instead of f(x) and g(x)]

and show also the resultand show also the result

• From previous problem:

h(x) = [tan (α + β) – tan β] x – [g/2v02 cos2(α + β)] x2

h'(x) = ?

xi+1 = xi + ?

v2011.09.20 FI3102 Computational Physics 20

Page 21: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Newton method (cont.)

• Answer 3. It can be found that

( )[ ]( )

+−−+

−=

2

22cos2

tantan io

i xv

gx

xxβα

ββα

v2011.09.20 FI3102 Computational Physics 21

( )

( )[ ]( )

+−−+

+−=+

2

22

1

costantan

cos2

io

oii

xv

g

vxx

βαββα

βα

Page 22: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Newton method (cont.)

• And the result is

• Only need about

6 iterations

• x = 1.443376

(analytical solu-

tion is 1.443376)

• In a good agree-

ment wtih a.s.v2011.09.20 FI3102 Computational Physics 22

Page 23: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Secant method

• In Newton’s method we must know or

calculate the derivative of f(x)

• In secant method it is calculated using

f '(x ) ≈ [f(x ) – f(x )] / (x – x )f '(xi) ≈ [f(xi) – f(xi-1)] / (xi – xi-1)

• And this gives the iterative expression

xi+1 = xi – (xi – xi-1) f (xi) / [f(xi) – f(xi-1)]

with xi and xi-1 as starting values

v2011.09.20 FI3102 Computational Physics 23

Page 24: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Secant method (cont.)

• Secant method (right) is more efficient

than Newton’s method (left) as illustrated

in [3]

v2011.09.20 FI3102 Computational Physics 24

Page 25: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Secant method (cont.)

• The result is

• Only (also) need

~ 6 iterations

• x = 1.443376

(analytical solu-

tion is 1.443376)

• In a good agree-

ment wtih a.s.v2011.09.20 FI3102 Computational Physics 25

Page 26: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Newton’s vs secant method

Iteration Newton’s

method

Secant method Difference

1 1.796544 1.675235 0.121309

2 1.501396 1.383598 0.117798

3 1.445534 1.434796 0.010738

4 1.443379 1.443749 -0.000374 1.443379 1.443749 -0.00037

5 1.443376 1.443373 2.23E-06

6 1.443376 1.443376 5.76E-10

7 1.443376 1.443376 -8.9E-16

8 1.443376 1.443376 -2.2E-16

9 1.443376 1.443376 4.44E-16

10 1.443376 1.443376 0

v2011.09.20 FI3102 Computational Physics 26

Page 27: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Newton’s vs secant ... (cont.)

• For Newton’s method x0 = 1

• For secant method x0 = 1 and x1 = 1.1

• Both methods gives similar result at about

10 iterations (difference less than 10–16)10 iterations (difference less than 10–16)

v2011.09.20 FI3102 Computational Physics 27

Page 28: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Bisection method

• Suppose that we have a function as in [6]

v2011.09.20 FI3102 Computational Physics 28

Page 29: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Bisection method (cont.)

• The algorithm:

v2011.09.20 FI3102 Computational Physics 29

Page 30: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Example: x2 – 4x + 3

v2011.09.20 FI3102 Computational Physics 30

Page 31: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Example: x2 – 4x + 3 (cont.)

v2011.09.20 FI3102 Computational Physics 31

Page 32: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Example: x2 – 4x + 3 (cont.)

• The roots have been found

x1 ≈ 2.999 (analytical solution is 3)

x2 ≈ 1.001 (analytical solution is 1)

• Further iteration (more than 10) can be

performed to get nearer results

• What about muliple roots? Any idea?v2011.09.20 FI3102 Computational Physics 32

Page 33: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Multiple roots

• After finding one root the original equation

must be modified

• Question 4. Do you know how to modify • Question 4. Do you know how to modify

the original equation in order to obtain

other root?

v2011.09.20 FI3102 Computational Physics 33

Page 34: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Multiple roots (cont.)

• Answer 4. Divide the original equation

with all previous found root(s) in order to

avoid the previous root(s) to be found

againagain

• Could you write the algorithm together with

the finding roots algorithm?

v2011.09.20 FI3102 Computational Physics 34

Page 35: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Multiple roots (cont.)

• Multiple roots algorithm (1st root)

– Finding roots algorithm

• Multiple roots algorithm (2nd root)

– Finding roots algorithm– Finding roots algorithm

• Multiple roots algorithm (3rd root)

– Finding roots algorithm

• .. for the nth root ..

v2011.09.20 FI3102 Computational Physics 35

Page 36: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Summary

• Three different methods for finding roots

have been presented

• A simple physical problem is used to

illustrate the first two methodillustrate the first two method

• Other problems which need to find roots

can also be solved using given methods

• Algorithm to solve multiple roots problem

has not yet been discussed (only the idea)

v2011.09.20 FI3102 Computational Physics 36

Page 37: 03 finding roots

0101011

0000000

1110100

0110001

0

1

References

1. Wikipedia contributors, “Function (mathe-

matics)”, Wikipedia, The Free

Encyclopedia, 18 September 2011, 10:26

UTC, oldid:451121927> [2011.09.19

v2011.09.20 FI3102 Computational Physics 37

UTC, oldid:451121927> [2011.09.19

07.31+07]

2. Paul L. DeVries, “A First Course in

Computational Physics”, John Wiley &

Sons, New York, First Edition, 1994, pp.

41-85

Page 38: 03 finding roots

0101011

0000000

1110100

0110001

0

1

References (cont.)

3. Steven E. Koonin and Dawn C. Meredith,

“Computational Physics Fortran Version”,

Westview, Canada, First Edition, 1990,

pp. 11-14

v2011.09.20 FI3102 Computational Physics 38

pp. 11-14

4. Gisela Engeln-Müllges, Klaus

Niederdrenk, and Reinhard Wodicka,

“Numerik-Algorithmen”, Springer, Berlin,

10. Auflage, 2011, pp.25-90

Page 39: 03 finding roots

0101011

0000000

1110100

0110001

0

1

References (cont.)

5. Wikipedia contributors, “Zero of a

function”, Wikipedia, The Free

Encyclopedia, 12 September 2011, 02:23

UTC, oldid:449926806 [2011.09.19 UTC, oldid:449926806 [2011.09.19

07.48+07]

6. Sparisoma Viridi, “Computational

Physics”, Version 3, 2011, pp. 49-53

v2011.09.20 FI3102 Computational Physics 39

Page 40: 03 finding roots

0101011

0000000

1110100

0110001

0

1

Thank you (for your patience)

v2011.09.20 FI3102 Computational Physics 40

(for your patience)