// 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 NumericalIntegration { public delegate double F(double x); public static double Simpson(F f, double a, double b) { int n = 10000; double h = (b - a) / n; double sum = 0; sum += f(a); for(int i = 1; i <= n/2; i++) sum += 4*f(a + (2*i - 1)*h); for(int i = 1; i < n/2-1; i++) sum += 2*f(a + 2*i*h); sum += f(b); return h/3*sum; } } public class Program { public static double Circle(double x) { return Math.Sqrt(1 - x * x); } public static void Main(string[] args) { Console.WriteLine(Math.PI/2); Console.WriteLine(NumericalIntegration.Simpson(Circle, -1, 1)); } } }