• Non ci sono risultati.

Lecture on Numerical Analysis

N/A
N/A
Protected

Academic year: 2021

Condividi "Lecture on Numerical Analysis"

Copied!
23
0
0

Testo completo

(1)

M. Dumbser 1 / 23

Lecture on Numerical Analysis

Dr.-Ing. Michael Dumbser

08 / 11 / 2007

(2)

Numerical Solution of Nonlinear Equations

0 ) ( xf

Task: compute approximately the solution x of the nonlinear algebraic equation

If the problem is not in form (1), then it can always be cast into form (1) by putting all terms on the left hand side.

Examples:

(1)

2 2

2

) sin(

) exp(

) (

0 )

sin(

) exp(

) sin(

) exp(

x x

x x

f

x x

x

x x

x

) ( )

( ) ( )

(

) ( )

( ) (

x e x

h x g x

f

x e x

h x g

(3)

M. Dumbser 3 / 23

Numerical Solution of Nonlinear Equations

0 ) ( xf

Task: compute approximately the solution x of the nonlinear algebraic equation (1)

Two problems present even at the analytical level:

• For general functions f(x), there may be no solution at all.

Example:

• For general functions f(x), there may be more than one solution.

Example:

0 2 ) sin(

)

( xx   f

0 1 )

( x  x

2

 

f x

1

  1 , x

2

  1

(4)

x0 x2 x3 x1 x f

x4

(I) Before we can start using this method, we must guess two points x0 and x1 where the function f(x) has opposite sign, i.e.

The Regula Falsi Method

0 ) ( )

( x

0

f x

1

f

(II) Now connect the two points (x0, f(x0)) and (x1, f(x1)) with a straight line and compute the intersection of this line with the x-axis, denoted by xi. The point xi is given by

1 0

1 0 1

0

f f

f x x

x

i

f

 

(III) Compute the sign of f(x2) and compare against the signs of f(x0) and f(x1). Then update the interval

(5)

M. Dumbser 5 / 23

x0 x2 x3 x1 x

f

x4

Advantages: - it is guaranteed that the method finds a solution of f(x)=0 because we already know that it the solution is contained in the initial interval [x0;x1].

- The method is very robust and reliable

The Regula Falsi Method

Disadvantage: - it can not be applied if we do not have an initial guess of the interval [x0;x1].

(6)

x0

x2 x1

x f

x3

• Variant of the regula falsi method: we start from any two points x0 and x1

The Secant Method

• Advantage: no initial interval must be guessed

• Disadvantage: may not always converge towards a solution !

(7)

M. Dumbser 7 / 23

x0 x1

x f

• Variant of the regula falsi method: we start from any two points x0 and x1

The Secant Method

• Advantage: no initial interval must be guessed

• Disadvantage: may not always converge towards a solution !

(8)

x0 x1

x f

• Differential form of the secant method. Instead of using the secant, Newton proposes to use the tangent!

Newton‘s Method

• Advantages: - no initial interval must be guessed - very fast

• Disadvantage: may not always converge towards a solution ! x2

(9)

M. Dumbser 9 / 23

x0 x1

x f

Newton‘s Method

x2

• Differential form of the secant method. Instead of using the secant, Newton proposes to use the tangent!

  0

)

(  

 

i

i

x x

x x f

f

x x f

f

x

i

 

 ( ) /

x x

x

i1

i

 

x

i

x x  

 :

(1) If |f(xi)| < tolerance then STOP (in Maple/MATLAB: break) or (2) If | x | < tolerance then STOP

(10)

Exercise 1

Write a Maple worksheet and a MATLAB script that solve the nonlinear equation

using the regula falsi method. Choose an appropriate starting interval.

Exercise 2

Now write a MATLAB script that solves the problem of exercise 1 using the Newton method. The user should have the possibility to choose the starting guess, the

maximum number of iterations as well as the tolerance.

0 )

sin(

) exp(

)

