// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class Program { public static double f1(double x) { return x*x - 2*x + 3; } public static double f2(double x) { return 1/x + x; } private const double START = 10; private const double SMALL = 0.0000001; private static double D1(func f, double x) { return (f(x+SMALL/2) - f(x-SMALL/2)) / SMALL; } private static double D2(func f, double x) { return (D1(f,x+SMALL/2) - D1(f,x-SMALL/2)) / SMALL; } public delegate double func(double x); public static double FindLocalExtrema(func f) { double x = START; double oldx; do { oldx = x; if(Math.Abs(D2(f,x)) > 1) x = x - D1(f,x) / D2(f,x); else x = x - D1(f,x); } while (Math.Abs(x - oldx) > SMALL); return x; } public static void Main(string[] args) { Console.WriteLine(FindLocalExtrema(f1)); Console.WriteLine(FindLocalExtrema(f2)); } } }