The following is an implementation of Newton's method of finding root of a function.
using System;
using System.Drawing;
using ZedGraph;
//Newton's Method
//https://www.youtube.com/watch?v=2GrfaB88w4M&t=350s
//https://stackoverflow.com/questions/12915317/how-to-find-derivative-of-a-function-using-c
//https://www.geeksforgeeks.org/program-for-newton-raphson-method/
namespace _1_22_Newton_Method_of_Root_Finding
{
public delegate double MyFunction(double x);
public class RootFinder
{
static double EPSILON = 0.01;
static double h = 1.0e-6;
public static double Derivative(MyFunction func, double x0)
{
double x1 = x0 - h;
double x2 = x0 + h;
double y1 = func(x1);
double y2 = func(x2);
double der = (y2 - y1) / (x2 - x1);//<==== i am not sure about this.
return der;
}
private static double NextNewton(MyFunction func, double guess)
{
return guess - func(guess) / Derivative(func, guess);
}
public static double Newton(MyFunction func, double init)
{
double x = init;
double y = NextNewton(func, x);
//for(int i=0 ; i < 10 ; i++)
while(Math.Abs(y) >= EPSILON)
//while(true)
{
y = NextNewton(func, x);
if (x == y)
{
break;
}
Console.WriteLine("Root = {0}", y);
x = y;
}
return y;
}
}
Kindly, review the source code.
Does this source code have any pitfall?