// matrix A let matrixA = [ [6, -2, -3], [-2, 5, -2], [-3, -2, 8], ]; // Generate L and U using matrix A length let matrixL = Array(matrixA.length) .fill() .map(() => Array(matrixA.length).fill(0)); let matrixU = Array(matrixA.length) .fill() .map(() => Array(matrixA.length).fill(0)); crout = () => { // assign matrix A length to a variable let n = matrixA.length; // loop in to index for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { // if j is less than i change value of matrixL to 0 with its corresponding indexes if (j < i) matrixL[j][i] = 0; else { // the first column of matrix L is equal to the first column of matrix A matrixL[j][i] = matrixA[j][i]; // loop the values excluding the first column and top row for (k = 0; k < i; k++) { matrixL[j][i] = matrixL[j][i] - matrixL[j][k] * matrixU[k][i]; } } } // loop in to index for (j = 0; j < n; j++) { // if j is less than i change value of martixU to 0 with its corresponding indexes if (j < i) matrixU[i][j] = 0; // if j is equal to i the change value to 1 // result to a diagonal values of 1 else if (j == i) matrixU[i][j] = 1; else { // get index of the first rows of matrixU matrixU[i][j] = matrixA[i][j] / matrixL[i][i]; // loop to get values excluding the first rows, 1's and 0's for (k = 0; k < i; k++) { matrixU[i][j] = matrixU[i][j] - (matrixL[i][k] * matrixU[k][j]) / matrixL[i][i]; } } } } // print in console console.log("matrixA:\n", matrixA, "\n\nmatrixL:\n", matrixL, "\n\nmatrixU:\n", matrixU); }; crout();