Hi..
d1 = a1x1 + b1x2 + c1x3
d2 = a2
x1 + b2x2 + c2x3
d3 = a3x1 + b3x2 + c3*x3

Knowing all of a,b,c,d, I have to find x1, x2, x3. As you might noticed, this is high school math. But, how do I write the code to solve this?

/*
/a1 b1 c1| /x1| /d1|
|a2 b2 c2|*|x2|=|d2|
|a3 b3 c3/ |x3/ |d3|
*/
double A[3][3], X[3], D[3];
X[0] = ?
X[1] = ?
X[2] = ?

Can anyone give me an idea please?

Edited 3 Years Ago by pritaeas: Moved.

public static function solveMatrix(a:Array, b:Array, X:Array):void
        {
            var invA:Array = [];
            invA[0] = [];
            invA[1] = [];
            invA[2] = [];

             var invDet:Number = 1/((a[0][0]*(a[2][2]*a[1][1]-a[2][1]*a[1][2]))-(a[1][0]*(a[2][2]*a[0][1]-a[2][1]*a[0][2]))+(a[2][0]*(a[1][2]*a[0][1]-a[1][1]*a[0][2])));
            invA[0][0] = invDet * (a[2][2]*a[1][1]-a[2][1]*a[1][2]);
            invA[0][1] = invDet * (-1 * (a[2][2]*a[0][1]-a[2][1]*a[0][2]));
            invA[0][2] = invDet * (a[1][2]*a[0][1]-a[1][1]*a[0][2]);

            invA[1][0] = invDet * (-1 * (a[2][2]*a[1][0]-a[2][0]*a[1][2]));
            invA[1][1] = invDet * (a[2][2]*a[0][0]-a[2][0]*a[0][2]);
            invA[1][2] = invDet * (-1 * (a[1][2]*a[0][0]-a[1][0]*a[0][2]));

            invA[2][0] = invDet * (a[2][1]*a[1][0]-a[2][0]*a[1][1]);
            invA[2][1] = invDet * (-1 * (a[2][1]*a[0][0]-a[2][0]*a[0][1]));
            invA[2][2] = invDet * (a[1][1]*a[0][0]-a[1][0]*a[0][1]);

            X[0] = invA[0][0] * b[0] + invA[0][1] * b[1] + invA[0][2] * b[2];
            X[1] = invA[1][0] * b[0] + invA[1][1] * b[1] + invA[1][2] * b[2];
            X[2] = invA[2][0] * b[0] + invA[2][1] * b[1] + invA[2][2] * b[2];
        }

You are doing this in high school?! It must be a pretty advanced high school. I don't recall doing anything like this before first year university.

In any case, normally, to solve N Equations in N Unknowns, [A](x) = (b), the easiest way is to make the [A] matrix triangular, and then use back-substitution. However, I doubt you want anything that complicated.

Are you only going to be working with systems of size 3? Are you supposed to solve the system using determinants? It looks like you are off to a good start: computing the determinant by the standard formula.

Are you unclear about the algorithm? Or the coding?

Edited 3 Years Ago by DavidB

This article has been dead for over six months. Start a new discussion instead.