Ken H

Ken H

  • NA
  • 646
  • 361.7k

Newton-Raphson method in c#.

Jul 25 2014 11:15 AM
Hi friend,
     How to reduce the square root values of error?
 
My codes:
 
using System;
namespace IsaacNewto{
 
static class INMath{
static INMath() { }
/// <summary>
/// Newton-Raphson method --Calculate square root.
/// </summary>
/// <param name="n">The square root of n.</param>
/// <returns>Returns the square root of n.</returns>
public static double Sqrt(double n){
 
double inNum1 = n / 2, inNum2;
inNum2 = (inNum1 + (n / inNum1)) / 2;
do
{
inNum1 = inNum2;
inNum2 = (inNum1 +n / inNum1) / 2;
} while (Math.Abs(inNum1 - inNum2) >=Math.Pow(10,-5)); // approximate value.
return inNum1;
}
}
class Program{
 
static void Main(string[] args){
 
Console.WriteLine(Math.Sqrt(36.0));
Console.WriteLine(INMath.Sqrt(36.0));
}
}
}
Thanks. 
 

Answers (2)