// 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 delegate double F(double x); public static class Integrate { private const int N = 1000; public static double Trapez(F f, double x1, double x2) { double d = (x2 - x1) / N; double res = 0; res += 0.5 * d * f(x1); for(int i = 1; i < N; i++) { res += d * f(x1 + i*d); } res += 0.5 * d * f(x2); return res; } } public class Program { public static void Main(string[] args) { Console.WriteLine(Integrate.Trapez(x => x*x, 0, 1)); Console.WriteLine(Integrate.Trapez(x => Math.Sin(x), 0, Math.PI)); } } }