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







<< Previous Next >>

Solving 3x3 Simultaneous Equations in JavaScript | Elimination Method Algorithm



Solving Simultaneous Equations in JavaScript: A Junior Secondary Guide

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

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


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

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


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

Let's try to draft a JavaScript 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 JavaScript 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 JavaScript 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 2 new files; On Notepad++: File, New.
Call them Simultaneous3Unknown.html and Simultaneous3Unknown.js;
Type out the adjoining JavaScript code for solving simultaneous equations with 3 unknowns.
.


Note: The code module for finding LCM in JavaScript has been explained in the Primary Category.


So! JavaScript 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 JavaScript code solves the resulting 3x3 Simultaneous Equations.







JavaScript Code for Simultaneous3Unknown.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Three Dimensional Simultaneous Equation</title>
        <script src="LCM.js"></script>
        <script src="Simultaneous2Unknown.js"></script>
        <script src="Simultaneous3Unknown.js"></script>
    </head>
    <body>

        <h3>3 Dimensional Simultaneous Equation</h3>
        <!-- This is where the result will be displayed when it is ready.-->
        <div id="three_dim"></div>

        <script>

            var operator = [];
            operator[0] = ['+', '+'];
            operator[1] = ['+', '+'];
            operator[2] = ['+', '+'];
            for (var i = 0; i < 3; i++) {
                if (y_3coeff[i] < 0) {
                    operator[i][0] = '-';
                }
                if (z_3coeff[i] < 0) {
                    operator[i][1] = '-';
                }
            }
            var result = "Solving simultaneously the equations:<br/>";

            //Print as an equation
            result += x_3coeff[0] + "x &nbsp;&nbsp;" + operator[0][0] + 
                    "&nbsp;&nbsp; " + Math.abs(y_3coeff[0]) + "y &nbsp;&nbsp;" + 
                    operator[0][1] + "&nbsp;&nbsp; " + Math.abs(z_3coeff[0]) + 
                    "z &nbsp;&nbsp;=&nbsp;&nbsp; " + eq_3coeff[0] + "<br/>";

            result += x_3coeff[1] + "x &nbsp;&nbsp;" + operator[1][0] + 
                    "&nbsp;&nbsp; " + Math.abs(y_3coeff[1]) + "y &nbsp;&nbsp;" + 
                    operator[1][1] + "&nbsp;&nbsp; " + Math.abs(z_3coeff[1]) + 
                    "z &nbsp;&nbsp;=&nbsp;&nbsp; " + eq_3coeff[1] + "<br/>";

            result += x_3coeff[2] + "x &nbsp;&nbsp;" + operator[2][0] + 
                    "&nbsp;&nbsp; " + Math.abs(y_3coeff[2]) + "y &nbsp;&nbsp;" + 
                    operator[2][1] + "&nbsp;&nbsp; " + Math.abs(z_3coeff[2]) + 
                    "z &nbsp;&nbsp;=&nbsp;&nbsp; " + eq_3coeff[2] + "<br/>";
            result += "Yields: <br/>";

            result += "(x, &nbsp;&nbsp;y, &nbsp;&nbsp;z) &nbsp;&nbsp;=&nbsp;&nbsp; ";

            try {
                // Solve simultaneously
                solveSimultaneous3D();
                result += "(" + x_variable + ", &nbsp;&nbsp;" + y_variable + 
                        ", &nbsp;&nbsp;" + z_variable + ")";
            } catch (ex) {
                result += "(&infin;&comma; &nbsp;&nbsp;&infin;&comma; &nbsp;&nbsp;&infin;)";
            }
            document.getElementById("three_dim").innerHTML = result;

        </script>

    </body>
</html>

JavaScript Code for Simultaneous3Unknown.js


var x_3coeff = [242];
var y_3coeff = [1-13];
var z_3coeff = [1-2-8];
var eq_3coeff = [41-3];
eliminator = [];
eliminator[0] = [];
eliminator[1] = [];
eliminator[2] = [];
var z_variable;

x_2coeff = [];
y_2coeff = [];
eq_2coeff = [];

function solveSimultaneous3D() {
    var lcm;
    // find the LCM z coefficients
    lcm = getLCM([Math.abs(z_3coeff[0])Math.abs(z_3coeff[1])Math.abs(z_3coeff[2])]);

    // STEP 1:
    eliminator[0][0] = (lcm * x_3coeff[0]) / z_3coeff[0];
    eliminator[0][1] = (lcm * y_3coeff[0]) / z_3coeff[0];
    eliminator[0][2] = (lcm * eq_3coeff[0]) / z_3coeff[0];

    eliminator[1][0] = (lcm * x_3coeff[1]) / z_3coeff[1];
    eliminator[1][1] = (lcm * y_3coeff[1]) / z_3coeff[1];
    eliminator[1][2] = (lcm * eq_3coeff[1]) / z_3coeff[1];

    eliminator[2][0] = (lcm * x_3coeff[2]) / z_3coeff[2];
    eliminator[2][1] = (lcm * y_3coeff[2]) / z_3coeff[2];
    eliminator[2][2] = (lcm * eq_3coeff[2]) / z_3coeff[2];

    // STEP 2:
    x_2coeff = [
        eliminator[0][0] - eliminator[1][0],
        eliminator[1][0] - eliminator[2][0]
    ];
    y_2coeff = [
        eliminator[0][1] - eliminator[1][1],
        eliminator[1][1] - eliminator[2][1]
    ];
    eq_2coeff = [
        eliminator[0][2] - eliminator[1][2],
        eliminator[1][2] - eliminator[2][2]
    ];

    try {
        // STEP 3:
        solveSimultaneous2D();
        // STEP 4:
        z_variable = (eq_3coeff[0] - x_3coeff[0] * x_variable -
                y_3coeff[0] * y_variable) / z_3coeff[0];
    } catch (ex) {
        throw ex;
    }
}




<< Previous Next >>