Solving simultaneous equations

Introduction
The word "simultaneous" implies that two or more equations exist in the same context at the same time.

For example:

$$3x + 2y - 13 = 0\ \dots\ (1)$$

$$2x - y - 4 = 0 \ \dots\ (2)$$

Equations $$(1)$$ and $$(2)$$ are 2 simultaneous equations.

When we see two simultaneous equations, it's natural to ask the question: What are the values of $$x$$ and $$y$$ that satisfy both equations $$(1)$$ and $$(2)$$?

To calculate $$(x,y)$$ common to both $$(1)$$ and $$(2)$$:

$$(2)*2\ \dots\ 4x - 2y - 8 = 0 \ \dots\ (3)$$

$$(3)+(1)\ \dots\ 7x - 21 = 0;\ x = 3.$$

Substitute $$3$$ for $$x$$ in $$(2):\ 2(3) - y - 4 = 0;\ y = 2.$$

The values $$(x,y)=(3,2)$$ satisfy both equations $$(1)$$ and $$(2).$$

In cartesian coordinate geometry of 2 dimensions, point $$(3,2)$$ is the point of intersection of two lines $$(1)$$ and $$(2).$$ See Figure 1 below.

If the 2 lines are parallel, there is no point of intersection. See Figure 2 below.

The trivial solution $$(0, 0)$$ is a valid solution. See Figure 3 below.

The information contained in equations $$(1)$$ and $$(2)$$ may constitute a matrix:

[ [3,  2, -13],  [2, -1,  -4] ]

In fact, in the python programming language, the following statement is valid code:

For there to be a point of intersection,  must be valid.

Here are examples of invalid data:

The matrix above with name "data" is described as matrix "2 by 3," meaning that it contains 2 rows with 3 columns or 3 members per row.

Solving 2 by 3
Below is python code containing function  The function accepts as input a matrix like above.

After the code are examples of invocation with both good data and bad data. Invocation with good data:

(3.0, 2.0)

Invocation with bad or invalid data:

solve2by3 : parallel input. input = [3, 2, -13]   [6, 4, -5] None

solve2by3 : error detected in processing. (, ValueError('too many values to unpack (expected 3)',)) input = [3, 2, -13, 7]   [6, 4, -5] None

solve2by3 : empty column in input. input = [3, 0, 2]   [6, 0, 4] None

Linear Function
Examples above calculate the point of intersection of 2 lines.

An example of reversing the process is this task:

Given two points $$(x_1, y_1), (x_2, y_2)$$ calculate equation of line containing these 2 points.

Equation of line is of format $$Ax + By + C = 0,$$

For example: $$3x + 2y + 10 = 0\ \dots\ (1).$$

Equation $$(1)$$ can be expressed as $$6x + 4y + 20 = 0$$ or $$-9x - 6y - 30 = 0.$$

Also, there appear to be 3 unknowns, but only two sets of values are required to define the line.

Assume $$C = 1$$ and the 2 relevant equations are:

$$Ax_1 + By_1 + 1 = 0$$

$$Ax_2 + By_2 + 1 = 0$$

To solve for $$A, B:$$

$$x_1A + y_1B + 1 = 0$$

$$x_2A + y_2B + 1 = 0$$

Suppose the line is defined by points $$(3,1), (-5,4):$$ Desired equation is: $$-0.17647058823529413\cdot x -0.47058823529411764\cdot y + 1 = 0\ \dots\ (2).$$

To improve appearance of $$(2):$$ Let $$A, B = 3, 8$$ and check: Desired equation $$(2)$$ becomes $$3x + 8y - 17 = 0\ \dots\ (3).$$

Wrong assumptions
Sometimes you create a matrix that seems correct, but it fails to produce a result. Here is such an example:

What is equation of line defined by points $$(3, 2), (6, 4)?$$

As above assume that $$C = 1.$$ This assumption failed: Assume that $$A = 1.$$ Equation of line is: $$1\cdot x + (-1.5)\cdot y + 0 = 0$$ or $$2x - 3y = 0$$

