3 by 3 Simultaneous Equations Code in Java
To solve 3 by 3 simultaneous equations, we will simply eliminate the z variable, then call out to our simultaneous2unknown code module.
To solve 3 by 3 simultaneous equations, we will simply eliminate the z variable, then call out to our simultaneous2unknown code module.
Consider the equations:
x + 2y - z = 2; and
3x - y + 2z = 4
2x + 3y + 4z = 9
Multiply equations 1, 2 & 3 by the LCM of the coefficients
of variable z, divided by the z coefficient
of the respective equation.
(4/-1) X (x + 2y - z = 2)
⇒
-4x - 8y + 4z = -8
(4/2) X (3x - y + 2z = 4)
⇒
6x - 2y + 4z = 8
(4/4) X (2x + 3y + 4z = 9)
⇒
2x + 3y + 4z = 9
Subtract the new equations obtained in Step 2;
eqn (2) from eqn (1) and eqn (3) from eqn (2).
-4x - 8y + 4z = -8
-
6x - 2y + 4z = 8
⇒
-10x - 6y = -16
6x - 2y + 4z = 8
-
2x + 3y + 4z = 9
⇒
4x - 5y = -1
Call out to our simultaneous2unknown
code module to solve for x and y.
⇒
(x, y) = (1, 1);
Obtain z by solving for z from any of the
original equations, using the found values of x
and y.
x + 2y - z = 2
⇒
1 + 2(1) - z = 2;
⇒
-z = 2 - 3 = -1;
⇒
z = -1/-1 = 1;
Create a new class file;
call it Simultaneous3Unknown.
Type out the adjoining Java code for solving simultaneous equations with 3 unknowns.
Note: The code module for
finding LCM
has been explained in the Primary Category.
You can comment out the Simultaneous2Unknown Java object
code in the main class from the previous lesson or simply continue from where it stopped.
Feel free to try out your own set of x_coefficients, y_coefficients, z_coefficients and equals values for 3 by 3 Simultaneous Equations.