using System.Collections.Generic;
using System.Threading.Tasks;
public static void Main(string[] args)
List<double> y0 = new List<double> { 1.5 };
List<Function> functions = new List<Function> { new Function("y - x", "x", "y") };
DifferentialEquation equation = new DifferentialEquation(x0, xn, n, y0, functions, 1.8, 2.12, 2.464);
List<Point> result = equation.MethodsAdamsa();
public Function(string str, params string[] variables)
operators = new List<string>(standart_operators);
operators.AddRange(prefix_operators);
this.variables = new List<string>(variables);
PostfixNotation = ConvertToPostfixNotation(strFunction);
public string strFunction;
private List<string> operators;
private List<string> standart_operators =
new List<string>(new string[] { "(", ")", "+", "-", "*", "/", "^" });
private List<string> prefix_operators =
new List<string>(new string[] { "cos", "sin", "tg", "ctg", "ln", "log10", "exp" });
string[] PostfixNotation;
private List<string> variables;
private List<string> constants = new List<string>(new string[] { "e", "pi" });
private List<double> constantValues = new List<double>(new double[] { Math.E, Math.PI });
private IEnumerable<string> Separate(string input)
while (pos < input.Length)
string s = string.Empty + input[pos];
if (!standart_operators.Contains(input[pos].ToString()))
if (Char.IsDigit(input[pos]))
for (int i = pos + 1; i < input.Length &&
(Char.IsDigit(input[i]) || input[i] == ',' || input[i] == '.'); i++)
else if (Char.IsLetter(input[pos]))
for (int i = pos + 1; i < input.Length &&
(Char.IsLetter(input[i]) || Char.IsDigit(input[i])); i++)
private byte GetPriority(string s)
public string[] ConvertToPostfixNotation(string input)
List<string> outputSeparated = new List<string>();
Stack<string> stack = new Stack<string>();
foreach (string c in Separate(input))
if (operators.Contains(c))
if (stack.Count > 0 && !c.Equals("("))
else if (prefix_operators.Contains(c))
else if (GetPriority(c) > GetPriority(stack.Peek()))
while (stack.Count > 0 && GetPriority(c) <= GetPriority(stack.Peek()))
outputSeparated.Add(stack.Pop());
foreach (string c in stack)
return outputSeparated.ToArray();
public double result(params double[] variableValues)
Stack<string> stack = new Stack<string>();
Queue<string> queue = new Queue<string>(PostfixNotation);
string str = queue.Dequeue();
if (!operators.Contains(str))
if (constants.Contains(str))
str = constantValues[constants.IndexOf(str)].ToString();
else if (variables.Contains(str))
str = variableValues[variables.IndexOf(str)].ToString();
else if (!double.TryParse(str, out digit))
throw new Exception("Error enter data");
double a = Convert.ToDouble(stack.Pop());
double b = Convert.ToDouble(stack.Pop());
double a = Convert.ToDouble(stack.Pop());
double b = Convert.ToDouble(stack.Pop());
double a = Convert.ToDouble(stack.Pop());
double b = Convert.ToDouble(stack.Pop());
double a = Convert.ToDouble(stack.Pop());
double b = Convert.ToDouble(stack.Pop());
double a = Convert.ToDouble(stack.Pop());
double b = Convert.ToDouble(stack.Pop());
summ = Convert.ToDouble(Math.Pow(Convert.ToDouble(b), Convert.ToDouble(a)));
double a = Convert.ToDouble(stack.Pop());
summ = Convert.ToDouble(Math.Sin(Convert.ToDouble(a)));
double a = Convert.ToDouble(stack.Pop());
summ = Convert.ToDouble(Math.Cos(Convert.ToDouble(a)));
double a = Convert.ToDouble(stack.Pop());
summ = Convert.ToDouble(Math.Tan(Convert.ToDouble(a)));
double a = Convert.ToDouble(stack.Pop());
summ = Convert.ToDouble(1 / Math.Tan(Convert.ToDouble(a)));
double a = Convert.ToDouble(stack.Pop());
summ = Convert.ToDouble(Math.Log(Convert.ToDouble(a)));
double a = Convert.ToDouble(stack.Pop());
summ = Convert.ToDouble(Math.Log10(Convert.ToDouble(a)));
double a = Convert.ToDouble(stack.Pop());
summ = Convert.ToDouble(Math.Exp(Convert.ToDouble(a)));
throw new Exception("Error enter data");
stack.Push(summ.ToString());
return Convert.ToDouble(stack.Pop());
public class DifferentialEquation
public List<Function> functions;
public DifferentialEquation()
public DifferentialEquation(double x0, double xn, double n, List<double> y0, List<Function> functions)
this.functions = functions;
public DifferentialEquation(double x0, double xn, double n, List<double> y0, List<Function> functions, double u1, double u2, double u3)
this.functions = functions;
public List<List<Point>> MethodsLobatto()
List<List<Point>> Points = new List<List<Point>>();
for (int k = 0; k < N; k++)
Points.Add(new List<Point>());
Points[k].Add(new Point(x0, y0[k]));
double[] b = new double[4] { 1 / (double)12, 5 / (double)12, 5 / (double)12, 1 / (double)12 };
double[] c = new double[4] { 0, (5 - Math.Sqrt(5)) / (double)10, (5 + Math.Sqrt(5)) / (double)10, 1 };
double[,] a = new double[4, 4] { { 0, 0, 0, 0 }, { (5 + Math.Sqrt(5)) / (double)60, 1 / (double)6, (15 - 7 * Math.Sqrt(5)) / (double)60, 0 }, { (5 - Math.Sqrt(5)) / (double)60, (15 + 7 * Math.Sqrt(5)) / (double)60, 1 / (double)6, 0 }, { 1 / (double)6, (5 - Math.Sqrt(5)) / (double)12, (5 + Math.Sqrt(5)) / (double)12, 0 } };
double h = (xn - x0) / n;
List<double> yi = Copy(y0);
double[] k1 = new double[N];
double[] k2 = new double[N];
double[] k3 = new double[N];
double[] k4 = new double[N];
for (int k = 0; k < N; k++)
k1[k] = functions[k].result(ToArray(xi, yi));
k2[k] = functions[k].result(ToArray(xi, yi));
k3[k] = functions[k].result(ToArray(xi, yi));
k4[k] = functions[k].result(ToArray(xi, yi));
double[][] K = new double[4][] { k1, k2, k3, k4 };
double[,] KPlusOne = new double[4, N];
for (int j = 0; j < s; j++)
List<double> y = new List<double>();
for (int k = 0; k < N; k++)
for (int i = 0; i < s; i++)
y[k] += h * a[j, i] * K[i][k];
for (int k = 0; k < N; k++)
KPlusOne[j, k] = functions[k].result(ToArray(x, y));
for (int k = 0; k < N; k++)
for (int j = 0; j < s; j++)
if (Math.Abs((KPlusOne[j, k] - K[j][k]) / K[j][k]) > eps) isEps = false;
for (int k = 0; k < N; k++)
for (int j = 0; j < s; j++)
K[j][k] = KPlusOne[j, k];
for (int k = 0; k < N; k++)
for (int j = 0; j < s; j++)
yi[k] += h * b[j] * K[j][k];
Points[k].Add(new Point(xi, yi[k]));
public List<Point> MethodsRungeKutta()
List<Point> Points = new List<Point>();
Points.Add(new Point(x0, y0[0]));
double h = (xn - x0) / n;
Console.WriteLine("y" + i + "=" + yi);
Console.WriteLine("x" + i + "=" + xi);
k1 = functions[0].result(xi, yi);
Console.WriteLine("k1=" + k1);
k2 = functions[0].result(xi + h / 2, yi + h * k1 / 2);
Console.WriteLine("k2=" + k2);
k3 = functions[0].result(xi + h / 2, yi + h * k2 / 2);
Console.WriteLine("k3=" + k3);
k4 = functions[0].result(xi + h, yi + h * k3);
Console.WriteLine("k4=" + k4);
yi = yi + (h / 6) * (k1 + 2 * k2 + 2 * k3 + k4);
Console.WriteLine("y" + (i + 1) + "=" + yi);
Console.WriteLine("x" + (i + 1) + "=" + xi + "\n");
Points.Add(new Point(xi, yi));
public List<Point> MethodsAdamsa()
List<Point> Points = new List<Point>();
double h = (xn - x0) / n;
List<double> xi = new List<double>();
List<double> yi = new List<double>();
Console.WriteLine("y0=" + y0[0]);
Console.WriteLine("x0=" + x0 + "\n");
Points.Add(new Point(xi.Last(), yi.Last()));
Console.WriteLine("y1=" + u1);
Console.WriteLine("x1=" + xi.Last() + "\n");
Points.Add(new Point(xi.Last(), yi.Last()));
Console.WriteLine("y2=" + u2);
Console.WriteLine("x2=" + xi.Last() + "\n");
Points.Add(new Point(xi.Last(), yi.Last()));
Console.WriteLine("y3=" + u3);
Console.WriteLine("x3=" + xi.Last() + "\n");
Points.Add(new Point(xi.Last(), yi.Last()));
yi.Add(yi.Last() + (h / 24) * (55 * functions[0].result(xi[i], yi[i]) - 59 * functions[0].result(xi[i - 1], yi[i - 1]) + 37 * functions[0].result(xi[i - 2], yi[i - 2]) - 9 * functions[0].result(xi[i - 3], yi[i - 3])));
Console.WriteLine("y" + (i + 1) + "=" + yi.Last());
Console.WriteLine("x" + (i + 1) + "=" + xi.Last() + "\n");
Points.Add(new Point(xi.Last(), yi.Last()));
public List<Point> MethodsEuler()
List<Point> Points = new List<Point>();
Points.Add(new Point(x0, y0[0]));
double h = (xn - x0) / n;
Console.WriteLine("y" + i + "=" + yi);
Console.WriteLine("x" + i + "=" + xi);
yi = yi + h * functions[0].result(xi, yi);
Console.WriteLine("y" + (i + 1) + "=" + yi);
Console.WriteLine("x" + (i + 1) + "=" + xi + "\n");
Points.Add(new Point(xi, yi));
public List<double> Copy(List<double> list)
List<double> listCopy = new List<double>();
foreach (double digit in list)
public double[] ToArray(double xi, List<double> yi)
double[] variableValues = new double[N + 1];
for (int i = 0; i < N; i++)
variableValues[i + 1] = yi[i];
public List<List<Point>> MethodsRungeKutta2()
List<List<Point>> Points = new List<List<Point>>();
Points.Add(new List<Point>());
Points.Add(new List<Point>());
Points[0].Add(new Point(x0, y0[0]));
Points[1].Add(new Point(x0, y0[1]));
double h = (xn - x0) / n;
k1 = functions[0].result(xi, y1i, y2i);
Console.WriteLine("k1=" + k1);
l1 = functions[1].result(xi, y1i, y2i);
Console.WriteLine("l1=" + l1);
k2 = functions[0].result(xi + h / 2, y1i + h * k1 / 2, y2i + h * l1 / 2);
Console.WriteLine("k2=" + k2);
l2 = functions[1].result(xi + h / 2, y1i + h * k1 / 2, y2i + h * l1 / 2);
Console.WriteLine("l2=" + l2);
k3 = functions[0].result(xi + h / 2, y1i + h * k2 / 2, y2i + h * l2 / 2);
Console.WriteLine("k3=" + k3);
l3 = functions[1].result(xi + h / 2, y1i + h * k2 / 2, y2i + h * l2 / 2);
Console.WriteLine("l3=" + l3);
k4 = functions[0].result(xi + h, y1i + h * k3, y2i + h * l3);
Console.WriteLine("k4=" + k4);
l4 = functions[1].result(xi + h, y1i + h * k3, y2i + h * l3);
Console.WriteLine("l4=" + l4);
y1i = y1i + (h / 6) * (k1 + 2 * k2 + 2 * k3 + k4);
Console.WriteLine("y" + (i + 1) + "=" + y1i);
y2i = y2i + (h / 6) * (l1 + 2 * l2 + 2 * l3 + l4);
Console.WriteLine("z" + (i + 1) + "=" + y2i);
Console.WriteLine("x" + (i + 1) + "=" + xi + "\n");
Points[0].Add(new Point(xi, y1i));
Points[1].Add(new Point(xi, y2i));
public List<List<Point>> MethodsEuler2()
List<List<Point>> Points = new List<List<Point>>();
Points.Add(new List<Point>());
Points.Add(new List<Point>());
Points[0].Add(new Point(x0, y0[0]));
Points[1].Add(new Point(x0, y0[1]));
double h = (xn - x0) / n;
y1i = y1i + h * functions[0].result(xi, y1i0, y2i);
Console.WriteLine("y" + (i + 1) + "=" + y1i);
y2i = y2i + h * functions[1].result(xi, y1i0, y2i);
Console.WriteLine("z" + (i + 1) + "=" + y2i);
Console.WriteLine("x" + (i + 1) + "=" + xi + "\n");
Points[0].Add(new Point(xi, y1i));
Points[1].Add(new Point(xi, y2i));