You're here: Home / Math /

Solving Simultaneous Equations Order 2

Let's derive the code to solve the simultaneous equations order 2 problem. For the equations,

We can obtain the expression of x by eliminating y. Do this by multiplying equation 1 with b2 and equation 2 with b1. So we got:

Substracting both equations, we'll have the expression for x:

And obtain the expression for y in similar way:

Writing the code is alot easier!

<script type="text/javascript">

/* solve simultaneous equations order 2 */
function simEquOrd2(eq1, eq2) {
    d0 = ( eq1[0] * eq2[1] - eq2[0] * eq1[1] );
    x  = ( eq1[1] * eq2[2] - eq2[1] * eq1[2] ) / d0;
    y  = ( eq2[0] * eq1[2] - eq1[0] * eq2[2] ) / d0;
    return( Array(x,y) );
}

/* let's test */
eq1 = [5, 2, 19];  /* 5x + 2y + 19 = 0 */
eq2 = [3, 4, 17];  /* 3x + 4y + 17 = 0 */
res = simEquOrd2(eq1, eq2);

alert("x=" + res[0] + ",y=" + res[1]); /* will print: x=-3,y=-2 */

</script>

Share:  del.icio.us logo Save to del.icio.us  digg logo Digg this!

comment.gifAdd your comment

(required, will not be published) (optional)