public void SecondEquationTest() { Matrix variableCoefficients = new Matrix(new float[, ] { { 2, -1, -1 }, { 1, 3, -2 }, { 1, 2, 3 } }); Matrix freeCoefficients = new Matrix(new float[, ] { { 5 }, { 7 }, { 10 } }); Matrix expectedResult = new Matrix(new float[, ] { { 61f / 16f }, { 27f / 16f }, { 15f / 16f } }); float epsilon = 0.0001f; float relaxationWeight = 0.8f; Matrix result = RelaxationMethod.Solve(variableCoefficients, freeCoefficients, epsilon, relaxationWeight); Assert.True(expectedResult.NearEquals(result), "Matrices are not equal:\nExpected:{0}\nResult:{1}", expectedResult.ToString(), result.ToString()); }
public void ThirdEquationTest() { Matrix variableCoefficients = new Matrix(new float[, ] { { 8, 5, 3 }, { -2, 8, 1 }, { 1, 3, -10 } }); Matrix freeCoefficients = new Matrix(new float[, ] { { 30 }, { 15 }, { 42 } }); Matrix expectedResult = new Matrix(new float[, ] { { 3f }, { 3f }, { -3f } }); float epsilon = 0.0001f; float relaxationWeight = 0.8f; Matrix result = RelaxationMethod.Solve(variableCoefficients, freeCoefficients, epsilon, relaxationWeight); Assert.True(expectedResult.NearEquals(result), "Matrices are not equal:\nExpected:{0}\nResult:{1}", expectedResult.ToString(), result.ToString()); }
public void FirstEquationTest() { Matrix variableCoefficients = new Matrix(new float[, ] { { 1, -2, 1 }, { 2, -5, -1 }, { -7, 0, 1 } }); Matrix freeCoefficients = new Matrix(new float[, ] { { 2 }, { -1 }, { -2 } }); float epsilon = 0.0001f; float relaxationWeight = 0.8f; Matrix expectedResult = new Matrix(new float[, ] { { 13f / 25f }, { 2f / 25f }, { 41f / 25f } }); Matrix result = RelaxationMethod.Solve(variableCoefficients, freeCoefficients, epsilon, relaxationWeight); Assert.True(expectedResult.NearEquals(result), "Matrices are not equal:\nExpected:{0}\nResult:{1}", expectedResult.ToString(), result.ToString()); }
static void Test(int iterations, Matrix variableCoefficients, Matrix freeCoefficients) { float epsilon = 0.0001f; float relaxationWeight = 0.8f; var stopwatch = new Stopwatch(); Console.Write("######################################################\nSelect the algorithm:\n1.Gauss\n2.Jacobi\n3.Gauss-Seidel\n4.Relaxation\nEnter the choice:"); int algorithm = Convert.ToInt32(Console.ReadLine()); switch (algorithm) { case 1: { stopwatch.Start(); for (int i = 0; i < iterations; i++) { var result = GaussMethod.Solve(Matrix.Clone(variableCoefficients), Matrix.Clone(freeCoefficients)); } stopwatch.Stop(); Console.WriteLine("Total elapsed time: " + stopwatch.Elapsed + "\n######################################################"); break; } case 2: { stopwatch.Start(); for (int i = 0; i < iterations; i++) { var result = JacobiMethod.Solve(Matrix.Clone(variableCoefficients), Matrix.Clone(freeCoefficients), epsilon); } stopwatch.Stop(); Console.WriteLine("Total elapsed time: " + stopwatch.Elapsed); break; } case 3: { stopwatch.Start(); for (int i = 0; i < iterations; i++) { var result = GaussZeidelMethod.Solve(Matrix.Clone(variableCoefficients), Matrix.Clone(freeCoefficients), epsilon); } stopwatch.Stop(); Console.WriteLine("Total elapsed time: " + stopwatch.Elapsed); break; } case 4: { stopwatch.Start(); for (int i = 0; i < iterations; i++) { var result = RelaxationMethod.Solve(Matrix.Clone(variableCoefficients), Matrix.Clone(freeCoefficients), epsilon, relaxationWeight); } stopwatch.Stop(); Console.WriteLine("Total elapsed time: " + stopwatch.Elapsed); break; } } }
/// <summary> /// Perform the approximation using the x and y values from matrices /// </summary> /// <param name="xValues">X values of the points provided - horizontal vector</param> /// <param name="yValues">Y values of the points provided - vertical vector</param> /// <returns>Matrix with A values for each X variable</returns> public static Matrix Approximate(Matrix xValues, Matrix yValues, int k) { if (xValues.Height != 1 || yValues.Width != 1) { throw new Exception("Incorrect input"); } if (k < 1) { return(new Matrix(0, 0)); } if (k < yValues.Height - 1) { xValues = new Matrix(xValues[0].Take(k + 1).ToArray()); yValues = yValues.RowSlice(0, k); } Matrix xMatrix = new Matrix(yValues.Height, xValues.Width); Matrix yMatrix = new Matrix(yValues.Height, 1); // Raise bets for (int y = 0; y < yValues.Height; y++) { for (int x = 0; x < xValues.Width; x++) { float xSum = 0; for (int i = 0; i < xValues.Width; i++) { xSum += (float)Math.Pow(xValues[0, i], y + x); } xMatrix[y, x] = xSum; } float ySum = 0; for (int i = 0; i < yValues.Height; i++) { ySum += (float)Math.Pow(xValues[0, i], y) * yValues[i, 0]; } yMatrix[y, 0] = ySum; } // Solving Matrix aMatrix = RelaxationMethod.Solve(xMatrix, yMatrix, 0.00001f, 0.8f); // GaussZeidelMethod.Solve(xMatrix, yMatrix, 0.00001f); /* * Matrix aMatrix = new Matrix(new float[,] { * { 21.655175185f }, * { -69.1485164816f }, * { 30.3403573138f }, * { 22.8318150818f }, * { -11.6418820939f }, * { -1.52035040089f }, * { 0.85228321723f } * }); */ return(aMatrix); }