Lecture 9—Non-linear systems
Multidimensional Newton-Rhapson
We have dealt with the solution of a single non-linear equation and the
simultaneous solution of multiple linear equations. Now we are going to look
at the simultaneous solution of solving multiple non-linear equations.
To start, let’s consider a single equilibrium reaction:
A −−→←−− B + 2 C
The stoichiometric coefficients are s = [−1 12] for A, B, and C.
To find the equilibrium conversion, solve for the reaction coordinate using
the single equilibrium relationship:
f(ξ) = K −
nB,in+ξ
ntot
in
+2ξ
nC,in+2ξ
ntot
in
+2ξ
2
nA,in−ξ
ntot
in
+2ξ
The value of ξ that satisfies f(ξ) = 0 is the extent of reaction that satisfies the
equilibrium relationship.
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 1 / 18
Multidimensional Newton-Rhapson
We could solve this equation using the Newton-Rhapson method:
ξnext = ξlast −
f(ξlast )
f (ξlast )
This is cumbersome since we would have to calculate the derivative of f(ξ).
Instead, we could use the Secant method:
ξiter = ξiter−1 − f(ξiter−1)
ξiter−1 − ξiter−2
f(ξiter−1) − f(ξiter−2)
≈ 1
f (ξiter−1)
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 2 / 18
Multidimensional Newton-Rhapson
It is often the case that more than 1 reaction occurs within a reactor:
reactor_eq.m
C
K1
−−−− 2 A + B C
K2
−−−− A + D
K1 =
C2
ACB
CC
K2 =
CACD
CC
S =
2 1 −1 0
1 0 −1 1
100 mol C
s
Now we have two coupled non-linear equations:
K1 =
nA,in+2ξ1+ξ2
ntot
in
+2ξ1+ξ2
2
nB,in+ξ1
ntot
in
+2ξ1+ξ2
nC,in−ξ1−ξ2
ntot
in
+2ξ1+ξ2
(1)
K2 =
nA,in+2ξ1+ξ2
ntot
in
+2ξ1+ξ2
nD,in+ξ2
ntot
in
+2ξ1+ξ2
nC,in−ξ1−ξ2
ntot
in
+2ξ1+ξ2
(2)
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 3 / 18
Multidimensional Newton-Rhapson
Now we have two coupled non-linear equations:
K1 =
nA,in+2ξ1+ξ2
ntot
in
+2ξ1+ξ2
2 nB,in+ξ1
ntot
in
+2ξ1+ξ2
nC,in−ξ1−ξ2
ntot
in
+2ξ1+ξ2
(1)
K2 =
nA,in+2ξ1+ξ2
ntot
in
+2ξ1+ξ2
nD,in+ξ2
ntot
in
+2ξ1+ξ2
nC,in−ξ1−ξ2
ntot
in
+2ξ1+ξ2
(2)
There are 2 equations and 2 unknowns (ξ1 and ξ2). So there exists 1 unique solution. Convert
these equations into a root-finding form:
f1(ξ1, ξ2) = K1 −
nA,in+2ξ1+ξ2
ntot
in
+2ξ1+ξ2
2 nB,in+ξ1
ntot
in
+2ξ1+ξ2
nC,in−ξ1−ξ2
ntot
in
+2ξ1+ξ2
(3)
f2(ξ1, ξ2) = K2 −
nA,in+2ξ1+ξ2
ntot
in
+2ξ1+ξ2
nD,in+ξ2
ntot
in
+2ξ1+ξ2
nC,in−ξ1−ξ2
ntot
in
+2ξ1+ξ2
(4)
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 4 / 18
Multidimensional Newton-Rhapson
There are 2 equations and 2 unknowns (ξ1 and ξ2). So there exists 1 unique solution. Convert
these equations into a root-finding form:
f1(ξ1, ξ2) = K1 −
nA,in+2ξ1+ξ2
ntot
in
+2ξ1+ξ2
2 nB,in+ξ1
ntot
in
+2ξ1+ξ2
nC,in−ξ1−ξ2
ntot
in
+2ξ1+ξ2
(1)
f2(ξ1, ξ2) = K2 −
nA,in+2ξ1+ξ2
ntot
in
+2ξ1+ξ2
nD,in+ξ2
ntot
in
+2ξ1+ξ2
nC,in−ξ1−ξ2
ntot
in
+2ξ1+ξ2
(2)
So what we’re interested in is the solution to the following non-linear system of equations:
f1(ξ1, ξ2)
f2(ξ1, ξ2)
= f(ξ) = 0 (3)
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 5 / 18
Multidimensional Newton-Rhapson
The multidimensional Taylor series
For a 1-dimensional function with 1 independent variable, the Taylor series
expansion around x0 is:
f(x)|x0 = f0 + (x − x0)f + . . .
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 6 / 18
Multidimensional Newton-Rhapson
The multidimensional Taylor series
For a 1-dimensional function with 1 independent variable, the Taylor series
expansion around x0 is:
f(x)|x0 = f0 + (x − x0)f + . . .
For a 1-dimensional function with 2 independent variables, x and y, the
Taylor series expansion around (x0, y0) is:
f(x, y)|(x0,y0) = f0 + (x − x0)
∂f
∂x
+ (y − y0)
∂f
∂y
+ . . .
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 6 / 18
Multidimensional Newton-Rhapson
The multidimensional Taylor series
For a 1-dimensional function with 1 independent variable, the Taylor series
expansion around x0 is:
f(x)|x0 = f0 + (x − x0)f + . . .
For a 1-dimensional function with 2 independent variables, x and y, the
Taylor series expansion around (x0, y0) is:
f(x, y)|(x0,y0) = f0 + (x − x0)
∂f
∂x
+ (y − y0)
∂f
∂y
+ . . .
For a 2 dimensional function, with 2 independent variables (ξ1, ξ2):
f1(ξ1, ξ2)|(ξ1,0,ξ2,0) = f1,0 + (ξ1 − ξ1,0)
∂f1
∂ξ1
+ (ξ2 − ξ2,0)
∂f1
∂ξ2
+ . . .
f2(ξ1, ξ2)|(ξ1,0,ξ2,0) = f2,0 + (ξ1 − ξ1,0)
∂f2
∂ξ1
+ (ξ2 − ξ2,0)
∂f2
∂ξ2
+ . . .
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 6 / 18
Multidimensional Newton-Rhapson
The multidimensional Taylor series
For a 2 dimensional function, with 2 independent variables (ξ1, ξ2):
f1(ξ1, ξ2)|(ξ1,0,ξ2,0) = f1,0 + (ξ1 − ξ1,0)
∂f1
∂ξ1
+ (ξ2 − ξ2,0)
∂f1
∂ξ2
+ . . .
f2(ξ1, ξ2)|(ξ1,0,ξ2,0) = f2,0 + (ξ1 − ξ1,0)
∂f2
∂ξ1
+ (ξ2 − ξ2,0)
∂f2
∂ξ2
+ . . .
We can write this more compactly with linear algebra:
f1(ξ1, ξ2)
f1(ξ1, ξ2)
f
=
f1,0
f2,0
f0
+
∂f1
∂ξ1
∂f1
∂ξ2
∂f2
∂ξ1
∂f2
∂ξ2
J
(ξ1 − ξ1,0)
(ξ2 − ξ2,0)
∆ξ
+ · · ·
f ≈ f0 + J∆ξ
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 7 / 18
Multidimensional Newton-Rhapson
The Jacobian Matrix
f ≈ f0 + J∆ξ
The key to dealing with multidimensional functions is the Jacobian Matrix J
For an m dimensional function with n independent variables:
J =








