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







<< Previous Next >>

How to solve Simultaneous Equations With 2 Unknowns in JavaScript | Elimination Method Algorithm



Understanding Linear Algebra Systems | Maths Explanation for JavaScript Kids

Welcome to this beginner-friendly JavaScript tutorial designed for junior secondary students learning algebra. JavaScript is a great tool for solving math problems. It helps students visualize and automate algebraic solutions. This tutorial is perfect for:

  • Junior secondary students learning JavaScript
  • Teachers looking for math coding projects
  • Anyone curious about solving equations with code
In this lesson, you'll discover how to solve simultaneous equations with two unknowns using JavaScript. We'll walk through the elimination method and show you how to write a simple JavaScript script to solve linear systems.
The resulting algorithm solves the linear system using basic algebra and JavaScript logic. It's ideal for school projects and math exercises.
Well, it hasn't been much of algebra in our junior secondary school JavaScript tutorial series so far; We are about to have our first taste of it.


What Are Simultaneous Equations? | Maths Explanation for JavaScript Kids

Simultaneous equations are a set of equations with multiple variables that are solved together. For example:
                  2x + 3y = 13; and
                  5x - y = 7
Simultaneous equations can be solved using algebraic methods like Substitution or Elimination, or advanced methods like Matrix and Cramer's Rule. In this tutorial, we'll focus on the elimination method and implement it using JavaScript.


Step-by-Step Guide to Solve Simultaneous Equations with 2 Unknowns | 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.
                  2x + 3y = 13; and
                  5x - y = 7

Step 1:

Pick a variable to eliminate, either x or y.
Our code will always eliminate the y variable.

Step 2:

Multiply equation 1 by the coefficient of variable y in equation 2.
⇒         -1 X (2x + 3y = 13)
⇒         -2x - 3y = -13

Step 3:

Multiply equation 2 by the coefficient of variable y in equation 1.
⇒         3 X (5x - y = 7)
⇒         15x - 3y = 21

Step 4:

Subtract the new equations obtained from Steps 2 and 3.
         -2x - 3y = -13
    -     15x - 3y = 21
⇒         -17x = -34

Step 5:

Divide the R.H.S. from Step 4 by the coefficient of x to obtain x.
⇒         x = -34/-17 = 2;

Step 6:

Obtain y by solving for y from any of the original equations, using the found value of x.
         2x + 3y = 13
⇒         2(2) + 3y = 13;
⇒         3y = 13 - 4 = 9;
⇒         y = 9/3 = 3;


Create 2 new files; On Notepad++: File, New.
Call them Simultaneous2Unknown.html and Simultaneous2Unknown.js;
Type out the adjoining JavaScript code for solving simultaneous equations with 2 unknowns.


So! JavaScript Fun Practice Exercise - Simultaneous Equations with 2 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 2 by 2 Simultaneous Equations.









JavaScript Code for Simultaneous2Unknown.html

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

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

        <script>

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

            //Print as an equation
            result += x_2coeff[0] + "x &nbsp;&nbsp;" + operator[0] + "&nbsp;&nbsp; " +
                    Math.abs(y_2coeff[0]) + "y &nbsp;&nbsp;=&nbsp;&nbsp; " + eq_2coeff[0] + "<br/>";

            result += x_2coeff[1] + "x &nbsp;&nbsp;" + operator[1] + "&nbsp;&nbsp; " +
                    Math.abs(y_2coeff[1]) + "y &nbsp;&nbsp;=&nbsp;&nbsp; " + eq_2coeff[1] + "<br/>";
            result += "Yields: <br/>";
            result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            result += "(x, &nbsp;&nbsp;y) &nbsp;&nbsp;=&nbsp;&nbsp; ";

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

        </script>

    </body>
</html>


JavaScript Code for Simultaneous2Unknown.js


var x_2coeff = [23];
var y_2coeff = [1-1];
var eq_2coeff = [32];
var eliminator = [];
eliminator[0] = [];
eliminator[1] = [];
var x_variable;
var y_variable;

function solveSimultaneous2D() {
    // STEP 2:
    eliminator[0][0] = y_2coeff[1] * x_2coeff[0];
    eliminator[0][1] = y_2coeff[1] * eq_2coeff[0];
    // STEP 3:
    eliminator[1][0] = y_2coeff[0] * x_2coeff[1];
    eliminator[1][1] = y_2coeff[0] * eq_2coeff[1];

    try {
        // STEPS 4, 5:
        x_variable = (eliminator[0][1] - eliminator[1][1]) / 
                (eliminator[0][0] - eliminator[1][0]);
        // STEP 6:
        y_variable = (eq_2coeff[0] - x_2coeff[0]*x_variable) / y_2coeff[0];
    } catch (ex) {
        throw ex;
    }
}




<< Previous Next >>