usingMaths.com
From Theory to Practice - Math You Can Use.







<< Previous Next >>

Solving 3x3 Simultaneous Equations in Java | Elimination Method Algorithm



Solving Simultaneous Equations in Java: A Junior Secondary Guide

Welcome to this junior secondary Java math project! In this tutorial, you'll learn how to solve simultaneous equations with three unknowns using Java. This is a great way to combine your coding skills with algebra and logic.
You'll also learn the following:

  • How to use Java to solve 3x3 simultaneous equations
  • Applying the elimination method step-by-step
  • Using LCM (Least Common Multiple) to simplify equations
  • Writing a Java class to automate the solving process
Solving equations is a key part of algebra. By coding the solution in Java, you'll not only understand the math better; you'll also build a useful tool. This project is perfect for students looking to explore Java math algorithms, or teachers seeking Java algebra exercises for the classroom.


How to Solve Three-Variable Algebra Problems | Maths Explanation for Java Kids

To solve 3 by 3 simultaneous equations, we will simply eliminate the z variable, then call out to our Java Code for Simultaneous Equations with 2 Unknowns module.



Step-by-Step Guide to Solve Three-Variable Algebra Equations | Elimination Method Java Algorithm

Let's try to draft a Java algorithm that solves simultaneous equations with 2 unknowns, using the elimination method, with the following set of equations in consideration.
         x + 2y - z = 2; and
         3x - y + 2z = 4
         2x + 3y + 4z = 9
These steps will help the student understand both the math and the logic behind the code.

Step 1:

Using the Find LCM in Java class from the Primary Category, find the LCM of the coefficients of variable z. 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

Step 2:

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

Step 3:

Call out to our Java Code for Simultaneous Equations with 2 Unknowns module to solve for x and y.
⇒         (x, y) = (1, 1);

Step 4:

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 Java 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 in Java 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.


So! Java Fun Practice Exercise - Simultaneous Equations with 3 Unknowns

As a fun practice exercise, feel free to try out your own set of x_coefficients, y_coefficients and equals values, and see how the Java code solves the resulting 3x3 Simultaneous Equations.







Java Code for Solving Simultaneous Equations with 3 Unknowns - Class File

package algebra;

import java.util.Arrays;
import java.util.List;

public class Simultaneous3Unknown {

    private final int[] x_coefficients;
    private final int[] y_coefficients;
    private final int[] z_coefficients;
    private final int[] equals;
    private final double[][] eliminator;
    private double x_variable;
    private double y_variable;
    private double z_variable;

    public Simultaneous3Unknown(int[] x_coeff, int[] y_coeff, int[] z_coeff, int[] eq) {
        x_coefficients = new int[]{x_coeff[0], x_coeff[1], x_coeff[2]};
        y_coefficients = new int[]{y_coeff[0], y_coeff[1], y_coeff[2]};
        z_coefficients = new int[]{z_coeff[0], z_coeff[1], z_coeff[2]};
        equals = new int[]{eq[0], eq[1], eq[2]};
        eliminator = new double[3][3];
    }

    public double[] solveSimultaneous() {
        List<Integer> stooge;
        stooge = Arrays.asList(
                Math.abs(z_coefficients[0]),
                Math.abs(z_coefficients[1]),
                Math.abs(z_coefficients[2])
        );

        LCM l_c_m = new LCM(stooge);
        int lcm = l_c_m.getLCM();

        // STEP 1:
        eliminator[0][0] = (lcm * x_coefficients[0]) / z_coefficients[0];
        eliminator[0][1] = (lcm * y_coefficients[0]) / z_coefficients[0];
        eliminator[0][2] = (lcm * equals[0]) / z_coefficients[0];

        eliminator[1][0] = (lcm * x_coefficients[1]) / z_coefficients[1];
        eliminator[1][1] = (lcm * y_coefficients[1]) / z_coefficients[1];
        eliminator[1][2] = (lcm * equals[1]) / z_coefficients[1];

        eliminator[2][0] = (lcm * x_coefficients[2]) / z_coefficients[2];
        eliminator[2][1] = (lcm * y_coefficients[2]) / z_coefficients[2];
        eliminator[2][2] = (lcm * equals[2]) / z_coefficients[2];

        // STEP 2:
        double[] new_x = {
            eliminator[0][0] - eliminator[1][0],
            eliminator[1][0] - eliminator[2][0]
        };
        double[] new_y = {
            eliminator[0][1] - eliminator[1][1],
            eliminator[1][1] - eliminator[2][1]
        };
        double[] new_eq = {
            eliminator[0][2] - eliminator[1][2],
            eliminator[1][2] - eliminator[2][2]
        };

        try {
            // STEP 3:
            double[] partial_solution;
            partial_solution = (new Simultaneous2Unknown(new_x, new_y, new_eq)).solveSimultaneous();

            x_variable = partial_solution[0];
            y_variable = partial_solution[1];
            // STEP 4:
            z_variable = (double)(equals[0] - x_coefficients[0] * x_variable - y_coefficients[0] * y_variable) / z_coefficients[0];
        } catch (ArithmeticException e) {
            throw e;
        }
        return new double[]{x_variabley_variablez_variable};
    }
}

Java Code for Solving Simultaneous Equations with 3 Unknowns - Main Class

package algebra;

import java.util.Arrays;

public class Algebra {

    public static void main(String[] args) {

        System.out.println("Welcome to our demonstration sequels");
        System.out.println("Hope you enjoy (and follow) the lessons.");
        System.out.println("\n");

        int[] x_coeff;
        int[] y_coeff;
        int[] z_coeff;
        int[] equals;
        
        /*
        * Simultaneous Equations with 3 unknowns
         */

        char[][] operators = new char[3][2];
        for (char[] op : operators) {
            Arrays.fill(op, '+');
        }

        double[] result3D;
        x_coeff = new int[]{2, 4, 2};
        y_coeff = new int[]{1, -1, 3};
        z_coeff = new int[]{1, -2, -8};
        equals = new int[]{4, 1, -3};

        for (int i = 0; i < 3; i++) {
            if (y_coeff[i] < 0) {
                operators[i][0] = '-';
            }
            if (z_coeff[i] < 0) {
                operators[i][1] = '-';
            }
        }

        System.out.println("Solving simultaneously the equations:");
        //Print as an equation
        System.out.printf(
                "%40dx %s %dy %s %dz = %d%n", x_coeff[0], operators[0][0],
                Math.abs(y_coeff[0]), operators[0][1], Math.abs(z_coeff[0]), equals[0]
        );
        System.out.printf(
                "%40dx %s %dy %s %dz = %d%n", x_coeff[1], operators[1][0],
                Math.abs(y_coeff[1]), operators[1][1], Math.abs(z_coeff[1]), equals[1]
        );
        System.out.printf(
                "%40dx %s %dy %s %dz = %d%n", x_coeff[2], operators[2][0],
                Math.abs(y_coeff[2]), operators[2][1], Math.abs(z_coeff[2]), equals[2]
        );
        System.out.printf("%n%30s%n%40s""Yields:""(x, y, z)  =  ");

        try {
            Simultaneous3Unknown sim3unk;
            sim3unk = new Simultaneous3Unknown(x_coeff, y_coeff, z_coeff, equals);
            result3D = sim3unk.solveSimultaneous();

            System.out.printf("(%.4f, %.4f, %.4f)%n", result3D[0], result3D[1], result3D[2]);

        } catch (ArithmeticException e) {
            System.out.printf("(%s, %s, %s)%n""∞""∞""∞");
        }

    }
}



<< Previous Next >>