∂f1
∂x1
∂f1
∂x2
∂f1
∂x3
· · · ∂f1
∂xn
∂f2
∂x1
∂f2
∂x2
∂f2
∂x3
· · · ∂f2
∂xn
∂f3
∂x1
∂f3
∂x2
∂f3
∂x3
· · · ∂f3
∂xn
...
...
...
...
...
∂fm
∂x1
∂fm
∂x2
∂fm
∂x3
· · · ∂fm
∂xn








Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 8 / 18
Multidimensional Newton-Rhapson
The Jacobian Matrix
f ≈ f0 + J∆ξ
So for our reactor with 2 reactions we have a
2-dimensional function, with 2 independent variables
ξ1 and ξ2:
J =


∂f1
∂ξ1
∂f1
∂ξ2
∂f2
∂ξ1
∂f2
∂ξ2


This shows the first non-linear equation, f1(ξ1, ξ2).
The black plane shows where f1 = 0
f
ξ1 ξ2
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 9 / 18
Multidimensional Newton-Rhapson
The Jacobian Matrix
f ≈ f0 + J∆ξ
So for our reactor with 2 reactions we have a
2-dimensional function, with 2 independent variables
ξ1 and ξ2:
J =


∂f1
∂ξ1
∂f1
∂ξ2
∂f2
∂ξ1
∂f2
∂ξ2


This shows the first non-linear equation, f1(ξ1, ξ2).
The black plane shows where f1 = 0
The light-colored plane is the tangent plane to
f1(ξ1, ξ2) at a single point:
ξ0 =
35
25
The equation of the tangent plane is:
P1 = f1(xi0) + J(1,:)*(xi - xi0);
f
ξ1 ξ2
ξ0
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 9 / 18
Multidimensional Newton-Rhapson
The Jacobian Matrix
f ≈ f0 + J∆ξ
So for our reactor with 2 reactions we have a
2-dimensional function, with 2 independent variables
ξ1 and ξ2:
J =


∂f1
∂ξ1
∂f1
∂ξ2
∂f2
∂ξ1
∂f2
∂ξ2


This shows the second non-linear equation,
f2(ξ1, ξ2).
The black plane shows where f2 = 0
f
ξ1 ξ2
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 9 / 18
Multidimensional Newton-Rhapson
The Jacobian Matrix
f ≈ f0 + J∆ξ
So for our reactor with 2 reactions we have a
2-dimensional function, with 2 independent variables
ξ1 and ξ2:
J =


∂f1
∂ξ1
∂f1
∂ξ2
∂f2
∂ξ1
∂f2
∂ξ2


This shows the second non-linear equation,
f2(ξ1, ξ2).
The black plane shows where f2 = 0
The light-colored plane is the tangent plane to
f2(ξ1, ξ2) at a single point:
ξ0 =
35
25
The equation of the tangent plane is:
t2 = f2(xi0)+ J(2,:)*(xi - xi0);
f
ξ1 ξ2
ξ0
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 9 / 18
Multidimensional Newton-Rhapson
The Jacobian Matrix
f ≈ f0 + J∆ξ
So for our reactor with 2 reactions we have a
2-dimensional function, with 2 independent variables
ξ1 and ξ2:
J =


∂f1
∂ξ1
∂f1
∂ξ2
∂f2
∂ξ1
∂f2
∂ξ2


