static void Main(string[] args) { decimal n = 1000; decimal a = 0; decimal b = 1; decimal error = Convert.ToDecimal(0.000001); string function = "ln((1+x)/(1-x))"; PostfixNotationExpression parser = new PostfixNotationExpression(); parser.ToPostfixNotation(function); 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.CalculateDerivativeByFiniteDifferencies(Xs, Ys); List<decimal> D2ys = differentiationComponent.CalculateDerivativeByFiniteDifferencies(Xs, Dys); List<decimal> D3ys = differentiationComponent.CalculateDerivativeByFiniteDifferencies(Xs, D2ys); List<decimal> D4ys = differentiationComponent.CalculateDerivativeByFiniteDifferencies(Xs, D3ys); decimal result = 0; decimal partitionCount = 0; /// 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); Console.WriteLine(String.Format("Result: {0}", result)); Console.Read(); }
static void Main(string[] args) { PostfixNotationExpression parser = new PostfixNotationExpression(); string expression = Console.ReadLine(); parser.ToPostfixNotation(expression); string[] strArr = parser.GetLastPostfixNotation(); //List<string> paramsList = new List<string>(); //List<string> parameters = parser.GetParameterList(); //if (parameters.Count > 0) //{ // Console.WriteLine("Set parameters as decimal:"); //} //foreach (string str in parameters) //{ // Console.WriteLine(str + " = "); // paramsList.Add(Console.ReadLine()); //} //Console.WriteLine("Result = " + parser.Result(paramsList.ToArray())); //Console.WriteLine(); //Console.WriteLine("................."); //Console.WriteLine(); foreach (string str in strArr) { Console.Write(str); Console.Write(" "); } parser.CalculatePoint(-2, 7, 100); /// In suggestion that counts of Xs and Ys are equal List<decimal> Xs = parser.GetXsList(); List<decimal> Ys = parser.GetYsList(); Console.WriteLine(); Console.WriteLine("................."); Console.WriteLine("Points:"); for (int i = 0; i < Xs.Count; ++i) { Console.WriteLine(String.Format("\tx = {0}\ty = {1}", Xs.ToArray()[i].ToString(), Ys.ToArray()[i].ToString())); } Console.WriteLine(); Console.WriteLine("................."); Console.WriteLine(); List<decimal> XsHalf = parser.GetXsHalfList(); List<decimal> YsHalf = parser.GetYsHalfList(); Console.WriteLine(); Console.WriteLine("................."); Console.WriteLine("Points:"); for (int i = 0; i < XsHalf.Count; ++i) { Console.WriteLine(String.Format("\tx = {0}\ty = {1}", XsHalf.ToArray()[i].ToString(), YsHalf.ToArray()[i].ToString())); } parser.Dispose(); Console.Read(); }
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()); } }