private double dF(double[] x, double h, double eps) { double[] x1 = new double[10]; x.CopyTo(x1, 0); for (int i = 0; i < x.Length; i++) { x1[i] = x[i] - h * GetdF(x, i, eps); } return(FunctionClass.Function3(x1)); }
private double GetdF(Double[] V, Int32 Index, double accuracy) { double[] temp = new double[V.Length]; for (Int32 i = 0; i < V.Length; i++) { if (i == Index) { temp.SetValue(V[i] + accuracy, i); } else { temp.SetValue(V[i], i); } } return((FunctionClass.Function3(temp) - FunctionClass.Function3(V)) / accuracy); }
// Метод Коши (Наискорейший градиентный спуск) private string Grad(double[] x0, double eps, double h) { // 1 шаг string result = ""; double[] x = new double[x0.Length], grad = new double[x0.Length]; double deltaX = 0; Int32 index = 0; x0.CopyTo(x, 0); result += "Начальная точка:\r\n"; for (Int32 i = 0; i < x0.Length; i++) { result += "x[" + i + "]=" + x0[i] + "\r\n"; } result += "F(x[0], x[1]) = " + FunctionClass.Function3(x0) + "\r\n\r\n"; do { index++; // 2 шаг result += "Вычисление градиенты: \r\n"; for (Int32 i = 0; i < x.Length; i++) { grad[i] = GetdF(x0, i, eps); result += grad[i] + "\r\n"; } // 3 шаг отпадает, т.к. нет ограничений на кол-во итераций // 4 шаг h = GoldenSection(x, eps, leftBorder, rightBorder); result += "Шаг: " + h + "\r\n"; // 5 шаг for (Int32 i = 0; i < x.Length; i++) { x[i] = x0[i] - h * GetdF(x0, i, eps); } result += "\r\nНовая точка: \r\n"; for (Int32 i = 0; i < x.Length; i++) { result += "x[" + i + "] = " + x[i] + "\r\n"; deltaX = Math.Pow(x[i] - x0[i], 2); } result += "F(x[0], x[1]) = " + FunctionClass.Function3(x) + "\r\n\r\n"; x.CopyTo(x0, 0); } // 6 шаг while (Math.Sqrt(Math.Abs(deltaX)) > eps); result += "Минимум:\r\n"; for (Int32 i = 0; i < x.Length; i++) { result += "x[" + i + "] = " + x[i] + "\r\n"; } result += "Значение: " + FunctionClass.Function3(x); MinXBox.Text = x[0].ToString(); MinYBox.Text = x[1].ToString(); MinFBox.Text = FunctionClass.Function3(x).ToString(); IterCountBox.Text = index.ToString(); return(result); }