// 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; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Reflection; using System.CodeDom.Compiler; using Microsoft.CSharp; namespace E { public struct Point { public double x; public double y; } public class Calc { private const double START = 10; private const double SMALL = 0.0000001; public delegate double func(double x); private static double D1(func f, double x) { return (f(x+SMALL/2) - f(x-SMALL/2)) / SMALL; } public static double FindLocalExtrema(func f) { double x = START; double oldx; do { oldx = x; x = x - f(x) / D1(f,x); } while (Math.Abs(x - oldx) > SMALL); return x; } public static Point TwoFunctionsCross(func f1, func f2) { double x0 = FindLocalExtrema(x => f1(x) - f2(x)); Point p; p.x = x0; p.y = f1(x0); return p; } } public class MainClass { public static void Test(Calc.func f1, Calc.func f2) { Point p = Calc.TwoFunctionsCross(f1, f2); Console.WriteLine(p.x + " " + p.y); } public static void Main(string[] args) { // 2x*e^x=log(2x)+x^e Test(x => 2*x*Math.Exp(x), x => Math.Log(2*x) + Math.Pow(x, Math.E)); // 2x*e^x=log(2/x)+x Test(x => 2*x*Math.Exp(x), x => Math.Log(2/x) + x); // e^(2x+1)=2*e^(x+2) Test(x => Math.Exp(2*x+1), x => 2*Math.Exp(x+2)); } } }