( xxxx

2

f

(11)

M. Dumbser 11 / 23

Globally Convergent Newton Methods

From the previous Example 4 we find that even for strictly monotone functions f(x) Newton‘s method may not always converge from arbitrary starting points x0, but only from starting points that are sufficiently close to the solution of the problem. However, if the starting point is sufficiently close, the method converges. We therefore say the original Newton method is only locally convergent.

The local convergence behaviour of Newton‘s method and the possible failure to converge for initial guesses far away from the solution can be mainly explained by too large step sizes

x:

(12)

Globally Convergent Newton Methods

As seen in the sketch of the previous slide, the absolute value of the function f(x) slightly increased during one Newton step. To avoid this problem, we therefore add the following requirement: The function |f(x)| must always decrease during one Newton step, i.e.:

) ( )

( x

i

x

i

f x

i

f   

x x f

f

x

i

 

 ( ) /

x x

x

    

If this is not fulfilled, the step size is sucessively reduced until the criterion is satisfied. The globally convergent Newton method then reads as follows:

) ( )

( x

i

x

i

f x

i

f    

 1

1 0   

WHILE

  

END DO i=0,1,…

Free parameter.

Choose e.g.  = 0.5

(13)

M. Dumbser 13 / 23

Globally Convergent Newton Methods

Exercise 5

Write a modified MATLAB script GlobalNewton.m to solve the nonlinear equation

with the globally convergent Newton method.

0 ) 1 arctan(

)

( xx  

f

1) Use x0 = 2 as initial guess.

2) Use x0 = 2.5 as initial guess. What do you realize?

3) Use x0 = 106 as initial guess. Does the method converge?

(14)

Computation of the Roots of a Polynomial

We now apply Newton‘s method to a special case of nonlinear algebraic equation where the function f(x) is a polynomial of degree n:

The values x which satisfy eqn. (1) are called the roots of the polynomial pn(x).

We suppose in the following that all the roots of the polynomial are distinct and real.

0 )

( )

(

0

 

n i

i i

n

x a x

p x

f

Suppose x =  is a root of pn(x), i.e.

then we have

If we knew all the roots i we would have the following expression of the polynomial:

) ( )

( )

( x x p

1

x

p

n

  

n

0

) (   p

n

) )...(

)(

( )

(

1 2 n

n

x x x x

p       

(15)

M. Dumbser 15 / 23

Computation of the Roots of a Polynomial

A simple and straightforward application of Newton‘s method now consists in finding a first root 1 (typically the largest one) of the polynomial pn and then compute the largest root 2 of the polynomial pn-1 obtained by dividing the polynomial pn by ( x – ):

1 1

) ) (

(   

x

x x p

p

n n

After having found the root 2 of pn-1 we divide again

) )(

(

) ( )

) ( (

2 1

2

2 1

     

x x

x p x

x x p

p

n n n

and compute the next root 3 and so on, until all the n roots of pn have been found.

In general, we have:

) )...(

(

) ) (

(

1 j

j n

n

x x

x x p

p

    

 

 

 

j

i i

j n

j n

j x

n

x x x

x p x

x

x x p

x p

1 1 1

1 )

)...(

(

) ( )

)...(

(

) ) (

(     

(16)

Computation of the Roots of a Polynomial

To express the polynomial pn-j(x) and its derivative, two simple MATLAB loops defining the auxiliary variables Dj and Sj are necessary:

j j n

n

D

x x p

p ( )

) ( 

j j

n j

n j x

n

S

D x p D

x x p

x p   

) ( )

) ( (

END

) (

*

j]

: [1 k

FOR 1

k j

j j

x D

D D

with

END

) /(

1

j]

: [1 k

FOR 0

k j

j j

x S

S S

We then obtain the so-called Newton-Maehly method:

(17)

M. Dumbser 17 / 23

Exercise 3

(1) Write a MATLAB script PolyRoot.m that finds all the roots of a general polynomial of degree n.

The user should be able to specify the coefficients a of the polynomial. Then, two MATLAB function files, called Polynomial.m and DPolynomial.m compute the polynomial and its first derivative in point x using the coefficients a.

Further user inputs required are tol, NMAX and x0.

(2) Apply your general MATLAB script to the particular polynomial

(3) Now try your MATLAB program on the polynomial

Hint: Use Maple to rewrite this polynomial in standard form

2 1 4

x 3 4 3 7

)

( xx

4

x

3

2

xp

12

0

) 2 (

) (

j

x

j

x p

n

j

j j

x a x

p

0

)

(

(18)

Efficient Evaluation of Polynomials

The standard format of defining a polynomial is computationally not the most efficient way to evaluate a polynomial:

...

)

( x a

0

a

1

x a

2

x x a

3

x x x a

4

x x x x

p               

A computationally more efficient but mathematically fully equivalent way of writing the polynomial p(x) is the so called Horner scheme, which uses the distributive law:

...

)

( x a

0

a

1

x a

2

x

2

a

3

x

3

a

4

x

4

p         

In this example, we have 4 additions and 10 multiplications

 

 

 

 ... 

)

( x a

0

x a

1

x a

2

x a

3

x a

4

p         

Now, we still have 4 additions but only 4 multiplications!

(19)

M. Dumbser 19 / 23

Globally Convergent Newton Methods

Exercise 4

Now try your original MATLAB script Newton.m to solve the nonlinear equation

0 ) 1 arctan(

)

( xx  

f

1) Use x0 = 2 as initial guess.

2) Use x0 = 2.5 as initial guess. What do you realize?

