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 SecondTest() { int n = 10; decimal a = 0; 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> firstDerivativesList = differentiation.GetFirstDervative(); Console.WriteLine("Polynomius first derivative:"); foreach (decimal dy in firstDerivativesList) { Console.WriteLine(String.Format("\t{0}", dy.ToString())); } Console.WriteLine(); Console.WriteLine("................."); Console.WriteLine(); List<decimal> secondDerivativesList = differentiation.GetSecondDervative(); Console.WriteLine("Polynomius second derivative:"); foreach (decimal d2y in secondDerivativesList) { Console.WriteLine(String.Format("\t{0}", d2y.ToString())); } Console.WriteLine(); Console.WriteLine("................."); Console.WriteLine(); List<decimal> thirdDerivativesList = differentiation.GetThirdDervative(); Console.WriteLine("Polynomius third derivative:"); foreach (decimal d3y in thirdDerivativesList) { Console.WriteLine(String.Format("\t{0}", d3y.ToString())); } Console.WriteLine(); Console.WriteLine("................."); Console.WriteLine(); List<decimal> fourthDerivativesList = differentiation.GetFourthDervative(); Console.WriteLine("Polynomius fourth derivative:"); foreach (decimal d4y in fourthDerivativesList) { Console.WriteLine(String.Format("\t{0}", d4y.ToString())); } Console.WriteLine(); Console.WriteLine("................."); Console.WriteLine(); }