This shows the both components (f1, f2), of
f(ξ1, ξ2).
The black plane shows where f = 0
The extent of the simultaneous reactions is the single
point where the red, blue, and black surfaces
intersect.
f
ξ1 ξ2
f(ξ) = 0
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 9 / 18
Multidimensional Newton-Rhapson
Relationship to 1-dimensional Newton-Rhapson
1 Suppose we are interested in
the solution to:
x3
2
+ x = −x2
2
− 1
5
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 10 / 18
Multidimensional Newton-Rhapson
Relationship to 1-dimensional Newton-Rhapson
1 Suppose we are interested in
the solution to:
x3
2
+ x = −x2
2
− 1
5
The root-finding equation is:
x3
2
+
x2
2
+ x +
1
5
f(x)
= 0
f(x) = x3
2
+ x2
2
+ x + 1
5
-3 0 3
–4
0
4
xr
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 10 / 18
Multidimensional Newton-Rhapson
Relationship to 1-dimensional Newton-Rhapson
0 Choose an initial guess, e.g.,
xg = −2
f(x) = x3
2
+ x2
2
+ x + 1
5
-3 0 3
–4
0
4
xrxg
fg
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 10 / 18
Multidimensional Newton-Rhapson
Relationship to 1-dimensional Newton-Rhapson
0 Choose an initial guess, e.g.,
xg = −2
1 Our next guess is
x
(new)
g = x
(old)
g − f0
f0
Graphically, this is done by
drawing the tangent line from
(x
(old)
g , f(x
(old)
g )) through the
y-axis to determine the root.
f(x) = x3
2
+ x2
2
+ x + 1
5
-3 0 3
–4
0
4
xrxg
fg
x
(new)
g
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 10 / 18
Multidimensional Newton-Rhapson
Relationship to 1-dimensional Newton-Rhapson
0 Choose an initial guess, e.g.,
xg = −2
1 Our next guess is
x
(new)
g = x
(old)
g − f0
f0
Graphically, this is done by
drawing the tangent line from
(x
(old)
g , f(x
(old)
g )) through the
y-axis to determine the root.
2 This is the new point from the
first iteration
f(x) = x3
2
+ x2
2
+ x + 1
5
-3 0 3
–4
0
4
xr
x
(new)
g
f
(new)
g
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 10 / 18
Multidimensional Newton-Rhapson
Relationship to 1-dimensional Newton-Rhapson
We derived 1-dimensional Newton-Rhapson
by using the 1-order truncated Taylor series of
f(x):
f(x) ≈ f(x0) + f (x0)(x − x0)
(this is the equation of the line tangent to f(x)
at x0)
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 11 / 18
Multidimensional Newton-Rhapson
Relationship to 1-dimensional Newton-Rhapson
We derived 1-dimensional Newton-Rhapson
by using the 1-order truncated Taylor series of
f(x):
f(x) ≈ f(x0) + f (x0)(x − x0)
(this is the equation of the line tangent to f(x)
at x0)
We set this to zero:
0 ≈ f(x0) + f (x0)(xnew − x0)
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 11 / 18
Multidimensional Newton-Rhapson
Relationship to 1-dimensional Newton-Rhapson
We derived 1-dimensional Newton-Rhapson
by using the 1-order truncated Taylor series of
f(x):
f(x) ≈ f(x0) + f (x0)(x − x0)
(this is the equation of the line tangent to f(x)
at x0)
We set this to zero:
0 ≈ f(x0) + f (x0)(xnew − x0)
And the update equation is then:
xnew = x0 −
f(x0)
f (x0)
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 11 / 18
Multidimensional Newton-Rhapson
Relationship to 1-dimensional Newton-Rhapson
We derived 1-dimensional Newton-Rhapson
by using the 1-order truncated Taylor series of
f(x):
f(x) ≈ f(x0) + f (x0)(x − x0)
(this is the equation of the line tangent to f(x)
at x0)
We set this to zero:
0 ≈ f(x0) + f (x0)(xnew − x0)
And the update equation is then:
xnew = x0 −
f(x0)
f (x0)
Multidimensional Newton-Rhapson uses the
1-order truncated Taylor series of f(x):
f(x) ≈ f(x0) + J(x0)(x − x0)
(this is the equation of the planes tangent to
f(x) at x0)
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 11 / 18
Multidimensional Newton-Rhapson
Relationship to 1-dimensional Newton-Rhapson
We derived 1-dimensional Newton-Rhapson
by using the 1-order truncated Taylor series of
f(x):
f(x) ≈ f(x0) + f (x0)(x − x0)
(this is the equation of the line tangent to f(x)
at x0)
We set this to zero:
0 ≈ f(x0) + f (x0)(xnew − x0)
And the update equation is then:
xnew = x0 −
f(x0)
f (x0)
Multidimensional Newton-Rhapson uses the
1-order truncated Taylor series of f(x):
f(x) ≈ f(x0) + J(x0)(x − x0)
(this is the equation of the planes tangent to
f(x) at x0)
We set this to 0:
0 ≈ f(x0) + J(x0)(xnew − x0)
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 11 / 18
Multidimensional Newton-Rhapson
Relationship to 1-dimensional Newton-Rhapson
We derived 1-dimensional Newton-Rhapson
by using the 1-order truncated Taylor series of
f(x):
f(x) ≈ f(x0) + f (x0)(x − x0)
(this is the equation of the line tangent to f(x)
at x0)
We set this to zero:
0 ≈ f(x0) + f (x0)(xnew − x0)
And the update equation is then:
xnew = x0 −
f(x0)
f (x0)
Multidimensional Newton-Rhapson uses the
1-order truncated Taylor series of f(x):
f(x) ≈ f(x0) + J(x0)(x − x0)
(this is the equation of the planes tangent to
f(x) at x0)
We set this to 0:
0 ≈ f(x0) + J(x0)(xnew − x0)
And the update equation is then:
xnew = x0 − J−1
f(x0)
Or in Matlab:
x = x - J f
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 11 / 18
Multidimensional Newton-Rhapson
Graphical interpretation
This shows the two functions we want to
simultaneously set to zero to solve for the two
extents of reaction.
f
ξ1 ξ2
f(ξ) = 0
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 12 / 18
Multidimensional Newton-Rhapson
Graphical interpretation
This shows the two functions we want to
simultaneously set to zero to solve for the two
extents of reaction.
We choose an initial guess, say
ξ0 =
35
25
Now we take the partial derivatives of f to
form the Jacobian matrix J, and evaluate it at
ξ0
f
ξ1 ξ2
f(ξ0)
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 12 / 18
Multidimensional Newton-Rhapson
Graphical interpretation
This shows the two functions we want to
simultaneously set to zero to solve for the two
extents of reaction.
We choose an initial guess, say
ξ0 =
35
25
Now we take the partial derivatives of f to
form the Jacobian matrix J, and evaluate it at
ξ0
f
ξ1 ξ2
f(ξ0)
ξnext
Mathematically, the first update to our initial guess is
ξnew = ξ0 − J−1
f(ξ0)
Graphically, we draw the tangent planes to f1(ξ) and f2(ξ).
The next guess is the intersection of these two planes at f = 0
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 12 / 18
Broyden’s Method
The multidimensional Secant algorithm
We don’t usually use 1-dimensional Newton-Rhapson method due to the
inconvenience of determining the analytic derivative of f(x).
Instead we generally employ the secant method, which uses the finite difference
approximation to numerically estimate f (x)
f (x) ≈
f(x1) − f(x0)
x1 − x0
We don’t usually use Multidimensional Newton-Rhapson, either. We would have to
calculate 4 partial derivatives in this reactor example.
Instead, we can estimate the numerical value of the Jacobian matrix using the
central finite difference formula:
∂fi
∂xj
≈
1
2h
fi(x + h) − fi(x − h)
h is a vector of length(x). All of its elements are zero except for hj, which is some small
number. The smaller the number, the more accurate the derivative.
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 13 / 18
Broyden’s Method
The multidimensional Secant algorithm
The algorithm
1 Enter all of the non-linear functions as a single
function handle:
S1 = [ 2 1 -1 0 ];
S2 = [ 1 0 -1 1 ];
K = [ 0.32 2.6 ];
n_tot = @(xi) sum(n_in) + ...
5 sum(S1) .* xi(1) + ...
sum(S2) .* xi(2)
n_out = @(xi) n_in + ...
S1 .* xi(1) + ...
S2 .* xi(2)
10 f = @(xi) ...
[ K(1) - ...
prod( n_out(xi).^S1 ) ...
/ n_tot(xi).^sum(S1) ; ...
K(2) - ...
15 prod( n_out(xi).^S2 ) ...
/ n_tot(xi).^sum(S2) ]
The reactions:
C
K1 = 0.32
−−−−−−−−−−−−−−−− 2 A + B
C
K2 = 2.60
−−−−−−−−−−−−−−−− A + D
S1 = [2 1 −1 0]
S2 = [1 0 −1 1]
The non-linear equations:
K1 =
C2
A
CB
CC
K2 =
CACD
CC
The root-finding functions:
f1(ξ1, ξ2) = K1 −


