1Daniel Baur / Numerical Methods for Chemical Engineers / Nonlinear Equations
Fixed point iterations andsolution of non-linear functions
( ) 0f x
Daniel Baur
ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften
ETH Hönggerberg / HCI F128 – Zürich
E-Mail: [email protected]
http://www.morbidelli-group.ethz.ch/education/index
2Daniel Baur / 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
3Daniel Baur / Numerical Methods for Chemical Engineers / Nonlinear Equations
Fixed point iterations
Fixed point iteration formulas 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
4Daniel Baur / 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
5Daniel Baur / Numerical Methods for Chemical Engineers / Numerical Quadrature
Bisection Method
1. Define starting interval [a,b] (check that f(a)*f(b) < 0)
2. Compute x = mean([a, b])
3. Redefine the interval Set either a = x or b = x so that f(a)*f(b) < 0 is still fulfilled
4. Iterate 2 and 3 until the requestedprecision is reached
Advantages After n iterations, the interval is reduced
by 2n
Final precision can be predicted a priori
Disadvantages Function characteristics are not used
to speed up the algorithm 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3-1.5
-1
-0.5
0
0.5
1
1.5
2
2.5
3
ab
xa
6Daniel Baur / Numerical Methods for Chemical Engineers / Nonlinear Equations
Newton Method The Newton method is based on Taylor expansion
Advantages It has 2nd order convergence
Disadvantages Convergence is not guaranteed
even if the uncertainty intervalis known
If the derivative must becalculated numerically, the secant method is more convenient
20 0 0
00 1
0
( ) ( ) ( )( ) ( )
( ) ( )( ) 0
( ) ( )k
k kk
f x f x f x x x O x
f x f xf x x x x x
f x f x
xxf
xxf
2)(
1)( 2
2
1k kc
x0x1
7Daniel Baur / Numerical Methods for Chemical Engineers / Nonlinear Equations
Secant Method The Secant is based on the same principles as the Newton
method, but it approximates the derivative numerically
Advantages Does not require the analytical
first order derivative
Disadvantages Convergence is not assured
even if the uncertainty intervalis known
Convergence order is only 1.618
1
1
1
( )
( )
( ) ( )( )
kk k
k
k kk
k k
f xx x
f x
f x f xf x
x x
x0 = 1.8
x1 = 1.7
Secant Newton
8Daniel Baur / Numerical Methods for Chemical Engineers / Nonlinear Equations
How does Matlab do it? Nonlinear Functions
fzero finds the zero of a scalar valued function; It uses a combination of bisection, secant, and inverse quadratic interpolation methods
roots finds all the roots of a polynomial function;It computes the eigenvalues of the companion matrix, which correspond to the roots of the polynomial A = diag(ones(n-1,1),-1);A(1,:) = -c(2:n+1)./c(1);eig(A);
Where c is the vector of the polynomial coefficients1 1
1 2 1n n
n np c x c x c x c
3 12 4
1 1 1 1
1 0 0 0
0 1 0 0
0 0 0 1 0
nc cc c
c c c c
A
9Daniel Baur / Numerical Methods for Chemical Engineers / Nonlinear Equations
Matlab Syntax Hints
x = fzero(fun, x0); fun is a function taking as input a scalar x, returning as output the
scalar function value f(x) x0 is either an initial guess (if it has length 1) or an uncertainty
interval (if it has length 2, then f(x0(1))*f(x0(2)) < 0 must be fulfilled)
x = roots(c); c is a vector containing the polynomial coefficients in the order
1 11 2 1
n nn np c x c x c x c
10Daniel Baur / Numerical Methods for Chemical Engineers / Nonlinear Equations
Exercise 1: Fixed point iterations
Consider the function
And the following iterative formulas:
1 exp 1 0f x x x
1
2
1
1
2 exp
exp 13
exp 1
4 1 exp
k k
k kk
k k
k k k k
x x
x xx
x x
x x x x
11Daniel Baur / 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
12Daniel Baur / Numerical Methods for Chemical Engineers / Nonlinear Equations
Exercise: CSTR Multiple Steady States
Consider a CSTR where a reaction takes place
We assume the following V = const., i.e. Qin = Qout = const. Perfect coolant behavior, i.e. TC,in = TC,out = const. Constant density and heat capacity of the reaction mixture Constant reaction enthalpy Constant feed, i.e. cA,in = const., Tin = const.
, ,in in inAc Q T
, ,outic Q T
CT( )A Bk T
0( ) exp AEk T k
RT
13Daniel Baur / Numerical Methods for Chemical Engineers / Nonlinear Equations
CSTR Mass and Energy Balances
The mass and energy balances read
With the T-dependent reaction rate constant
Total Reaction In Out Cool
d( )
dd
( )d
( )
inAA A A
BA B
in Cool CoolP A R P
cV Vc k T Qc Qc
tc
V Vc k T Qct
Q Q Q Q Q
dTV c Vc k T H Q c T T K T T
dt
0( ) exp AEk T k
RT
14Daniel Baur / Numerical Methods for Chemical Engineers / Nonlinear Equations
Dimensionless Mass and Energy Balances
If we define
We get a dimensionless form
1 1 ( )
( )
( ) 1
AA
BA B
C CA
duu
ddu
u udd
u Kd
ii in
A
in
cu
c
T
TQ
tV
CoolC
P
CoolC
in
Vk
Q
KK
Q c
T
T
inR A
inP
Ain
H c
c T
E
RT
,0 ,0
0
0
0
1
( ) exp
A Bu u
15Daniel Baur / Numerical Methods for Chemical Engineers / Nonlinear Equations
CSTR Temperature Equilibrium
The steady state concentration of A reads
The temperature in steady state is therefore given by
Reaction
( ) 1
In Out Cool
SS C CA
Q Q Q Q
u K
1
1 ( )ssAu
0
( )1 0
1 ( )
( ) exp
C CK
16Daniel Baur / Numerical Methods for Chemical Engineers / Nonlinear Equations
Assignment 21. Plot the total heat flow from and to the reactor vs. the
dimensionless reactor temperature for θ = 0.9...1.25 Use α = 49.46; κ0 = 2.17e20; KC = 0.83; η = 0.33 and θC = 0.9.
2. Implement and use the secant method in a function to find the three steady state temperature of the CSTR. Use a function of the formfunction [x,xvec] = secantRoot(f,x0)
Also return the x-values calculated as a vector xvec. The calculation steps of the secant method can be found on slide 7 The secant method uses two starting guesses; from x0, calculate x1
= (1+ε)*x0. Suggest a value for ε (not too small, why?). Loop while abs(xk – xk-1) > 1e-8 and f(xk) > 1e-6 and n < 1e5
You will have to work with three x-values at any given iteration, that is xk+1, xk and xk-1
17Daniel Baur / Numerical Methods for Chemical Engineers / Nonlinear Equations
Assignment 2 (continued)
3. In what range of x0 can you converge to the intermediate solution? What feature of the function determines which solution is found?
4. Use one of the resulting xvec to estimate the convergence order and rate of convergence of the secant method.