solution of nonlinear functions 1michael sokolov / numerical methods for chemical engineers /...

18
Solution of Nonlinear Functions 1 Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften ETH Hönggerberg / HCI F135 – Zürich E-Mail: [email protected] http://www.morbidelli-group.ethz.ch/education/index () 0 fx

Upload: beverly-haynes

Post on 14-Jan-2016

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

Solution of Nonlinear Functions

( ) 0f x

Michael Sokolov

ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften

ETH Hönggerberg / HCI F135 – Zürich

E-Mail: [email protected]

http://www.morbidelli-group.ethz.ch/education/index

Page 2: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

2Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

Zero of a Nonlinear Function

Problem definition: Find the solution of the equation f(x) = 0

for scalar valued f and x; Look for the solution either in An interval, generally –∞ < x < ∞ In the uncertainty interval [a, b], where f(a)f(b) < 0

Types of algorithms available:1. Bisection method2. Substitution methods3. Methods based on function approximation

Assumptions: In the defined intervals, at least one solution exists We are looking for one solution, not all of them

Page 3: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

3Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

Fixed point iterations

Fixed point iterations generally have the form

A fixed point of F is a point x*, where

A fixed point iteration is called consistent with a non-linear equation f(x), if

1k kx x F

* *x xF

* * *0f x x x F

Page 4: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

4Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

Convergence order of fixed point iterations

For any (converging) fixed point iteration, we can write

where c is the rate of convergence and q is the convergence order

If we take the logarithm on both sides, we get

Which we can use to fit an average q and c

1 1

q

k k k kx x c x x

1log log logk kx q x c

Y a X b

Page 5: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

5Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

Solution of Nonlinear Equation Systems

( ) 0x F

Michael Sokolov

ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften

ETH Hönggerberg / HCI F135 – Zürich

E-Mail: [email protected]

http://www.morbidelli-group.ethz.ch/education/index

Page 6: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

6Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

Zero of Nonlinear Equation Systems

Problem definition: Find the solution of F(x) = 0, where both F and x are vector

(or matrix) valued; Look for the solution either In an interval xlb < x < xub

Types of algorithm available:1. Substitution algorithms2. Methods based on function approximation

Assumptions: At least one zero exists in the defined interval We are looking for one zero, not all of them

Page 7: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

7Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

Function linearization

Let us again consider Taylor expansion

In matrix form, this reads

Which is equivalent to Newton Method!

0 0 0 0

1 11 1 0 0 0 0

( , ) ( , )

( , ) ( , ) ( ) ( ) 0x y x y

f ff x y f x y x x y y

x y

0 0

0 1 0 01 1

0 2 0 02 2 ,

( , )/ /

( , )/ /x y

x x f x yf x f y

y y f x yf x f y

0 0( ) ( )f J x x x

1

2

,0

,

f x yf x

f x y

0 0 0 0

2 22 2 0 0 0 0

( , ) ( , )

( , ) ( , ) ( ) ( ) 0x y x y

f ff x y f x y x x y y

x y

Page 8: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

8Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

The Jacobian matrix

Consider a continuous, differentiable function : n nf

1 1 2 3

2 1 2 3

1 2 3

, , , ,

, , , ,

, , , ,

n

n

n n

f x x x x

f x x x x

f x x x x

1 1 1

1 2

2 2 2

1 1

1 2

n

n

n n n

n

f f f

x x x

f f f

x x x

f f f

x x x

J

,i

i kk

f

x

J

Page 9: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

9Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

The Multidimensional Newton-Algorithm

1. Define a starting point x2. Compute –f(x)3. Compute J(x)

Either analytically or numerically

4. Solve the linear system of equations J(x)*Δx = -f(x)for Δx, then set x = x + Δx

Iterate 2 through 4 until some stopping criteria are fulfilled

0 0( ) ( )f J x x x

Page 10: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

10Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

Improvements on the Algorithm

It might not be necessary to compute the Jacobian at every iteration, the algorithm can also work nicely if the Jacobian is only computed every few iterations

When doing this, instead of solving the linear system of equations, it might be more efficient to compute inv(J) when recomputing J, or factorize it in a convenient manner

A numerical Jacobian can sometimes lead to better (numerical) results than an analytical one

Another option is to adapt the step size, so before accepting a Δx, one checks if norm(f(x+Δx)) is in fact smaller than norm(f(x)), and if not, a step of size λ*Δx with λ < 1 is tried instead

0 0( ) ( )f J x x x

Page 11: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

11Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

How does Matlab do it? Nonlinear Systems fsolve is the general solver for non-linear equation

systems; It can use different algorithms, namely Trust-region algorithms: Instead of just going in the direction that the