This line includes the origin. Therefore constant $$C$$ cannot be non-zero.

Generally:


 * Without rotation, $$A, B = \text{result}$$ and $$C = 1.$$


 * With rotation left by one column, $$B, C = \text{result}$$ and $$A = 1.$$

In this case:

For example of big matrix that failed the first time, but was successful the second time, see cubic resistive network below.

Solving 3 by 4
Below is python code containing function  The function accepts as input a 3 by 4 matrix like that described in the code comments.

In 3d coordinate geometry the equations  are the equations of 3 planes and the solution of the system is the point of intersection of the 3 planes.

Given 3 random planes, there are 2 conditions that do not produce a point of intersection:


 * If 2 of the planes are parallel. See Figure 1 below.

The trivial solution $$(0, 0, 0)$$ is valid. See Figure 6 below.
 * If the 3 planes form a "tent" or "delta." See Figure 2, 3 or 4 below. A special case of this condition occurs when size of "tent" is zero, in which case all three planes intersect on a common line and there is an infinite number of solutions. See Figure 5 below.

Below are examples of invocation with both good data and bad data.

Invocation with good data:

(3.0, 4.0, 5.0)

(2.0, 0.0, -0.0)

Invocation with bad or invalid data:

solve2by3 : parallel input. input = (-692, 346, -829)   (396, -198, 517) checkDirectionNumbers : 3 rows form a tent. input of solve3by4 = [20, 16, 2, 17]   [-77, -27, -25, -24]    [81, 45, 18, 43] None

solve2by3 : empty column in input. input = (-16, 0, -176)   (-1176, 0, -1104) checkDirectionNumbers : 3 rows form a tent. input of solve3by4 = [-34, -6, 34, -32]   [82, 14, -82, 72]    [-59, -45, 59, -88] None

solve2by3 : parallel input. input = (1392, -1008, -3728)   (-464, 336, -752) checkDirectionNumbers : 2 rows parallel, 2 and 3. input of solve3by4 = [34, -5, -21, 30]   [-54, -33, 63, 62]    [18, 11, -21, 38] None

solve3by4 : error detected in processing. (, TypeError("unsupported operand type(s) for -: 'int' and 'str'",)) input of solve3by4 = [20, 16, 2, 17]   [-77, -27, '-25', -24]    [81, 45, 18, 43] None

Quadratic function
A quadratic function $$y = f(x) = Ax^2 + Bx + C$$ is defined by 3 points: $$(-2, 9.0), (5, 4.8), (10, 13.8).$$

What is $$f(x)?$$

$$y = Ax^2 + Bx + C$$

$$A, B, C$$ are 3 unknowns.

$$x^2(A) + x(B) + 1(C) - y = 0$$ $$y = f(x) = 0.2x^2 - 1.2x + 5.8$$

Wrong assumptions
In 3 dimensions a plane ($$Ax + By + Cz + D = 0$$) is defined by 3 points: $$(5, 3, -2), (-1, 12, -8), (37, 15, -10).$$

What is equation of plane?

$$Ax + By + Cz + D = 0$$

$$xA + yB + zC + D = 0$$

Assume that $$D = 1.$$

$$xA + yB + zC + 1 = 0$$

Input was invalid. Rotate input left and try again. Input was invalid. Rotate input left and try again. Without rotation: $$A, B, C = \text{result}$$ and $$D = 1.$$

With rotation left: $$B, C, D = \text{result}$$ and $$A = 1.$$

With another rotation left: $$C, D, A = \text{result}$$ and $$B = 1.$$ Equation of plane is: $$0x + 1y + 1.5z + 0 = 0$$ or $$2y + 3z = 0$$

Solving M by (M+1)
Below is python code containing function  The function accepts as input a 4 by 5 matrix (or greater) like that described in the code comments.

This function is recursive, calling itself with M being reduced by 1 with each recursive invocation until the matrix is size 3 by 4 in which case function solve3by4 is called.

Below are examples of invocation with both good data and bad data.

