| | |
| | | return result; |
| | | } |
| | | |
| | | public static (double a, double b, double c) quadraticLine(double[] arrX, double[] arrY) |
| | | { |
| | | double[] coef = new double[3]; |
| | | double[,] AA = new double[3, 3]; |
| | | double[,] TAA = new double[3, 3]; |
| | | double[,] BAA = new double[3, 1]; |
| | | double[] B = new double[3]; |
| | | int m = arrX.Length; |
| | | |
| | | double[] arrXp = new double[m]; |
| | | double[] arrXc = new double[m]; |
| | | double[] arrXv = new double[m]; |
| | | double[] y = new double[m]; |
| | | |
| | | for (int i = 0; i < m; i++) |
| | | { |
| | | arrXp[i] = arrX[i] * arrX[i]; |
| | | } |
| | | for (int i = 0; i < m; i++) |
| | | { |
| | | arrXc[i] = arrX[i] * arrX[i] * arrX[i]; |
| | | } |
| | | for (int i = 0; i < m; i++) |
| | | { |
| | | arrXv[i] = arrX[i] * arrX[i] * arrX[i] * arrX[i]; |
| | | } |
| | | double a1 = mysum(arrX); |
| | | double a2 = mysum(arrXp); |
| | | double a3 = mysum(arrXc); |
| | | double a4 = mysum(arrXv); |
| | | |
| | | AA[0, 0] = m; |
| | | AA[0, 1] = a1; |
| | | AA[0, 2] = a2; |
| | | AA[1, 0] = a1; |
| | | AA[1, 1] = a2; |
| | | AA[1, 2] = a3; |
| | | AA[2, 0] = a2; |
| | | AA[2, 1] = a3; |
| | | AA[2, 2] = a4; |
| | | double[] xy = new double[m]; |
| | | double[] xxy = new double[m]; |
| | | for (int i = 0; i < m; i++) |
| | | { |
| | | xy[i] = arrX[i] * arrY[i]; |
| | | } |
| | | for (int i = 0; i < m; i++) |
| | | { |
| | | xxy[i] = arrX[i] * arrX[i] * arrY[i]; |
| | | } |
| | | double b1 = mysum(arrY); |
| | | double b2 = mysum(xy); |
| | | double b3 = mysum(xxy); |
| | | B[0] = b1; |
| | | B[1] = b2; |
| | | B[2] = b3; |
| | | TAA = ReverseMatrix(AA, 3); |
| | | for (int i = 0; i < 3; i++) |
| | | { |
| | | for (int j = 0; j < 3; j++) |
| | | { |
| | | coef[i] = coef[i] + TAA[i, j] * B[j]; |
| | | } |
| | | } |
| | | return (coef[2], coef[1], coef[0]); |
| | | |
| | | } |
| | | |
| | | public static double[,] ReverseMatrix(double[,] dMatrix, int Level) |
| | | { |
| | | double dMatrixValue = MatrixValue(dMatrix, Level); |
| | | if (dMatrixValue == 0) return null; |
| | | double[,] dReverseMatrix = new double[Level, 2 * Level]; |
| | | double x, c; |
| | | // Init Reverse matrix |
| | | for (int i = 0; i < Level; i++) |
| | | { |
| | | for (int j = 0; j < 2 * Level; j++) |
| | | { |
| | | if (j < Level) |
| | | dReverseMatrix[i, j] = dMatrix[i, j]; |
| | | else |
| | | dReverseMatrix[i, j] = 0; |
| | | } |
| | | dReverseMatrix[i, Level + i] = 1; |
| | | } |
| | | for (int i = 0, j = 0; i < Level && j < Level; i++, j++) |
| | | { |
| | | if (dReverseMatrix[i, j] == 0) |
| | | { |
| | | int m = i; |
| | | for (; dMatrix[m, j] == 0; m++) ; |
| | | if (m == Level) |
| | | return null; |
| | | else |
| | | { |
| | | // Add i-row with m-row |
| | | for (int n = j; n < 2 * Level; n++) |
| | | dReverseMatrix[i, n] += dReverseMatrix[m, n]; |
| | | } |
| | | } |
| | | // Format the i-row with "1" start |
| | | x = dReverseMatrix[i, j]; |
| | | if (x != 1) |
| | | { |
| | | for (int n = j; n < 2 * Level; n++) |
| | | if (dReverseMatrix[i, n] != 0) |
| | | dReverseMatrix[i, n] /= x; |
| | | } |
| | | // Set 0 to the current column in the rows after current row |
| | | for (int s = Level - 1; s > i; s--) |
| | | { |
| | | x = dReverseMatrix[s, j]; |
| | | for (int t = j; t < 2 * Level; t++) |
| | | dReverseMatrix[s, t] -= (dReverseMatrix[i, t] * x); |
| | | } |
| | | } |
| | | // Format the first matrix into unit-matrix |
| | | for (int i = Level - 2; i >= 0; i--) |
| | | { |
| | | for (int j = i + 1; j < Level; j++) |
| | | if (dReverseMatrix[i, j] != 0) |
| | | { |
| | | c = dReverseMatrix[i, j]; |
| | | for (int n = j; n < 2 * Level; n++) |
| | | dReverseMatrix[i, n] -= (c * dReverseMatrix[j, n]); |
| | | } |
| | | } |
| | | double[,] dReturn = new double[Level, Level]; |
| | | for (int i = 0; i < Level; i++) |
| | | for (int j = 0; j < Level; j++) |
| | | dReturn[i, j] = dReverseMatrix[i, j + Level]; |
| | | return dReturn; |
| | | } |
| | | private static double MatrixValue(double[,] MatrixList, int Level) |
| | | { |
| | | double[,] dMatrix = new double[Level, Level]; |
| | | for (int i = 0; i < Level; i++) |
| | | for (int j = 0; j < Level; j++) |
| | | dMatrix[i, j] = MatrixList[i, j]; |
| | | double c, x; |
| | | int k = 1; |
| | | for (int i = 0, j = 0; i < Level && j < Level; i++, j++) |
| | | { |
| | | if (dMatrix[i, j] == 0) |
| | | { |
| | | int m = i; |
| | | for (; dMatrix[m, j] == 0; m++) ; |
| | | if (m == Level) |
| | | return 0; |
| | | else |
| | | { |
| | | // Row change between i-row and m-row |
| | | for (int n = j; n < Level; n++) |
| | | { |
| | | c = dMatrix[i, n]; |
| | | dMatrix[i, n] = dMatrix[m, n]; |
| | | dMatrix[m, n] = c; |
| | | } |
| | | // Change value pre-value |
| | | k *= (-1); |
| | | } |
| | | } |
| | | // Set 0 to the current column in the rows after current row |
| | | for (int s = Level - 1; s > i; s--) |
| | | { |
| | | x = dMatrix[s, j]; |
| | | for (int t = j; t < Level; t++) |
| | | dMatrix[s, t] -= dMatrix[i, t] * (x / dMatrix[i, j]); |
| | | } |
| | | } |
| | | double sn = 1; |
| | | for (int i = 0; i < Level; i++) |
| | | { |
| | | if (dMatrix[i, i] != 0) |
| | | sn *= dMatrix[i, i]; |
| | | else |
| | | return 0; |
| | | } |
| | | return k * sn; |
| | | } |
| | | |
| | | public static double mysum(double[] XX) |
| | | { |
| | | double reout = 0; |
| | | int mm = XX.Length; |
| | | for (int i = 0; i < mm; i++) |
| | | { |
| | | reout = reout + XX[i]; |
| | | } |
| | | return reout; |
| | | } |
| | | |
| | | public static double GetParabolaValue(double x, double a, double b, double c) |
| | | { |
| | | return a * x * x + b * x + c; |