(20)

Newton‘s Method for Systems of Equations

  0

)

(  

 

i

i

x x

x x f

f  

 

)

1

( x

i

f D

x   

 

x x

x

i1

 

i

  

x

i

x x     

 :

(1) If ||f(x)|| < tolerance then STOP (in Maple/MATLAB: break) or

The extension of Newton‘s method to systems of nonlinear algebraic equations with several unknown variables is straightforward.

0 ) ( xf 

The problem is written in standard form in matrix-vector notation as follows:

Linearization (multi-dimensional Taylor series up to first order) leads to:

j ij i

x f x

D f

D

 

 

 

 :

The matrix D is called the Jacobian matrix of the vector function f with respect to ist vector argument x and is the multi-dimensional equivalent to the first derivative of a scalar function.

This corresponds to solving the linear system

Dx    f  ( x

i

)

(21)

M. Dumbser 21 / 23

Exercise 6

Write a MATLAB script NewtonSys.m that solves the nonlinear system of equations

using the Newton method for systems. The user should have the possibility to choose the initial guess for the vector of unknowns, the maximum number of iterations as well as the tolerance. The vector function and its Jacobian matrix should be implemented in two separate .m files, called FuncSys.m and DFuncSys.m.

0 2

1 )

2 sin(

) 1 exp(

) 2 , 1

(

2 2

1

x xxxxx

f

0 2 1

) 2 sin(

) 1 cos(

) 2 , 1

2

( x xx xxx

f

(22)

Globally Convergent Newton‘s Method for Systems

The extension of the globally convergent Newton‘s method to systems of nonlinear algebraic equations with several unknown variables is done as in the scalar case:

) (

1

x

i

x f

x f  

 

 

 

 

x x

x

i1

 

i

 

k

 

) ( )

( x

i k

x

i

f x

i

f     

 

0

 1

1 0   

WHILE

k

k

 

1

 

END DO i=0,1,…

 0 k

 1

 k

k

(23)

M. Dumbser 23 / 23

Exercise 7

We want to solve the following nonlinear system of equations:

(1) Use the classical Newton method for systems starting from [0,0]

(2) Use the classical Newton method for systems starting from [1000,1000]

(3) Can you find a starting point from which the classical Newton method converges?

(4) Construct a globally convergent Newton method for systems and start again from the points [0,0] and [1000,1000]. Does the global method even converge from the starting point [1e6,1e6] ?

0 ) arctan(

) 1 arctan(

) ,

( x yx   y

f

3 0 ) 1 exp(

) ,

( x y   x

2

y

g

Riferimenti

Documenti correlati

[r]

[r]

[r]

[r]

For general functions f(x) the accuracy of polynomial interpolation does not increase when increasing the degree of the interpolation polynomial. It may even happen that the

Write a MATLAB function Thomas.m that realizes the Thomas algorithm and that accepts as input four vectors of equal length a, b, c and d, where a contains the lower diagonal

The aim of the Gaussian quadrature formulae is now to obtain an optimal quadrature formula with a given number of points by making also the nodes an unknown in the

(2) Use the modified second order Euler method (=second order Runge-Kutta scheme) with different time steps, and compare with the exact solutions. (3) Use the fourth order