In this post I will give an example of using the previously created Newton Raphson class to solve a system of nonlinear equations.
For this example, I will use four unknowns E1, E2, E3, and E4, and the four equations shown below.
0.005(100 – E1 - 2E2)(1.0 - E1 - E3) = 100E1
500(100 - E1 - 2E2)2 = 100E2
0.5(100 - E1 - E3 - 2.0 E4) = 100E3
10000(100E3 - 2E4)2 = 100E4
Inside of the Loaded event of the Window, I added the code shown below.
Parameter E1 =
new Parameter(0.0);
Parameter E2 =
new Parameter(0.0);
Parameter E3 =
new Parameter(0.0);
Parameter E4 =
new Parameter(0.0);
Func<
double>[] functions =
new Func<
double>[]
{
() => 0.005 * (100.0 - E1 - 2.0 * E2) * (1.0 - E1 - E3) - 100.0 * E1,
() => 500.0 * Math.Pow(100.0 - E1 - 2.0 * E2, 2.0) - 100.0 * E2,
() => 0.5 * (100.0 - E1 - E3 - 2.0 * E4) - 100.0 * E3,
() => 10000.0 * Math.Pow(100.0 * E3 - 2.0 * E4, 2.0) - 100.0 * E4
};
Parameter[] parameters = new Parameter[] { E1, E2, E3, E4 };
NewtonRaphson nr = new NewtonRaphson(parameters, functions);
for (int i = 0; i < 15; i++)
{
nr.Iterate();
}
This code creates an instance of the class created above called rxn, creates an array of functions with the four functions created on the class called funcs, and then creates an instance of the NewtonRaphson class passing in rxn and funcs.
I created a string called strResults which will hold the results of the calculation for each iteration. At the end of the function I copy the contents of strResults to the clipboard so that I can paste them for this post.
The values from strResults are shown in the table below.
|
n
|
E1
|
E2
|
E3
|
E4
|
|
F1
|
F2
|
F3
|
F4
|
|
0
|
1.00000
|
1.00000
|
1.00000
|
1.00000
|
|
-1.005E+02
|
4.704E+06
|
-5.200E+01
|
9.604E+07
|
|
1
|
0.00485
|
25.73431
|
0.49500
|
0.25002
|
|
-3.636E-01
|
1.175E+06
|
-2.416E-13
|
2.401E+07
|
|
2
|
0.00082
|
37.82898
|
0.41362
|
8.43064
|
|
-1.033E-02
|
2.925E+05
|
6.054E-12
|
6.002E+06
|
|
3
|
0.00041
|
43.82447
|
0.37293
|
12.52011
|
|
-2.464E-03
|
7.189E+04
|
-1.563E-13
|
1.500E+06
|
|
4
|
0.00022
|
46.72308
|
0.35260
|
14.56372
|
|
-5.950E-04
|
1.680E+04
|
1.279E-13
|
3.746E+05
|
|
5
|
0.00013
|
47.99535
|
0.34245
|
15.58326
|
|
-1.301E-04
|
3.237E+03
|
-1.208E-13
|
9.324E+04
|
|
6
|
0.00011
|
48.39411
|
0.33743
|
16.08855
|
|
-2.015E-05
|
3.180E+02
|
-7.105E-14
|
2.290E+04
|
|
7
|
0.00010
|
48.44286
|
0.33500
|
16.33250
|
|
-1.185E-06
|
4.753E+00
|
7.105E-15
|
5.338E+03
|
|
8
|
0.00010
|
48.44361
|
0.33394
|
16.43902
|
|
-7.959E-09
|
1.128E-03
|
0.000E+00
|
1.018E+03
|
|
9
|
0.00010
|
48.44361
|
0.33361
|
16.47185
|
|
-5.838E-13
|
6.003E-11
|
7.105E-15
|
9.668E+01
|
|
10
|
0.00010
|
48.44361
|
0.33357
|
16.47570
|
|
-6.939E-18
|
9.095E-12
|
-7.105E-15
|
1.329E+00
|
|
11
|
0.00010
|
48.44361
|
0.33357
|
16.47575
|
|
-5.204E-18
|
-2.728E-12
|
-7.105E-15
|
2.657E-04
|
|
12
|
0.00010
|
48.44361
|
0.33357
|
16.47575
|
|
0.000E+00
|
-1.819E-12
|
7.105E-15
|
-2.274E-11
|
|
13
|
0.00010
|
48.44361
|
0.33357
|
16.47575
|
|
-3.469E-18
|
-1.819E-12
|
0.000E+00
|
3.479E-11
|
|
14
|
0.00010
|
48.44361
|
0.33357
|
16.47575
|
|
-1.735E-18
|
-1.819E-12
|
0.000E+00
|
3.479E-11
|
|
15
|
0.00010
|
48.44361
|
0.33357
|
16.47575
|
|
-1.735E-18
|
-1.819E-12
|
0.000E+00
|
3.479E-11
|
As can be seen from the above table, the solution for this example problem has essentially converged after about 10 iterations with values of 0.00010 for E1, 48.44361 for E2, 0.33357 for E3, and 16.47575 for E4.
Plugging these values back into the original equations
0.005(100 – E1 - 2E2)(1.0 - E1 - E3) = 100E1
500(100 - E1 - 2E2)2 = 100E2
0.5(100 - E1 - E3 - 2.0 E4) = 100E3
10000(100E3 - 2E4)2 = 100E4
Gives
|
LHS
|
RHS
|
|
0.01037
|
0.01037
|
|
4844.361
|
4844.361
|
|
33.35741
|
33.35741
|
|
1647.575
|
1647.575
|