Invocation with good data:

The following results were produced with decimal precision set at 50: ( Decimal('-0.06331410760212760088313271882866553044211194694576'), Decimal('1.0969089506763419155148060427588009931309074487534'), Decimal('1.0837038580207682607126722016017867670831200359767'), Decimal('-0.42989566124678842791674322699930461340596671792215'), Decimal('-2.0541601084281682582146753421426610779297051535362') )

Invocation with bad or invalid data:

solve2by3 : parallel input. input = (-181440, 181440, -101088)   (90720, -90720, 406944) checkDirectionNumbers : 3 rows form a tent. input of solve3by4 = [-756, -1176, 0, -1872]   [-324, -744, 240, -936]    [-324, -384, -120, -264]  input of solveMbyN with 4 rows = [-6, 11, 16, -4, 20]   [-66, -5, -20, -44, -92]    [-42, 23, -12, 12, -16]    [-18, -21, -16, -32, 16]  input of solveMbyN with 5 rows = [-4, -6, 3, -4, -8, -8]   [1, 0, 2, 5, 1, 7]    [9, -3, -8, 4, 7, -5]    [1, -9, 5, -2, 5, -2]    [1, -3, -6, -3, -6, 6] None Provided that numbers are small, integers, floats and Decimal objects produce the same results:
 * 1) With each step towards solve2by3 the length of each number in digits can theoretically double.

solve2by3 : parallel input. input = (-181440.0, 181440.0, -101088.0)   (90720.0, -90720.0, 406944.0) checkDirectionNumbers : 3 rows form a tent. input of solve3by4 = [-756.0, -1176.0, 0.0, -1872.0]   [-324.0, -744.0, 240.0, -936.0]    [-324.0, -384.0, -120.0, -264.0]  input of solveMbyN with 4 rows = [-6.0, 11.0, 16.0, -4.0, 20.0]   [-66.0, -5.0, -20.0, -44.0, -92.0]    [-42.0, 23.0, -12.0, 12.0, -16.0]    [-18.0, -21.0, -16.0, -32.0, 16.0]  input of solveMbyN with 5 rows = [-4.0, -6.0, 3.0, -4.0, -8.0, -8.0]   [1.0, 0.0, 2.0, 5.0, 1.0, 7.0]    [9.0, -3.0, -8.0, 4.0, 7.0, -5.0]    [1.0, -9.0, 5.0, -2.0, 5.0, -2.0]    [1.0, -3.0, -6.0, -3.0, -6.0, 6.0] None

solve2by3 : parallel input. input = (Decimal('-181440'), Decimal('181440'), Decimal('-101088')) (Decimal('90720'), Decimal('-90720'), Decimal('406944')) checkDirectionNumbers : 3 rows form a tent. input of solve3by4 = [Decimal('-756'), Decimal('-1176'), Decimal('0'), Decimal('-1872')] [Decimal('-324'), Decimal('-744'), Decimal('240'), Decimal('-936')] [Decimal('-324'), Decimal('-384'), Decimal('-120'), Decimal('-264')] input of solveMbyN with 4 rows = [Decimal('-6'), Decimal('11'), Decimal('16'), Decimal('-4'), Decimal('20')] [Decimal('-66'), Decimal('-5'), Decimal('-20'), Decimal('-44'), Decimal('-92')] [Decimal('-42'), Decimal('23'), Decimal('-12'), Decimal('12'), Decimal('-16')] [Decimal('-18'), Decimal('-21'), Decimal('-16'), Decimal('-32'), Decimal('16')] input of solveMbyN with 5 rows = [Decimal('-4'), Decimal('-6'), Decimal('3'), Decimal('-4'), Decimal('-8'), Decimal('-8')] [Decimal('1'), Decimal('0'), Decimal('2'), Decimal('5'), Decimal('1'), Decimal('7')] [Decimal('9'), Decimal('-3'), Decimal('-8'), Decimal('4'), Decimal('7'), Decimal('-5')] [Decimal('1'), Decimal('-9'), Decimal('5'), Decimal('-2'), Decimal('5'), Decimal('-2')] [Decimal('1'), Decimal('-3'), Decimal('-6'), Decimal('-3'), Decimal('-6'), Decimal('6')] None