Newton step suggests, a check is performed if xk+1 is really better than xk. This is done by approximating the behavior of the (maximally quadratic) function around xk (the trust-region). An optimization is then performed to find an optimal step.

Trust-region-dogleg: The so-called dogleg method is used to solve the optimization problem, which constructs the step as a convex combination of the Cauchy step (steepest descent) and a Gauss-Newton step.

Trust-region-reflective: The optimization problem is solved via a conjugate gradient method in a 2-D subspace.

The Levenberg-Marquardt method, which finds the step by solving

( ) ( ) ( ) ( )T Tk k k k k kx x d x f x J J I J

Page 12: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

12Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

Matlab Syntax Hints

fsolve uses the syntax x = fsolve(nl_fun, x0, options); where nl_fun is a function taking as input x, returning as output the

function values f at x, both can be vectors or matrices x0 is an initial guess options can be set by options = optimset(...) to choose

algorithms, give analytical Jacobians, etc.; see doc fsolve for details

Page 13: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

13Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

Exercise: CSTR with multiple reactions

Consider a CSTR where two reactions take place

We assume the following V = const., i.e. Qin = Qout = const. Constant temperature and density of the reaction mixture Constant feed, i.e. cA,in = const., cB,in = const.

, ,in inA Bc Q

, outic Q

1

2

A+B C

C+B D

k

k

Page 14: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

14Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

CSTR mass balance

inA A A 1 A B

inB B B 1 A B 2 B C

C C 1 A B 2 B C

D D 2 B C

dA:

dd

B:dd

C: 0dd

D: 0d

Vc Q c c V k c ct

Vc Q c c V k c c k c ct

Vc Q c V k c c k c ct

Vc Q c V k c ct

(Accumulation) = Flow*(In – Out) + Reaction

Page 15: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

15Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

CSTR steady state

d0

dt A B C Dx c c c c

in1 1 1 1 2

in2 2 1 1 2 2 2 3

3 1 1 2 2 2 3

4 2 2 3

A: 0

B: 0

C: 0

D: 0

x x k x x

x x k x x k x x

x k x x k x x

x k x x

where is the residence time/V Q

Page 16: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

16Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

Assignment 1

1. For each of the iterative formulas try to find a fixed point Define a starting guess x0 between 0 and 1 while abs(xk – xk-1) > 1e-8, calculate the next x value Store the values calculated in a vector xvec Also terminate the while-loop if 1e5 iterations are exceeded

2. Is there a fixed point and is it consistent with (1)?

3. Estimate the convergence orders and the rates of convergence for the formulas which have a fixed point Calculate dX = log(abs(diff(xvec))); Set X = dX(1:end-1); and Y = dX(2:end); Perform a linear fit with p = polyfit(X,Y,1); Calculate q and c from the fitting coefficients in p, remember that

1log log logk kx q x c

( ) exp 1 0f x x x

1 expk kx x

2

1

exp 1

exp 1k k

kk k

x xx

x x

1 1 expk k k kx x x x

Page 17: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

17Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

Assignment 2

1. Write down the analytical Jacobian matrix for the steady state CSTR shown on slide 15.

2. Implement the basic Newton method as shown on slide 5. Use a function of the formfunction [x, info] = newtonMethod(f, J, x0, tol);where f is a function handle to the function you want to solve, J is a function handle that returns the Jacobian matrix, x0 is an initial guess and tol is a vector of tolerances.

As in with the secand method, use a while loop to find the solution. Suggest stopping criteria and failure checks. When can the Newton

method fail in general? Use left division «\» to solve the linear system at every iteration (do

not use inv(J)!) Let info be a struct you can use to return additional information,

like reason of termination and number of steps needed.

Page 18: Solution of Nonlinear Functions 1Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations Michael Sokolov ETH Zurich, Institut

18Michael Sokolov / Numerical Methods for Chemical Engineers / Nonlinear Equations

Assignment 2 (continued)

3. Use your Newton algorithm to solve the steady state CSTR numerically. Create two function files; one that calculates the CSTR equations (1)

as functions of x, and one that calculates the analytical Jacobian as a function of x.

Use xIn1 = 1; xIn

2 = 1.5; k1 = 0.5; k2 = 10; and τ = 5 What is the total conversion of A to D? Compare your result to what fsolve() finds. Try different starting

guesses. Can you find more than one solution?

4. Find online the function jacobianest. Modify your Newton algorithm so that it uses jacobianest to

approximate the Jacobian if the input J is empty (use isempty(J) to check). To provide an empty input, use [] in the call.

How many steps are required with the analytical Jacobian compared to the numerical Jacobian? Which algorithm takes longer?