33
數數數數 2008, Applied Mathematics NDHU 1 An iterative approach for root finding Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU1 An iterative approach for root finding Newton method Lecture 3II Root Finding

Embed Size (px)

Citation preview

Page 1: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 1

An iterative approach for root finding Newton method

Lecture 3II Root Finding

Page 2: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 2

Problem statement

• Find x such that f(x)=0 for a given f• f denotes an arbitrary single-variable function

Root finding

Page 3: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 3

1102)(2

xxf x >> x=linspace(-1,3);>> plot(x,2.^x.^2-10*x+1)

Page 4: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 4

Root finding

Input: an expressionPlot given functionFind a root

Page 5: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 5

Tangent line

xxn

y=f(x)

yn=f(xn)

xn+1

)('

)(

)('

)('

1

1

n

nn

n

nnn

nn

nn

xf

xfx

xf

yxx

xx

yxfslope

Page 6: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

Taylor series

數值方法 2008, Applied Mathematics NDHU 6

)('/)(

)('/)()(

0))((')(

)(

))((')()(

at

nnn

nnn

nnn

nnn

n

xfxfxx

xfxfxx

xxxfxf

xfzero toSet

xxxfxfxf

xxExpand

Page 7: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 7

An iterative approach

Start at some guessRefine current guess by

Find a tangent line that passes (xn,f(xn))

Set xn to a point at intersection of the tangent line to horizontal axis

Page 8: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 8

Updating rule

)('

)(1

n

nnn xf

xfxx

Page 9: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 9

Newton method

An Iterative approach

Random guess Refine current guess by an updating rule If a halting condition holds, go to step 2

otherwise exit

Page 10: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 10

An iterative approach

n=0 ; Set xn

Determine xn+1

Increase n by one

Halting condition

T

F

Page 11: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 11

An iterative approach

Set x randomly

Apply an updating rule

to refine x

~( halting condition)exit

Page 12: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 12

Halting condition

Let x denote the current guessThe absolute value of f(x) is expected as

close as possible to zeroHalting Condition

abs(f(x)) < epslonepslon denotes a predefined small positive

number

Page 13: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 13

Initialization

Random initialization A guess by the user

Page 14: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 14

An iterative approach

x = rand*2-1; s=sym(ss); ep=10^-6f=inline(s); s1=diff(s); f1=inline(s1)

x=x-f(x)/f1(x);fprintf('%f\n' ,x)

~( abs(f(x)) < ep)exit

input an expression, ss

Page 15: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 15

Demo_newton

source code demo_newton

fstr=input('input a function:','s');x_ini=input('guess its zero:');x_zero=newton(fstr,x_ini);

newton.m

Page 16: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 16

Main Program

Page 17: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 17

Newton method

Page 18: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 18

>> demo_newtoninput a function:cos(x)guess its zero:2 iter=1 x=1.542342 fx=0.028450 iter=2 x=1.570804 fx=-0.000008 iter=3 x=1.570796 fx=0.000000>>

Page 19: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 19

Example

demo_newtoninput a function:2.^x.^2-10*x+1guess its zero:1.5

Page 20: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 20

Example

>> demo_newtoninput a function:2.^x.^2-10*x+1guess its zero:0.5

Page 21: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

Second order expansion

數值方法 2008, Applied Mathematics NDHU 21

2

22

2

2

!2

)('')(')(

),(')('',!2

)(''2

4 ,0

0)(!2

)(''))((')(

)(

)(!2

)(''))((')()(

at

nn

nnn

nnn

nn

nnn

nn

nnn

n

xxf

xfxxfc

xfxfbxf

a

a

acbbxcbxax

xxxf

xxxfxf

xfzero toSet

xxxf

xxxfxfxf

xxExpand

Page 22: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

Second order method

Update rule

數值方法 2008, Applied Mathematics NDHU 22

2

2

1

!2

)('')(')(

),(')('',!2

)(''2

4

nn

nnn

nnn

n

xxf

xfxxfc

xfxfbxf

a

a

acbbx

Page 23: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 23

An iterative approach

x = rand*2-1; s=sym(ss); ep=10^-6f=inline(s); s1=diff(s); f1=inline(s1)

s2=diff(s1); f2=inline(s2)

a=f2(x)/2;b=-f2(x)+f1(x);c=f(x)-x*f1(x)+f2(x)*x^2/2;

x=…fprintf('%f\n' ,x)

~( abs(f(x)) < ep)exit

input an expression, ss

2

2

1

!2

)('')(')(

),(')('',!2

)(''2

4

nn

nnn

nnn

n

xxf

xfxxfc

xfxfbxf

a

a

acbbx

Page 24: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 24

Unconstrained Optimization

Problem statement

Given a differentiable function, y=g(x), unconstrained optimization aims to find the minimum of g(x)

Let x denote a minimum anddx

xdgxf

)()(

0 that suchx

Then

f(x)find

Page 25: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 25

Optimization by Newton method

Use symbolic differentiation to find the first derivative of a given function

Apply the Newton method to find zeros of the first derivative

Page 26: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 26

An iterative approach

x = rand*2-1; s=sym(ss); ep=10^-6g=inline(s); s1=diff(s); f=inline(s1);

s2=diff(s1); f1=inline(s2)

x=x-f(x)/f1(x);fprintf('%f gx=%f\n' ,x, g(x))

~( abs(f(x)) < ep)exit

input an expression, ss

Page 27: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 27

First derivative

ss=input('input a function:','s');s=sym(ss);f=inline(s);s1=diff(s);f1=inline(s1);x=linspace(-5,5);plot(x,f(x));hold on;plot(x,f1(x));

Page 28: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 28

>> x=linspace(-2*pi,2*pi);>> plot(x,(x-tanh(2*x+10)).^2)

Page 29: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 29

>> demo_mininput a function:(x-tanh(2*x+10)).^2guess its minimum:4

Local minimum

Page 30: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 30

>> demo_mininput a function:(x-tanh(2*x+10)).^2guess its minimum:-4

Local Minimum

Page 31: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 31

Global minimum

Page 32: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 32

>> demo_mininput a function:0.2*x.^3-3*x+cos(x)guess its minimum:2

Page 33: 數值方法 2008, Applied Mathematics NDHU1  An iterative approach for root finding  Newton method Lecture 3II Root Finding

數值方法 2008, Applied Mathematics NDHU 33

Exercise

ex3II.pdf