Sphere
In cartesian coordinate geometry of three dimensions a sphere is represented by equation:

$$x^2 + y^2 + z^2 + Ax + By + Cz + D = 0.$$

On the surface of a certain sphere there are 4 known points:

What is equation of sphere?

Rearrange equation of sphere to prepare for creation of input matrix:

$$(x)A + (y)B + (z)C + (1)D + (x^2 + y^2 + z^2) = 0.$$

Create input matrix of size 4 by 5:

Equation of sphere is :

$$x^2 + y^2 + z^2 - 8x + 10y - 24z - 104 = 0$$

Quintic function
A quintic function $$y = f(x) = Ax^5 + Bx^4 + Cx^3 + Dx^2 + Ex + F$$ is defined by 6 points: $$(-8, 0), (-6, 0), (-3, 0), (1,0), (3,0), (5,10).$$

What is $$f(x)?$$

$$y = f(x) = Ax^5 + Bx^4 + Cx^3 + Dx^2 + Ex + F$$

$$A, B, C, D, E, F$$ are 6 unknowns.

$$x^5(A) + x^4(B) + x^3(C) + x^2(D) + x(E) + 1(F) - y = 0$$ From these results the following ratios may be deduced:

$$\frac{1}{A} = 915.2;\ $$ $$\frac{B}{A} = 13;\ $$ $$\frac{C}{A} = 25;\ $$ $$\frac{D}{A} = -165;\ $$ $$\frac{E}{A} = -306;\ $$ $$\frac{F}{A} = 432.$$

$$y = f(x) = \frac{x^5 + 13x^4 + 25x^3 - 165x^2 - 306x + 432}{915.2}$$

Resistive network
A resistive network containing 8 resistors is connected as shown in Figure 1.

What is resistance $$R$$ between points $$P_1, P_3?$$

Solve for 8 Branch Currents
Voltage across $$r_1:$$

Using $$E = IR,\ e_1 = i_1\cdot r_1 = r_1\cdot a.$$

Similarly:

$$e_2 = r_2\cdot b.$$

$$e_3 = r_3\cdot c.$$

$$\dots\dots$$

$$e_8 = r_8\cdot h.$$

Build a matrix size 8 by 9 which will be input to function solveMbyN.

This last entry can contain almost any arbitrary values. For example: Changing this last entry changes all values of $$e_x, i_x.$$ However, the ratio $$\frac{e_x}{i_x} = r_x$$ remains constant.

input is matrix of size 8 by 9:

$$i_7$$ has negative value. Reverse $$i_7$$ to make all branch currents positive.

Make all Branch Currents positive
Make $$i_7$$ positive and check results.

Compute R
$$R = R_1 = R_2$$$$$$$$$$$$$$

Solving big matrices
A big matrix is one of size greater than 10 by 11.

With each recursive call of solveMbyN the size of each value in decimal digits can theoretically double. Python's integer math enjoys infinite precision. If your original matrix is of size 32 by 33, and each value in the matrix is an int of 1 decimal digit, function solve2by3 can be called with ints of size 2**30 (1,073,741,824) decimal digits.

Floats can be used instead of ints, but the limited precision of floats may not provide a desirable level of accuracy in the result.

Python's decimal objects are a good compromise and function reduceRow above is an attempt to keep the size of each member manageable.

Function reduceRow works well. However, as with all operations involving python's decimal objects, you need to pay careful attention to precision and accuracy of the results.

On my Mac the following python code generates a matrix of size 100 by 101 and calculates the result containing 100 values in about 1.5 seconds, including the time to generate 10,100 random numbers, each containing up to 12 decimal digits with the decimal point in a random position, and including the time to test output over 100 rows to verify that all calculated values of the solution satisfy each row.