nA,in+2ξ1+ξ2
ntot
in
+2ξ1+ξ2


2 

nB,in+ξ1
ntot
in
+2ξ1+ξ2




nC,in−ξ1−ξ2
ntot
in
+2ξ1+ξ2


f2(ξ1, ξ2) = K2 −


nA,in+2ξ1+ξ2
ntot
in
+2ξ1+ξ2




nD,in+ξ2
ntot
in
+2ξ1+ξ2




nC,in−ξ1−ξ2
ntot
in
+2ξ1+ξ2


Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 14 / 18
Broyden’s Method
The multidimensional Secant algorithm
The algorithm
2 Choose an initial guess
xi0 = [ 35; 25] ;
It is always helpful to have some idea of the neighborhood of the solution.
In this example, we know that component C is reacting. At least 0 moles of C will be
converted.
Therefore, our initial guesses should be positive.
Also, no more than nC,in moles of C can be converted. There are two reactions, so the
total number of moles of C consumed will be ξ1 + ξ2.
If 100 moles of C are entering the reactor, then ξ1 + ξ2 < 100
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 15 / 18
Broyden’s Method
The multidimensional Secant algorithm
The algorithm
3 Choose a tolerance, and initialize an error variable to be greater than this.
We will also initialize an iteration counter
15 err = tol + 1;
iter = 0;
xi = xi0;
4 We can begin iterating now:
while err > tol
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 16 / 18
Broyden’s Method
The multidimensional Secant algorithm
The algorithm
5 Numerically calculate the Jacobian matrix J
We have to choose a value of h to use in the finite difference formula. Any
small number will work.
h = 0.01;
20 J = jacobian_numeric(f, xi, h)
6 Calculate the next guess
xi_last = xi;
for ii = 1:length(f)
ff(ii) = f{ii}(xi);
end
25 xi = xi - J  ff;
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 17 / 18
Broyden’s Method
The multidimensional Secant algorithm
The algorithm
7 Calculate the error
err = max(abs( (xi - xi_last) ./ xi ) )
iter = iter + 1;
8 Repeat
end
Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 18 / 18

