0
システムインデックスが範囲外の例外エラーに遭遇しましたが、どこに間違いがありますか。おそらくインデックスに失敗しました。手伝ってくれませんか?あなたの優しさに感謝します。私が最初にここにエラーが発生します。System.IndexOutOfRangeExceptionが発生しました
x1 = x1 - (hess_universe[1, 1] * grad[1] + hess_universe[1, 2] * grad[2] + hess_universe[1, 3] * grad[3])
をここに私のコード:
あなたはサイズの3×3である2次元配列のインデックス3を使用しているので、おそらくです double x1 = 1, x2 = 1, x3 = 1;
double eps = Math.Pow(10, -5);
double[] grad = new double[3];
double [,] hess_universe=new double [3,3]{ { -1.25,-0.5,0.75},{ -0.5,-0.34,-0.5},{ -0.75,-0.5,-1.25} };
while(Math.Abs(funcderiv1(x1,x2,x3))>eps || Math.Abs(funcderiv2(x1, x2,x3))> eps)
{
grad[0] = funcderiv1(x1, x2, x3);
grad[1] = funcderiv2(x1, x2, x3);
grad[2] = funcderiv3(x1, x2, x3);
x1 = x1 - (hess_universe[1, 1] * grad[1] + hess_universe[1, 2] * grad[2] + hess_universe[1, 3] * grad[3]);//i get error in here
x2 = x2 - (hess_universe[2, 1] * grad[1] + hess_universe[2, 2] * grad[2] + hess_universe[2, 3] * grad[3]);
x3 = x3 - (hess_universe[3, 1] * grad[1] + hess_universe[3, 2] * grad[2] + hess_universe[3, 3] * grad[3]);
}
Console.WriteLine("Optimal solution found at: (" + x1 + ',' + x2 + ',' + x3 + ")");
Console.ReadLine();
}
static double funcderiv1(double x1,double x2, double x3)
{
return 3 * x2 - 2 * x1;
}
static double funcderiv2(double x1, double x2, double x3)
{
return 3*x1+3*x3-12*x2;
}
static double funcderiv3(double x1, double x2,double x3)
{
return 3 * x2 - 2 * x3;
}
ありがとう、私の友人。私はかなり単純な失敗が起こるようにコーディングではかなり新しいです:) – horadus
@horadusそれがあなたの問題を修正したら、答えを受け入れるべきです。ここでは、理由と方法を読むことができます:http://stackoverflow.com/help/someone-answers – CodingYoshi