In function solveMbyN above find line: Change it to: Then: A small sample of the random numbers in data: +ve values: 5021, [Decimal('1.16871265E-7'), Decimal('1.73721245E-7')] [Decimal('998523464710'), Decimal('999768605679')] -ve values: 5079, [Decimal('-991079944209'), Decimal('-989786083110')] [Decimal('-4.10272824E-7'), Decimal('-2.38664537E-7')] A small sample of the solutions: output[0] = 0.43689400018920695253771832432651757885163362928745 output[19] = 0.19867480862268284151047885891372314311902973127033 output[59] = 0.46169681824826309421940885550155275946668017604646 output[99] = -0.020598220802233733529683040085023443421113627790464

Testing results
Data input to function solveMbyN is an array of 100 rows, each row containing 101 numbers. [ [v0_0, v0_1, v0_2, ..... v0_98,v0_99,v0_100], ....... [v99_0,v99_1,v99_2, ...... v99_98,v99_99,v99_100],] ] Data received from function solveMbyN is an array of 100 numbers. [ s0,s1,s2, ...... s98,s99 ] The first row is tested with the solutions by; L2 = [ v0_0*s0, v0_1*s1, v0_2*s2, ..... v0_98*s98,v0_99*s99,v0_100 ] sum is the sum of all items in L2.

sum is appended to L1 and the process is repeated for all 100 rows.

Ideally sum should be zero.

In practice it's extremely unlikely that sum will be zero. My results typically gave values close to 100 4.1065149609E-38 9.6425614873452398368906E-25 The worst of the results above was 23 zeroes after the decimal point.

Better results can be achieved by increasing precision.

Cubic Resistive Network
A resistive network containing 12 resistors is connected as shown in Figure 1.

The network is called "cubic" because, in 3 dimensions, each resistor is located on one edge of a cube.

What is resistance $$R$$ between points $$P_1, P_2?$$

Create matrix 12 by 13
This matrix failed:

Compute R
$$e_3$$ should be $$48,\ i_3*r_3$$ or $$1.6*30.$$ Display results with precision of 28: $$R = R_1 = R_2.$$

Review
In matrix all values $$i_1, i_2, \dots, i_{11}, i_{12}$$ and $$e_1, e_2, \dots, e_{11}, e_{12}$$ are included.

Testing was done with the following lines substituted for last line of matrix: All tests produced a consistent value of $$R.$$

Python code
Result of each of the above calculations should be $$0.$$

Because of small rounding errors, result may not be exactly $$0.$$

See values_of_i and values_of_e above.

This code verifies that result is close enough to $$0$$ to be acceptable.

For example:

$$\text{result} = e_{10} + e_2 - e_{12} - e_8 + e_{11} - e_3 - e_9 + e_5.$$

$$\text{v1} = e_{10} + e_2 + e_{11} + e_5,$$ sum of positive values.

$$\text{status = (abs(result)} < \text{(v1}*\text{Tolerance)). status}$$ should be $$\text{True.}$$

Big Matrix 64 by 65
A network containing 64 resistors is connected as shown in diagram.

What is resistance R between points P1, P2?

While this resistive circuit may be very theoretical, it lends itself well to the creation of a big matrix, 64 by 65, meaning that there are 64 resistors and 64 simultaneous equations to be solved.

Create matrix
An examination of the circuit produces the following conditions:

In iconditions and econditions there are 63 conditions that include all values of i and e. These 63 conditions are used to create matrix.

The 64th condition is added manually.

The matrix is: Bottom line of matrix is equivalent to: $$(1)i10 + (-1) = 0$$ or $$i10 = 1.$$

First result
The following conditions are included for testing: Produce results: $$\text{R}$$ is printed with precision of 60.

Matrix corrected
Original assumptions were that currrent flow through 64 resistors would be from bottom to top or from left to right.

Examination of results shows that flow through R70, R73 is from right to left. This is reflected in testing below.

Corrected diagram conforms to assumption that all current flow is from bottom towards top or from left to right.

$$\text{R}$$ is printed with precision of 56.

With precision of 56 all 64 results are equal.