Solve 3 equations with 3 variables

Hello Processing foundation

image

I have these 3 equations with 3 variables (a,b,c) that i want to solve using processing, but unfortunaly I do not have a clue how to do this. Therefore, I really hope that someone could help me with this:)

Mikkel

Hello,

For starters:


That should get you started.

:)

Welcome! I just taught a unit on linear systems in Algebra 2, and we happened to use p5.js to explore the topic.

Your three equations describe three planes that might intersect at a point (a, b, c) . Gil Strang does a wonderful job presenting the idea in the first lecture of his linear algebra course. One algorithm to find the right (a, b, c) is Gaussian elimination, covered in the third lecture.

Let’s start with a simpler case.

1a + 2b = 3
4a + 5b = 6

This system describes two lines that might intersect at the point (a, b) . In other words, we want to find the following.

a \mspace{5mu} \qquad = \_
\mspace{5mu} \qquad b \mspace{1mu} = \_

So how do we get there? Matrices are workhorses for many things, and their row operations are perfectly suited to transform the original system into its solution.

We could rewrite the original system as a matrix filled with the multipliers (coefficients) like so.

\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}

The goal then becomes to transform this matrix into its reduced row echelon form (RREF).

\begin{bmatrix} 1 & 0 & \_ \\ 0 & 1 & \_ \end{bmatrix}

The elements in the last column correspond to the values of a and b where the lines (maybe) intersect. We’ll get there by steadily transforming each column in the original matrix to match its RREF.

Step 1: Subtract 4 multiples of the first row from the second row. R2 - 4R1 \rightarrow R2

\begin{bmatrix} 1 & 2 & 3 \\ 0 & -3 & -6 \end{bmatrix}

One column down, one to go!

Step 2: Multiply the second row by -1/3 . -\frac{1}{3}R2 \rightarrow R2

\begin{bmatrix} 1 & 2 & 3 \\ 0 & 1 & 2 \end{bmatrix}

Step 3: Subtract 2 multiples of the second row from the first row. R1 - 2R2 \rightarrow R1

\begin{bmatrix} 1 & 0 & -1 \\ 0 & 1 & 2 \end{bmatrix}

Now you can imagine casting this matrix back into a system of equations.

a \mspace{5mu} \qquad = -1
\mspace{5mu} \qquad b \mspace{1mu} = 2

Here is a p5.js sketch that runs elimination manually using a math library I wrote with @ashneel.das.

let INDEX_MODE = 1;

function setup() {
  
  let matrix = createTensor([[1, 2, 3],
                             [4, 5, 6]]);
  
  print('Original Matrix');
  matrix.print();
  
  print('R2 - R1 * 4 -> R2')
  matrix = matrix.subRows(2, 1, 4);
  matrix.print();
  
  print('R2 * -1/3 -> R2');
  matrix = matrix.mulRow(2, -1/3);
  matrix.print();
  
  print('R1 - R2 * 2 -> R1');
  matrix = matrix.subRows(1, 2, 2);
  matrix.print();
}

It might be interesting to try your 3 \times 4 example manually, then automate the process. This could give you an idea of what you’d like to see in your own Processing implementation.

1 Like

One way to solve simultaneous equations is to use matrices. The sketch below demonstrates how to use the EJML library, you can download the sketch here, it includes the necessary EMJL jar files. Simply download the zip file and extract the sketch into your Processing sketchbook folder.

import org.ejml.simple.SimpleMatrix;

/**
 * Demonstrates how to solve a system of simultaneous 
 * equations using the EJML (www.emjl.org)
 * The three equations to solve are
 * 3x + y + z = 16
 * x + 2y + 9z = -7
 * 2x + 5y - z = 27
 * 
 * The correct solution is 
 * x = 5 ; y = 3 ; z = -2
 * 
 * Note the library use the double data type internally so we
 * pass the coefficients in arrays of type double.
 */
double[][] lhs = {
  { 3, 1, 1 }, 
  { 1, 2, 9 }, 
  { 2, 5, -1}
};
double [] rhs = { 16, -7, 27 };
float x, y, z;

public void setup() {
  // Solve the equations
  SimpleMatrix a = new SimpleMatrix(lhs);
  SimpleMatrix b = new SimpleMatrix(3, 1, true, rhs);
  a = a.invert().mult(b);
  x = (float) a.get(0);
  y = (float) a.get(1);
  z = (float) a.get(2);
  // Solution :-)
  println("Solution");
  println("x = " + x);
  println("y = " + y);
  println("z = " + z);
}
1 Like

If you are not familiar with Matrix, and just discovering linear algebra, you can solve that the “old way”

if you don’t want to solve for the general case, it’s not rocket science to find a, b and c manually in your example.

if you look at the eq3 and write c as a function of a and b, you get:
c = 167.1/5 - 33a - 5b

replace c in eq2, and you get only a and b in eq2

1225a + 165b + 25(167.1/5 - 33a - 5b) = 809.7
<=>
1225a + 165b + 835,5 - 825a - 125b = 809.7
<=>
400a + 40b = -25.8
<=>
write b as a function of a
b = -25.8/40 - 10a

As we know that c = 167.1/5 - 33a - 5b , if you replace b in that equation:
you have c = 167.1/5 - 33a - 5(-25.8/40 - 10a)

==> that means you now have b and c only as a function of a

take the first equation, replace b and c by their equivalent with a and you’ll have an equation with just a and constants. it should be trivial for you to find the value of a as a constant.

Then as you know b and c as a function of a, you just inject the value of a you found and here you go, you have a,b and c.

Now you get the idea on how those are solved.

To solve for the general case, assume the coefficients for a, b and c in the three equations are some constants that you don’t know.

eq1: C11 a + C12 b + C13 c = C14
eq2: C21 a + C22 b + C23 c = C24
eq3: C31 a + C32 b + C33 c = C34

where Cxy are those constants.

You solve it in the same way, take an equation where the c coefficient is not null and write c as a function of a and b and some constants.

replace in the next equation and find b as a function of a.
And replace in the last equation to write a as a function of the constants.
then replace a by its value in the b = equation and then a and b in the c = equation.

That will introduce along the way some divisions by the constants, so your program should test if these division are 0, in which case it means there are none or an infinite number of solutions to your 3 equations.

To introduce matrix, you can have a look at Gaussian elimination