1

I am calculating a value using Newton method, and I use a for loop to test if it is converged or not. But I feel like using a fix value inside the for loop is not a best way to do so. Any suggestion?

double startingRate = 0.1;
double oldStartingRate;
double rateDeviation;
for (int x = 0; x < 50; x++)
{
    oldStartingRate = startingRate;
    startingRate = NewtonMethod(startingRate);
    rateDeviation = Math.Abs(startingRate - oldStartingRate);
    Debug.WriteLine("deviation:" + rateDeviation .ToString()+" rate:" + startingRate);
    if (rateDeviation < deviation)  //deviation is a constant
    {
        return startingRate;
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1701840
  • 1,062
  • 3
  • 19
  • 27
  • a percentage of the initial starting rate perhaps, say a tolerance of 2% deviation = Math.Abs(startingRate * 0.02); – dbugger Dec 18 '12 at 02:42

0 Answers0