2016-11-19 9 views
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; 
    } 

答えて

0

。代わりにインデックス0〜2を使用する必要があります。

x1 = x1 - (hess_universe[0, 0] * grad[0] + hess_universe[0, 1] * grad[1] + hess_universe[0, 2] * grad[2]) 

1次元配列でも同様です。

+0

ありがとう、私の友人。私はかなり単純な失敗が起こるようにコーディングではかなり新しいです:) – horadus

+0

@horadusそれがあなたの問題を修正したら、答えを受け入れるべきです。ここでは、理由と方法を読むことができます:http://stackoverflow.com/help/someone-answers – CodingYoshi

関連する問題