private void calculateButton_Click(object sender, EventArgs e) { try { string method = MethodsComboBox.SelectedItem.ToString(); string function = UserFunctionTextBox.Text; PostfixNotationExpression parser = new PostfixNotationExpression(); parser.ToPostfixNotation(function); decimal a = Convert.ToDecimal(ParamATextBox.Text); decimal b = Convert.ToDecimal(ParamBTextBox.Text); decimal n = 1000; decimal error = Convert.ToDecimal(ErrorTextBox.Text); parser.CalculatePoint(a, b, n); List<decimal> Xs = parser.GetXsList(); List<decimal> Ys = parser.GetYsList(); Differentiation differentiationComponent = new Differentiation(Xs, Ys); List<decimal> Dys = differentiationComponent.GetFirstDervative(); //= differentiationComponent.CalculateDerivativeByFiniteDifferencies(Xs, Ys); List<decimal> D2ys = differentiationComponent.GetSecondDervative(); //= differentiationComponent.CalculateDerivativeByFiniteDifferencies(Xs, Dys); List<decimal> D3ys = differentiationComponent.GetThirdDervative(); //= differentiationComponent.CalculateDerivativeByFiniteDifferencies(Xs, D2ys); List<decimal> D4ys = differentiationComponent.GetFourthDervative(); //= differentiationComponent.CalculateDerivativeByFiniteDifferencies(Xs, D3ys); decimal result = 0; decimal partitionCount = 0; switch (method) { case "Rectangle Method": { /// Rectangle Method RectangleMethod rectangleMethodComponent = new RectangleMethod(); partitionCount = rectangleMethodComponent.CalculatePartitionCount(a, b, error, D2ys.Max()); if (partitionCount == 0) { partitionCount = n; } parser.CalculatePoint(a, b, partitionCount); List<decimal> FunctionHalfValues = parser.GetYsHalfList(); result = rectangleMethodComponent.Calculate(a, b, partitionCount, FunctionHalfValues); MessageBox.Show(result.ToString(), "Rectangle Method"); rectangleMethodComponent.Dispose(); break; } case "Trapezoidal Rule": { /// Trapezoidal Rule TrapezoidalRule trapezoidalRuleComponent = new TrapezoidalRule(); partitionCount = trapezoidalRuleComponent.CalculatePartitionCount(a, b, error, D2ys.Max()); if (partitionCount == 0) { partitionCount = n; } parser.CalculatePoint(a, b, partitionCount); List<decimal> FunctionValues = parser.GetYsList(); result = trapezoidalRuleComponent.Calculate(a, b, partitionCount, FunctionValues); MessageBox.Show(result.ToString(), "Trapezoidal Rule"); trapezoidalRuleComponent.Dispose(); break; } case "Simpson's Rule": { /// Simpson's Rule SimpsonsRule simpsonsRuleComponent = new SimpsonsRule(); partitionCount = simpsonsRuleComponent.CalculatePartitionCount(a, b, error, D4ys.Max()); if (partitionCount == 0) { partitionCount = n; } parser.CalculatePoint(a, b, partitionCount); List<decimal> FunctionHalfValues = parser.GetYsHalfList(); List<decimal> FunctionValues = parser.GetYsList(); result = simpsonsRuleComponent.Calculate(a, b, partitionCount, FunctionValues, FunctionHalfValues); MessageBox.Show(result.ToString(), "Simpson's Rule"); simpsonsRuleComponent.Dispose(); break; } case "All of these methods": { Container container = new Container(); RectangleMethod rectangleMethodComponent = new RectangleMethod(); TrapezoidalRule trapezoidalRuleComponent = new TrapezoidalRule(); SimpsonsRule simpsonsRuleComponent = new SimpsonsRule(); container.Add(rectangleMethodComponent, "Rectangle Method"); container.Add(trapezoidalRuleComponent, "Trapezoidal Rule"); container.Add(simpsonsRuleComponent, "Simpson's Rule"); string message = ""; /// Rectangle Method partitionCount = rectangleMethodComponent.CalculatePartitionCount(a, b, error, D2ys.Max()); if (partitionCount == 0) { partitionCount = n; } parser.CalculatePoint(a, b, partitionCount); List<decimal> FunctionHalfValues = parser.GetYsHalfList(); result = rectangleMethodComponent.Calculate(a, b, partitionCount, FunctionHalfValues); message += rectangleMethodComponent.Site.Name + ":\n" + result.ToString() + "\n"; /// Trapezoidal Rule partitionCount = trapezoidalRuleComponent.CalculatePartitionCount(a, b, error, D2ys.Max()); if (partitionCount == 0) { partitionCount = n; } parser.CalculatePoint(a, b, partitionCount); List<decimal> FunctionValues = parser.GetYsList(); result = trapezoidalRuleComponent.Calculate(a, b, partitionCount, FunctionValues); message += trapezoidalRuleComponent.Site.Name + ":\n" + result.ToString() + "\n"; /// Simpson's Rule partitionCount = simpsonsRuleComponent.CalculatePartitionCount(a, b, error, D4ys.Max()); if (partitionCount == 0) { partitionCount = n; } parser.CalculatePoint(a, b, partitionCount); FunctionHalfValues = parser.GetYsHalfList(); FunctionValues = parser.GetYsList(); result = simpsonsRuleComponent.Calculate(a, b, partitionCount, FunctionValues, FunctionHalfValues); message += simpsonsRuleComponent.Site.Name + ":\n" + result.ToString(); MessageBox.Show(message, "All methods"); container.Dispose(); break; } default: { MessageBox.Show("Choose method", "Warning"); break; } } differentiationComponent.Dispose(); parser.Dispose(); } catch (Exception exception) { MessageBox.Show(exception.ToString()); } }
private static void FirstTest() { int n = 5; decimal a = -2; decimal b = 7; decimal h = (b - a) / n; List<decimal> Xs = new List<decimal>(); List<decimal> Ys = new List<decimal>(); for (int i = 0; i < n; ++i) { decimal x = a + h * i; Xs.Add(x); Ys.Add(UserFunction(x)); } Console.WriteLine("Points:"); for (int i = 0; i < Xs.Count; ++i) { Console.WriteLine(String.Format("For x = {0}, y = {1}", Xs[i].ToString(), Ys[i].ToString())); } Console.WriteLine(); Console.WriteLine("................."); Console.WriteLine(); Differentiation differentiation = new Differentiation(Xs, Ys); List<decimal> coeffsList = differentiation.GetLagrangeFourthPolynomius(Xs, Ys); Console.WriteLine("Polynomius coeffs:"); foreach (decimal coeff in coeffsList) { Console.WriteLine(String.Format("\t{0}", coeff.ToString())); } Console.WriteLine(); Console.WriteLine("................."); Console.WriteLine(); List<decimal> firstDerivativeCoeffsList = differentiation.GetFirstDerivativeOfLagrangeFourthPolynomius(Xs, Ys); Console.WriteLine("Polynomius first derivative coeffs:"); foreach (decimal coeff in firstDerivativeCoeffsList) { Console.WriteLine(String.Format("\t{0}", coeff.ToString())); } Console.WriteLine(); Console.WriteLine("................."); Console.WriteLine(); List<decimal> secondDerivativeCoeffsList = differentiation.GetSecondDerivativeOfLagrangeFourthPolynomius(Xs, Ys); Console.WriteLine("Polynomius second derivative coeffs:"); foreach (decimal coeff in secondDerivativeCoeffsList) { Console.WriteLine(String.Format("\t{0}", coeff.ToString())); } Console.WriteLine(); Console.WriteLine("................."); Console.WriteLine(); foreach (decimal x in Xs) { decimal y = differentiation.CalculateLagrangeFourthPolynomius(x, Xs, Ys); Console.WriteLine(String.Format("For X = {0}, Y = {1}", x.ToString(), y.ToString())); } Console.WriteLine(); Console.WriteLine("................."); Console.WriteLine(); /// Hand type Console.WriteLine("Type x:"); decimal X = Convert.ToDecimal(Console.ReadLine()); decimal Y = differentiation.CalculateLagrangeFourthPolynomius(X, Xs, Ys); Console.WriteLine(String.Format("For X = {0}, Y = {1}", X.ToString(), Y.ToString())); decimal DY = differentiation.CalculateFirstDerivativeOfLagrangeFourthPolynomius(X, Xs, Ys); Console.WriteLine(String.Format("For X = {0}, DY = {1}", X.ToString(), DY.ToString())); decimal D2Y = differentiation.CalculateSecondDerivativeOfLagrangeFourthPolynomius(X, Xs, Ys); Console.WriteLine(String.Format("For X = {0}, D2Y = {1}", X.ToString(), D2Y.ToString())); Console.WriteLine(); Console.WriteLine("................."); Console.WriteLine(); differentiation.Dispose(); }