Solution of Nonlinear System

Your Progress in this Chapter

0%

0 / 13 units completed

Chapter Overview

Nonlinear Equation

Section 6.1 of 424 min10 code examples

Root of a nonlinear equation near initial guess x_0 can be found using Fzero or Fsolve. It numerically locates a value: x such that: f(x) = 0. This is particularly useful when analytical solutions are difficult or impossible to obtain.

To solve the equation: x\exp(x) = 2, start with initial guess of x_0 = 0.5;

Example 1C#

1
2
3
4
5
6
Code is ready to run
OutputFrom the book
x = 0.8526055020137255

In this case, Fzero first search for an interval that brackets the root. Then uses brent's method to hone in on the root. If we are sure of the interval containing the root, we can save the effort spent on bracketing the root by supplying that.

Example 2C#

1
2
3
4
Code is ready to run
OutputFrom the book
x = 0.8526055020137254

To have window into the solution process, we can using solver setting SolverSet() to get the solver to print out the result after each iteration.

Example 3C#

1
2
3
4
5
6
7
8
9
Code is ready to run
OutputFrom the book
 Search for an interval around 0.5 containing a sign change:
fun-count     a          f(a)           b          f(b)     Procedure 
    1         5e-1   -1.1756e+0         5e-1   -1.1756e+0   initial interval 
    3    4.7172e-1    -1.244e+0    5.2828e-1    -1.104e+0   search          
    5       4.6e-1   -1.2713e+0       5.4e-1   -1.0734e+0   search          
    7    4.4343e-1   -1.3091e+0    5.5657e-1    -1.029e+0   search          
    9       4.2e-1   -1.3608e+0       5.8e-1    -9.641e-1   search          
   11    3.8686e-1   -1.4304e+0    6.1314e-1   -8.6802e-1   search          
   13       3.4e-1   -1.5223e+0       6.6e-1   -7.2304e-1   search          
   15    2.7373e-1   -1.6401e+0    7.2627e-1   -4.9853e-1   search          
   17       1.8e-1   -1.7845e+0       8.2e-1   -1.3819e-1   search          
   19    4.7452e-2   -1.9502e+0    9.5255e-1     4.693e-1   search          

 Solving for solution between 0.047452 and 0.952548
fun-count     x         f(x)       Procedure 
   19    9.5255e-1     4.693e-1    initial        
   20    7.7699e-1    -3.101e-1    interpolation  
   21    8.4684e-1   -2.4938e-2    interpolation  
   22    8.5264e-1    1.5721e-4    interpolation  
   23    8.5261e-1   -6.9891e-7    interpolation  
   24    8.5261e-1  -1.9465e-11    interpolation  
   25    8.5261e-1         0e+0    interpolation  
x = 0.8526055020137255

by setting the solver setting in the case of bracketed root, we can see how the solution process differs from the case of a single initial guess.

Example 4C#

1
2
3
4
5
6
7
8
9
Code is ready to run
OutputFrom the book
fun-count     x         f(x)       Procedure 
    2         1e+0    7.1828e-1    initial        
    3    8.1037e-1   -1.7768e-1    interpolation  
    4    8.4798e-1    -2.004e-2    interpolation  
    5    8.5263e-1    9.9913e-5    interpolation  
    6    8.5261e-1   -3.5651e-7    interpolation  
    7    8.5261e-1  -6.3105e-12    interpolation  
    8    8.5261e-1  -2.2204e-16    interpolation  
x = 0.8526055020137254

SepalSolver also has gradient based "Fsolve", which can use finite difference, user defined functions, or automatic differentiation methods to evaluate the gradient. How to do this is demonstrated in the following example.

  1. Using finite difference

Example 5C#

1
2
3
4
5
6
7
8
9
10
11
12
Code is ready to run
OutputFrom the book
Func-count      x               f(x) 
     1      1.00000E-1     -1.19995E+0
     4      4.99717E-1      3.01682E-2
     7      4.90426E-1      6.23684E-5
    10      4.90406E-1     2.62400E-10
x = 0.4904064540069147
  1. Using user defined function example

Example 6C#

1
2
3
4
5
6
7
8
9
10
11
12
13
Code is ready to run
OutputFrom the book
Func-count      x               f(x) 
     1      1.00000E-1     -1.19995E+0
     2      4.99717E-1      3.01682E-2
     3      4.90426E-1      6.23684E-5
     4      4.90406E-1     2.62401E-10
x = 0.49040645400691485
  1. Using automatic differentiation example

Example 7C#

1
2
3
4
5
6
7
8
9
10
11
12
Code is ready to run
OutputFrom the book
Func-count      x               f(x) 
     1      1.00000E-1     -1.19995E+0
     2      4.99717E-1      3.01682E-2
     3      4.90426E-1      6.23684E-5
     4      4.90406E-1     2.62401E-10
x = 0.49040645400691485

Practical Application

The gas compressibility factor (Z-factor) measures how much a real gas deviates from ideal gas behavior. It is defined as:

Z=PVnRTZ = \frac{P V}{n R T}

where:

  • P = pressure
  • V = volume
  • n = number of moles
  • R = gas constant
  • T = temperature

Accurate determination of Z is essential in petroleum engineering for reservoir simulation, material balance, and pipeline design. Unlike explicit correlations, which provide Z directly as a function of pseudo-reduced pressure (P_{pr}) and pseudo-reduced temperature (T_{pr}), implicit correlations require solving an equation iteratively because Z appears on both sides of the equation.

The Hall–Yarbrough correlation (1973) is one of the most widely used implicit methods for estimating Z. It was developed based on the hard-sphere equation of state and tested against multiple reservoir gas systems. The general form is:

A=0.06125texp(1.2(1t)2)B=14.76t9.76t2+4.58t3C=90.7t242.2t2+42.4t3D=2.18+2.82tAPpr+y+y2+y3y4(1y)3By2+CyD=0Z=APpry\begin{array}{c} A = 0.06125t \exp\left(-1.2(1 - t)^2\right) \\ B = 14.76t - 9.76t^2 + 4.58t^3 \\ C = 90.7t - 242.2t^2 + 42.4t^3 \\ D = 2.18 + 2.82t \\ -AP_{pr} + \cfrac{y + y^2 + y^3 - y^4}{(1 - y)^3} - By^2 + Cy^D = 0 \\ Z = \cfrac{A P_{pr}}{y} \end{array}

where:

  • P_{pr} = P/P_c (pseudo-reduced pressure)
  • T_{pr} = T/T_c (pseudo-reduced temperature)
  • t = 1/T_{pr}
  • P_c, T_c = pseudo-critical properties of the gas mixture

Because reduced density equation is nonlinear, iterative numerical methods such as Newton–Raphson or successive substitution are required to solve it.

Applications

  • Reservoir engineering: material balance calculations and reserves estimation.
  • Pipeline design: predicting pressure drop and flow efficiency.
  • Simulation software: incorporated into PVT packages for automated Z-factor evaluation.

Example 8C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Code is ready to run
OutputFrom the book
Zfactor_Hall_Yarborough_.png
Hall_Yarborough_Chart.png

Example 9C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Code is ready to run
OutputFrom the book
Zfactor_Dranchuk_Abou_Kassem.png
Dranchuk_Abou_Kassem_Chart.png

Example 10C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Code is ready to run
OutputFrom the book
Zfactor_Dranchuk_Purvis_Robinson.png
Dranchuk_Purvis_Robinson_Chart.png