Lecture 9 f17

  • 1.
    Lecture 9—Non-linear systems MultidimensionalNewton-Rhapson We have dealt with the solution of a single non-linear equation and the simultaneous solution of multiple linear equations. Now we are going to look at the simultaneous solution of solving multiple non-linear equations. To start, let’s consider a single equilibrium reaction: A −−→←−− B + 2 C The stoichiometric coefficients are s = [−1 12] for A, B, and C. To find the equilibrium conversion, solve for the reaction coordinate using the single equilibrium relationship: f(ξ) = K − nB,in+ξ ntot in +2ξ nC,in+2ξ ntot in +2ξ 2 nA,in−ξ ntot in +2ξ The value of ξ that satisfies f(ξ) = 0 is the extent of reaction that satisfies the equilibrium relationship. Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 1 / 18
  • 2.
    Multidimensional Newton-Rhapson We couldsolve this equation using the Newton-Rhapson method: ξnext = ξlast − f(ξlast ) f (ξlast ) This is cumbersome since we would have to calculate the derivative of f(ξ). Instead, we could use the Secant method: ξiter = ξiter−1 − f(ξiter−1) ξiter−1 − ξiter−2 f(ξiter−1) − f(ξiter−2) ≈ 1 f (ξiter−1) Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 2 / 18
  • 3.
    Multidimensional Newton-Rhapson It isoften the case that more than 1 reaction occurs within a reactor: reactor_eq.m C K1 −−−− 2 A + B C K2 −−−− A + D K1 = C2 ACB CC K2 = CACD CC S = 2 1 −1 0 1 0 −1 1 100 mol C s Now we have two coupled non-linear equations: K1 = nA,in+2ξ1+ξ2 ntot in +2ξ1+ξ2 2 nB,in+ξ1 ntot in +2ξ1+ξ2 nC,in−ξ1−ξ2 ntot in +2ξ1+ξ2 (1) K2 = nA,in+2ξ1+ξ2 ntot in +2ξ1+ξ2 nD,in+ξ2 ntot in +2ξ1+ξ2 nC,in−ξ1−ξ2 ntot in +2ξ1+ξ2 (2) Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 3 / 18
  • 4.
    Multidimensional Newton-Rhapson Now wehave two coupled non-linear equations: K1 = nA,in+2ξ1+ξ2 ntot in +2ξ1+ξ2 2 nB,in+ξ1 ntot in +2ξ1+ξ2 nC,in−ξ1−ξ2 ntot in +2ξ1+ξ2 (1) K2 = nA,in+2ξ1+ξ2 ntot in +2ξ1+ξ2 nD,in+ξ2 ntot in +2ξ1+ξ2 nC,in−ξ1−ξ2 ntot in +2ξ1+ξ2 (2) There are 2 equations and 2 unknowns (ξ1 and ξ2). So there exists 1 unique solution. Convert these equations into a root-finding form: f1(ξ1, ξ2) = K1 − nA,in+2ξ1+ξ2 ntot in +2ξ1+ξ2 2 nB,in+ξ1 ntot in +2ξ1+ξ2 nC,in−ξ1−ξ2 ntot in +2ξ1+ξ2 (3) f2(ξ1, ξ2) = K2 − nA,in+2ξ1+ξ2 ntot in +2ξ1+ξ2 nD,in+ξ2 ntot in +2ξ1+ξ2 nC,in−ξ1−ξ2 ntot in +2ξ1+ξ2 (4) Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 4 / 18
  • 5.
    Multidimensional Newton-Rhapson There are2 equations and 2 unknowns (ξ1 and ξ2). So there exists 1 unique solution. Convert these equations into a root-finding form: f1(ξ1, ξ2) = K1 − nA,in+2ξ1+ξ2 ntot in +2ξ1+ξ2 2 nB,in+ξ1 ntot in +2ξ1+ξ2 nC,in−ξ1−ξ2 ntot in +2ξ1+ξ2 (1) f2(ξ1, ξ2) = K2 − nA,in+2ξ1+ξ2 ntot in +2ξ1+ξ2 nD,in+ξ2 ntot in +2ξ1+ξ2 nC,in−ξ1−ξ2 ntot in +2ξ1+ξ2 (2) So what we’re interested in is the solution to the following non-linear system of equations: f1(ξ1, ξ2) f2(ξ1, ξ2) = f(ξ) = 0 (3) Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 5 / 18
  • 6.
    Multidimensional Newton-Rhapson The multidimensionalTaylor series For a 1-dimensional function with 1 independent variable, the Taylor series expansion around x0 is: f(x)|x0 = f0 + (x − x0)f + . . . Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 6 / 18
  • 7.
    Multidimensional Newton-Rhapson The multidimensionalTaylor series For a 1-dimensional function with 1 independent variable, the Taylor series expansion around x0 is: f(x)|x0 = f0 + (x − x0)f + . . . For a 1-dimensional function with 2 independent variables, x and y, the Taylor series expansion around (x0, y0) is: f(x, y)|(x0,y0) = f0 + (x − x0) ∂f ∂x + (y − y0) ∂f ∂y + . . . Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 6 / 18
  • 8.
    Multidimensional Newton-Rhapson The multidimensionalTaylor series For a 1-dimensional function with 1 independent variable, the Taylor series expansion around x0 is: f(x)|x0 = f0 + (x − x0)f + . . . For a 1-dimensional function with 2 independent variables, x and y, the Taylor series expansion around (x0, y0) is: f(x, y)|(x0,y0) = f0 + (x − x0) ∂f ∂x + (y − y0) ∂f ∂y + . . . For a 2 dimensional function, with 2 independent variables (ξ1, ξ2): f1(ξ1, ξ2)|(ξ1,0,ξ2,0) = f1,0 + (ξ1 − ξ1,0) ∂f1 ∂ξ1 + (ξ2 − ξ2,0) ∂f1 ∂ξ2 + . . . f2(ξ1, ξ2)|(ξ1,0,ξ2,0) = f2,0 + (ξ1 − ξ1,0) ∂f2 ∂ξ1 + (ξ2 − ξ2,0) ∂f2 ∂ξ2 + . . . Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 6 / 18
  • 9.
    Multidimensional Newton-Rhapson The multidimensionalTaylor series For a 2 dimensional function, with 2 independent variables (ξ1, ξ2): f1(ξ1, ξ2)|(ξ1,0,ξ2,0) = f1,0 + (ξ1 − ξ1,0) ∂f1 ∂ξ1 + (ξ2 − ξ2,0) ∂f1 ∂ξ2 + . . . f2(ξ1, ξ2)|(ξ1,0,ξ2,0) = f2,0 + (ξ1 − ξ1,0) ∂f2 ∂ξ1 + (ξ2 − ξ2,0) ∂f2 ∂ξ2 + . . . We can write this more compactly with linear algebra: f1(ξ1, ξ2) f1(ξ1, ξ2) f = f1,0 f2,0 f0 + ∂f1 ∂ξ1 ∂f1 ∂ξ2 ∂f2 ∂ξ1 ∂f2 ∂ξ2 J (ξ1 − ξ1,0) (ξ2 − ξ2,0) ∆ξ + · · · f ≈ f0 + J∆ξ Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 7 / 18
  • 10.
    Multidimensional Newton-Rhapson The JacobianMatrix f ≈ f0 + J∆ξ The key to dealing with multidimensional functions is the Jacobian Matrix J For an m dimensional function with n independent variables: J =         ∂f1 ∂x1 ∂f1 ∂x2 ∂f1 ∂x3 · · · ∂f1 ∂xn ∂f2 ∂x1 ∂f2 ∂x2 ∂f2 ∂x3 · · · ∂f2 ∂xn ∂f3 ∂x1 ∂f3 ∂x2 ∂f3 ∂x3 · · · ∂f3 ∂xn ... ... ... ... ... ∂fm ∂x1 ∂fm ∂x2 ∂fm ∂x3 · · · ∂fm ∂xn         Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 8 / 18
  • 11.
    Multidimensional Newton-Rhapson The JacobianMatrix f ≈ f0 + J∆ξ So for our reactor with 2 reactions we have a 2-dimensional function, with 2 independent variables ξ1 and ξ2: J =   ∂f1 ∂ξ1 ∂f1 ∂ξ2 ∂f2 ∂ξ1 ∂f2 ∂ξ2   This shows the first non-linear equation, f1(ξ1, ξ2). The black plane shows where f1 = 0 f ξ1 ξ2 Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 9 / 18
  • 12.
    Multidimensional Newton-Rhapson The JacobianMatrix f ≈ f0 + J∆ξ So for our reactor with 2 reactions we have a 2-dimensional function, with 2 independent variables ξ1 and ξ2: J =   ∂f1 ∂ξ1 ∂f1 ∂ξ2 ∂f2 ∂ξ1 ∂f2 ∂ξ2   This shows the first non-linear equation, f1(ξ1, ξ2). The black plane shows where f1 = 0 The light-colored plane is the tangent plane to f1(ξ1, ξ2) at a single point: ξ0 = 35 25 The equation of the tangent plane is: P1 = f1(xi0) + J(1,:)*(xi - xi0); f ξ1 ξ2 ξ0 Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 9 / 18
  • 13.
    Multidimensional Newton-Rhapson The JacobianMatrix f ≈ f0 + J∆ξ So for our reactor with 2 reactions we have a 2-dimensional function, with 2 independent variables ξ1 and ξ2: J =   ∂f1 ∂ξ1 ∂f1 ∂ξ2 ∂f2 ∂ξ1 ∂f2 ∂ξ2   This shows the second non-linear equation, f2(ξ1, ξ2). The black plane shows where f2 = 0 f ξ1 ξ2 Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 9 / 18
  • 14.
    Multidimensional Newton-Rhapson The JacobianMatrix f ≈ f0 + J∆ξ So for our reactor with 2 reactions we have a 2-dimensional function, with 2 independent variables ξ1 and ξ2: J =   ∂f1 ∂ξ1 ∂f1 ∂ξ2 ∂f2 ∂ξ1 ∂f2 ∂ξ2   This shows the second non-linear equation, f2(ξ1, ξ2). The black plane shows where f2 = 0 The light-colored plane is the tangent plane to f2(ξ1, ξ2) at a single point: ξ0 = 35 25 The equation of the tangent plane is: t2 = f2(xi0)+ J(2,:)*(xi - xi0); f ξ1 ξ2 ξ0 Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 9 / 18
  • 15.
    Multidimensional Newton-Rhapson The JacobianMatrix f ≈ f0 + J∆ξ So for our reactor with 2 reactions we have a 2-dimensional function, with 2 independent variables ξ1 and ξ2: J =   ∂f1 ∂ξ1 ∂f1 ∂ξ2 ∂f2 ∂ξ1 ∂f2 ∂ξ2   This shows the both components (f1, f2), of f(ξ1, ξ2). The black plane shows where f = 0 The extent of the simultaneous reactions is the single point where the red, blue, and black surfaces intersect. f ξ1 ξ2 f(ξ) = 0 Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 9 / 18
  • 16.
    Multidimensional Newton-Rhapson Relationship to1-dimensional Newton-Rhapson 1 Suppose we are interested in the solution to: x3 2 + x = −x2 2 − 1 5 Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 10 / 18
  • 17.
    Multidimensional Newton-Rhapson Relationship to1-dimensional Newton-Rhapson 1 Suppose we are interested in the solution to: x3 2 + x = −x2 2 − 1 5 The root-finding equation is: x3 2 + x2 2 + x + 1 5 f(x) = 0 f(x) = x3 2 + x2 2 + x + 1 5 -3 0 3 –4 0 4 xr Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 10 / 18
  • 18.
    Multidimensional Newton-Rhapson Relationship to1-dimensional Newton-Rhapson 0 Choose an initial guess, e.g., xg = −2 f(x) = x3 2 + x2 2 + x + 1 5 -3 0 3 –4 0 4 xrxg fg Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 10 / 18
  • 19.
    Multidimensional Newton-Rhapson Relationship to1-dimensional Newton-Rhapson 0 Choose an initial guess, e.g., xg = −2 1 Our next guess is x (new) g = x (old) g − f0 f0 Graphically, this is done by drawing the tangent line from (x (old) g , f(x (old) g )) through the y-axis to determine the root. f(x) = x3 2 + x2 2 + x + 1 5 -3 0 3 –4 0 4 xrxg fg x (new) g Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 10 / 18
  • 20.
    Multidimensional Newton-Rhapson Relationship to1-dimensional Newton-Rhapson 0 Choose an initial guess, e.g., xg = −2 1 Our next guess is x (new) g = x (old) g − f0 f0 Graphically, this is done by drawing the tangent line from (x (old) g , f(x (old) g )) through the y-axis to determine the root. 2 This is the new point from the first iteration f(x) = x3 2 + x2 2 + x + 1 5 -3 0 3 –4 0 4 xr x (new) g f (new) g Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 10 / 18
  • 21.
    Multidimensional Newton-Rhapson Relationship to1-dimensional Newton-Rhapson We derived 1-dimensional Newton-Rhapson by using the 1-order truncated Taylor series of f(x): f(x) ≈ f(x0) + f (x0)(x − x0) (this is the equation of the line tangent to f(x) at x0) Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 11 / 18
  • 22.
    Multidimensional Newton-Rhapson Relationship to1-dimensional Newton-Rhapson We derived 1-dimensional Newton-Rhapson by using the 1-order truncated Taylor series of f(x): f(x) ≈ f(x0) + f (x0)(x − x0) (this is the equation of the line tangent to f(x) at x0) We set this to zero: 0 ≈ f(x0) + f (x0)(xnew − x0) Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 11 / 18
  • 23.
    Multidimensional Newton-Rhapson Relationship to1-dimensional Newton-Rhapson We derived 1-dimensional Newton-Rhapson by using the 1-order truncated Taylor series of f(x): f(x) ≈ f(x0) + f (x0)(x − x0) (this is the equation of the line tangent to f(x) at x0) We set this to zero: 0 ≈ f(x0) + f (x0)(xnew − x0) And the update equation is then: xnew = x0 − f(x0) f (x0) Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 11 / 18
  • 24.
    Multidimensional Newton-Rhapson Relationship to1-dimensional Newton-Rhapson We derived 1-dimensional Newton-Rhapson by using the 1-order truncated Taylor series of f(x): f(x) ≈ f(x0) + f (x0)(x − x0) (this is the equation of the line tangent to f(x) at x0) We set this to zero: 0 ≈ f(x0) + f (x0)(xnew − x0) And the update equation is then: xnew = x0 − f(x0) f (x0) Multidimensional Newton-Rhapson uses the 1-order truncated Taylor series of f(x): f(x) ≈ f(x0) + J(x0)(x − x0) (this is the equation of the planes tangent to f(x) at x0) Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 11 / 18
  • 25.
    Multidimensional Newton-Rhapson Relationship to1-dimensional Newton-Rhapson We derived 1-dimensional Newton-Rhapson by using the 1-order truncated Taylor series of f(x): f(x) ≈ f(x0) + f (x0)(x − x0) (this is the equation of the line tangent to f(x) at x0) We set this to zero: 0 ≈ f(x0) + f (x0)(xnew − x0) And the update equation is then: xnew = x0 − f(x0) f (x0) Multidimensional Newton-Rhapson uses the 1-order truncated Taylor series of f(x): f(x) ≈ f(x0) + J(x0)(x − x0) (this is the equation of the planes tangent to f(x) at x0) We set this to 0: 0 ≈ f(x0) + J(x0)(xnew − x0) Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 11 / 18
  • 26.
    Multidimensional Newton-Rhapson Relationship to1-dimensional Newton-Rhapson We derived 1-dimensional Newton-Rhapson by using the 1-order truncated Taylor series of f(x): f(x) ≈ f(x0) + f (x0)(x − x0) (this is the equation of the line tangent to f(x) at x0) We set this to zero: 0 ≈ f(x0) + f (x0)(xnew − x0) And the update equation is then: xnew = x0 − f(x0) f (x0) Multidimensional Newton-Rhapson uses the 1-order truncated Taylor series of f(x): f(x) ≈ f(x0) + J(x0)(x − x0) (this is the equation of the planes tangent to f(x) at x0) We set this to 0: 0 ≈ f(x0) + J(x0)(xnew − x0) And the update equation is then: xnew = x0 − J−1 f(x0) Or in Matlab: x = x - J f Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 11 / 18
  • 27.
    Multidimensional Newton-Rhapson Graphical interpretation Thisshows the two functions we want to simultaneously set to zero to solve for the two extents of reaction. f ξ1 ξ2 f(ξ) = 0 Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 12 / 18
  • 28.
    Multidimensional Newton-Rhapson Graphical interpretation Thisshows the two functions we want to simultaneously set to zero to solve for the two extents of reaction. We choose an initial guess, say ξ0 = 35 25 Now we take the partial derivatives of f to form the Jacobian matrix J, and evaluate it at ξ0 f ξ1 ξ2 f(ξ0) Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 12 / 18
  • 29.
    Multidimensional Newton-Rhapson Graphical interpretation Thisshows the two functions we want to simultaneously set to zero to solve for the two extents of reaction. We choose an initial guess, say ξ0 = 35 25 Now we take the partial derivatives of f to form the Jacobian matrix J, and evaluate it at ξ0 f ξ1 ξ2 f(ξ0) ξnext Mathematically, the first update to our initial guess is ξnew = ξ0 − J−1 f(ξ0) Graphically, we draw the tangent planes to f1(ξ) and f2(ξ). The next guess is the intersection of these two planes at f = 0 Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 12 / 18
  • 30.
    Broyden’s Method The multidimensionalSecant algorithm We don’t usually use 1-dimensional Newton-Rhapson method due to the inconvenience of determining the analytic derivative of f(x). Instead we generally employ the secant method, which uses the finite difference approximation to numerically estimate f (x) f (x) ≈ f(x1) − f(x0) x1 − x0 We don’t usually use Multidimensional Newton-Rhapson, either. We would have to calculate 4 partial derivatives in this reactor example. Instead, we can estimate the numerical value of the Jacobian matrix using the central finite difference formula: ∂fi ∂xj ≈ 1 2h fi(x + h) − fi(x − h) h is a vector of length(x). All of its elements are zero except for hj, which is some small number. The smaller the number, the more accurate the derivative. Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 13 / 18
  • 31.
    Broyden’s Method The multidimensionalSecant algorithm The algorithm 1 Enter all of the non-linear functions as a single function handle: S1 = [ 2 1 -1 0 ]; S2 = [ 1 0 -1 1 ]; K = [ 0.32 2.6 ]; n_tot = @(xi) sum(n_in) + ... 5 sum(S1) .* xi(1) + ... sum(S2) .* xi(2) n_out = @(xi) n_in + ... S1 .* xi(1) + ... S2 .* xi(2) 10 f = @(xi) ... [ K(1) - ... prod( n_out(xi).^S1 ) ... / n_tot(xi).^sum(S1) ; ... K(2) - ... 15 prod( n_out(xi).^S2 ) ... / n_tot(xi).^sum(S2) ] The reactions: C K1 = 0.32 −−−−−−−−−−−−−−−− 2 A + B C K2 = 2.60 −−−−−−−−−−−−−−−− A + D S1 = [2 1 −1 0] S2 = [1 0 −1 1] The non-linear equations: K1 = C2 A CB CC K2 = CACD CC The root-finding functions: f1(ξ1, ξ2) = K1 −   nA,in+2ξ1+ξ2 ntot in +2ξ1+ξ2   2   nB,in+ξ1 ntot in +2ξ1+ξ2     nC,in−ξ1−ξ2 ntot in +2ξ1+ξ2   f2(ξ1, ξ2) = K2 −   nA,in+2ξ1+ξ2 ntot in +2ξ1+ξ2     nD,in+ξ2 ntot in +2ξ1+ξ2     nC,in−ξ1−ξ2 ntot in +2ξ1+ξ2   Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 14 / 18
  • 32.
    Broyden’s Method The multidimensionalSecant algorithm The algorithm 2 Choose an initial guess xi0 = [ 35; 25] ; It is always helpful to have some idea of the neighborhood of the solution. In this example, we know that component C is reacting. At least 0 moles of C will be converted. Therefore, our initial guesses should be positive. Also, no more than nC,in moles of C can be converted. There are two reactions, so the total number of moles of C consumed will be ξ1 + ξ2. If 100 moles of C are entering the reactor, then ξ1 + ξ2 < 100 Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 15 / 18
  • 33.
    Broyden’s Method The multidimensionalSecant algorithm The algorithm 3 Choose a tolerance, and initialize an error variable to be greater than this. We will also initialize an iteration counter 15 err = tol + 1; iter = 0; xi = xi0; 4 We can begin iterating now: while err > tol Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 16 / 18
  • 34.
    Broyden’s Method The multidimensionalSecant algorithm The algorithm 5 Numerically calculate the Jacobian matrix J We have to choose a value of h to use in the finite difference formula. Any small number will work. h = 0.01; 20 J = jacobian_numeric(f, xi, h) 6 Calculate the next guess xi_last = xi; for ii = 1:length(f) ff(ii) = f{ii}(xi); end 25 xi = xi - J ff; Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 17 / 18
  • 35.
    Broyden’s Method The multidimensionalSecant algorithm The algorithm 7 Calculate the error err = max(abs( (xi - xi_last) ./ xi ) ) iter = iter + 1; 8 Repeat end Che 310 | Chapra 12.2–3 | Non-linear Systems 9 — NLSE October 24, 2017 18 / 18