public void Oneeq2a() { // Test case from http://www.polymath-software.com/library/nle/Oneeq2a.htm Func <double, double> f1 = T => { const double x1 = 0.332112; const double x2 = 1 - x1; const double G12 = 0.07858889; const double G21 = 0.30175355; double P2 = Math.Pow(10, 6.87776 - 1171.53 / (224.366 + T)); double P1 = Math.Pow(10, 8.04494 - 1554.3 / (222.65 + T)); const double t1 = x1 + x2 * G12; const double t2 = x2 + x1 * G21; double gamma2 = Math.Exp(-Math.Log(t2) - (x1 * (G12 * t2 - G21 * t1)) / (t1 * t2)); double gamma1 = Math.Exp(-Math.Log(t1) + (x2 * (G12 * t2 - G21 * t1)) / (t1 * t2)); double k1 = gamma1 * P1 / 760; double k2 = gamma2 * P2 / 760; return(1 - k1 * x1 - k2 * x2); }; double x = Bisection.FindRoot(f1, 0, 100); Assert.AreEqual(58.7000023925600, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-14); }
public void Oneeq6b() { // Test case from http://www.polymath-software.com/library/nle/Oneeq6b.htm Func <double, double> f1 = V => { const double R = 0.08205; const double T = 273.0; const double B0 = 0.05587; const double A0 = 2.2769; const double C = 128300.0; const double A = 0.01855; const double B = -0.01587; const double P = 100.0; const double Beta = R * T * B0 - A0 - R * C / (T * T); const double Gama = -R * T * B0 * B + A0 * A - R * C * B0 / (T * T); const double Delta = R * B0 * B * C / (T * T); return(R * T * V * V * V + Beta * V * V + Gama * V + Delta - P * V * V * V * V); }; double x = Bisection.FindRoot(f1, 0.1, 1); Assert.AreEqual(0.174749600398905, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-13); }
public void LocalMinima() { Func <double, double> f1 = x => x * x * x - 2 * x + 2; Assert.AreEqual(0, f1(Bisection.FindRoot(f1, -5, 5, 1e-14)), 1e-14); Assert.AreEqual(0, f1(Bisection.FindRoot(f1, -2, 4, 1e-14)), 1e-14); }
public void Oneeq16() { // Test case from http://www.polymath-software.com/library/nle/Oneeq16.htm Func <double, double> f1 = T => { const double y = 0.7; const double x = 1.7; const double z = 1 - y - 0.02; const double CH4 = 0; const double C2H6 = 0; const double COtwo = y + 2 * z; const double H2O = 2 * y + 3 * z; const double N2 = 0.02 + 3.76 * (2 * y + 7 * z / 2) * x; const double O2 = (2 * y + 7 * z / 2) * (x - 1); const double alp = 3.381 * CH4 + 2.247 * C2H6 + 6.214 * COtwo + 7.256 * H2O + 6.524 * N2 + 6.148 * O2; const double bet = 18.044 * CH4 + 38.201 * C2H6 + 10.396 * COtwo + 2.298 * H2O + 1.25 * N2 + 3.102 * O2; const double gam = -4.3 * CH4 - 11.049 * C2H6 - 3.545 * COtwo + 0.283 * H2O - 0.001 * N2 - 0.923 * O2; const double H0 = alp * 298 + bet * 0.001 * 298 * 298 / 2 + gam * 1e-6 * 298 * 298 * 298 / 3; double Hf = alp * T + bet * 0.001 * T * T / 2 + gam * 1e-6 * T * T * T / 3; const double xx = 1; return(212798 * y * xx + 372820 * z * xx + H0 - Hf); }; double r = Bisection.FindRoot(f1, 1000, 3000); Assert.AreEqual(1779.48406483697, r, 1e-5); Assert.AreEqual(0, f1(r), 1e-9); }
public void Oneeq19b() { // Test case from http://www.polymath-software.com/library/nle/Oneeq19b.htm Func <double, double> f1 = z => { const double P = 200; const double Pc = 33.5; const double T = 631; const double Tc = 126.2; const double Pr = P / Pc; const double Tr = T / Tc; double Asqr = 0.4278 * Pr / Math.Pow(Tr, 2.5); const double B = 0.0867 * Pr / Tr; double Q = B * B + B - Asqr; double r = Asqr * B; double p = (-3 * Q - 1) / 3; double q = (-27 * r - 9 * Q - 2) / 27; double R = Math.Pow(p / 3, 3) + Math.Pow(q / 2, 2); double V = (R > 0) ? Math.Pow(-q / 2 + Math.Sqrt(R), 1 / 3) : 0; double WW = (R > 0) ? (-q / 2 - Math.Sqrt(R)) : (0); double psi1 = (R < 0) ? (Math.Acos(Math.Sqrt((q * q / 4) / (-p * p * p / 27)))) : (0); double W = (R > 0) ? (Math.Sign(WW) * Math.Pow(Math.Abs(WW), 1 / 3)) : (0); double z1 = (R < 0) ? (2 * Math.Sqrt(-p / 3) * Math.Cos((psi1 / 3) + 2 * 3.1416 * 0 / 3) + 1 / 3) : (0); double z2 = (R < 0) ? (2 * Math.Sqrt(-p / 3) * Math.Cos((psi1 / 3) + 2 * 3.1416 * 1 / 3) + 1 / 3) : (0); double z3 = (R < 0) ? (2 * Math.Sqrt(-p / 3) * Math.Cos((psi1 / 3) + 2 * 3.1416 * 2 / 3) + 1 / 3) : (0); double z0 = (R > 0) ? (V + W + 1 / 3) : (0); return(z * z * z - z * z - Q * z - r); }; double x = Bisection.FindRoot(f1, -0.5, 1.2); Assert.AreEqual(1.06831213384200, x, 1e-7); Assert.AreEqual(0, f1(x), 1e-14); }
public EuropeanCallOption(string name, object stockP, object strike, object riskFree, object maturity, object sigma = null, object price = null) : base(name, maturity) { this.StockPrice = Input(stockP); this.Strike = Input(strike); this.RiskFree = Input(riskFree); if (sigma != null) { this.Sigma = Input(sigma); this.Price = GenericOption.PriceFunc( () => Math.Exp(-this.StockDivd() * this.Maturity()) * this.StockPrice(), () => Math.Exp(-this.RiskFree() * this.Maturity()) * this.Strike(), this.Sigma, this.Maturity); } else if (price != null) { this.Price = Input(price); this.Sigma = () => { var t = new EuropeanCallOption(string.Empty, this.StockPrice, this.Strike, this.RiskFree, this.Maturity, sigma: 0); return(Bisection.Search(t.RemoteLink(nameof(t.Sigma), nameof(t.Price)), this.Price(), (0, 1))); }; } else { throw new ArgumentNullException(nameof(sigma), "sigma and price can't be both null"); } }
public static bool TryFindRoot(Func <double, double> function, double lowerBound, double upperBound, out double root, double accuracy = 1e-6, int maxIterations = 100) { try { double lowerBoundValue = function(lowerBound); if (lowerBoundValue == 0.0 && lowerBoundValue == function(upperBound)) { // For our usage, we don't want to consider a constant function at zero to have any roots. root = lowerBound; return(false); } if (Brent.TryFindRoot(function, lowerBound, upperBound, accuracy: accuracy, maxIterations: maxIterations, root: out root)) { return(true); } return(Bisection.TryFindRoot(function, lowerBound, upperBound, accuracy: accuracy, maxIterations: maxIterations, root: out root)); } catch (ArithmeticException) { root = double.NaN; return(false); } }
public virtual double InvF(double z) { if (z > H) { return(0); } return(Bisection.FindRoot(x => F(x) - z, -Prec, W / 2 + Prec, Prec)); }
static void Main(string[] args) { Bisection bisection = new Bisection(); var result = bisection.Find(2, 0); Console.WriteLine(result); }
public void BisectionTest1() { FunctionOne f = new FunctionOne(x => 4 * Math.Pow(x, 3) + 3 * Math.Pow(x, 2) - 4 * x - 5); double eps = 0.001; Bisection b = new Bisection(f, -0.1, 1.9, eps); Assert.AreEqual(true, Math.Abs(1.125 - b.Result) <= eps); }
public void Oneeq1() { // Test case from http://www.polymath-software.com/library/nle/Oneeq1.htm Func <double, double> f1 = z => 8 * Math.Pow((4 - z) * z, 2) / (Math.Pow(6 - 3 * z, 2) * (2 - z)) - 0.186; double x = Bisection.FindRoot(f1, 0.1, 0.9, accuracy: 1e-9, maxIterations: 80); Assert.AreEqual(0.277759543089215, x, 1e-9); Assert.AreEqual(0, f1(x), 1e-16); }
public void Oneeq3() { // Test case from http://www.polymath-software.com/library/nle/Oneeq3.htm Func <double, double> f1 = T => Math.Exp(21000 / T) / (T * T) - 1.11e11; double x = Bisection.FindRoot(f1, 550, 560, 1e-2); Assert.AreEqual(551.773822885233, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-2); }
public void Oneeq7() { // Test case from http://www.polymath-software.com/library/nle/Oneeq7.htm Func <double, double> f1 = x => x / (1 - x) - 5 * Math.Log(0.4 * (1 - x) / (0.4 - 0.5 * x)) + 4.45977; double r = Bisection.FindRoot(f1, 0, 0.79, 1e-2); Assert.AreEqual(0.757396293891, r, 1e-5); Assert.AreEqual(0, f1(r), 1e-13); }
public void BisectionTest2() { FunctionOne f = new FunctionOne(x => Math.Pow(x, 2)); double eps = 0.00001; Bisection b = new Bisection(f, -1, 1, eps); Assert.AreEqual(0, b.Result); Assert.AreEqual(1, b.IterationsNumber); }
public void Calculate_ShouldReturnXPosWhereFunctionResultIsMinimum() { var functionHandler = new SingleVarFunc("2 * x ^ 2 - 12 * x"); var bisection = new Bisection(new Interval(0, 10), 1, functionHandler); var result = Math.Round(bisection.Calculate() * 1000) / 1000; Assert.AreEqual(3.125, result); }
static void Main(string[] args) { Console.WriteLine("Main Start"); Bisection bisection = BisectionMethod; IAsyncResult ar = bisection.BeginInvoke(F, -3, 2, 0.001, null, null); Console.WriteLine("Main End"); Console.WriteLine(bisection.EndInvoke(ar)); }
public static double yieldBisection(Bisection solver, Bond bond, double cleanPrice, DayCounter dayCounter, Compounding compounding, Frequency frequency) { double ret = NQuantLibcPINVOKE.BondFunctions_yieldBisection__SWIG_3(Bisection.getCPtr(solver), Bond.getCPtr(bond), cleanPrice, DayCounter.getCPtr(dayCounter), (int)compounding, (int)frequency); if (NQuantLibcPINVOKE.SWIGPendingException.Pending) { throw NQuantLibcPINVOKE.SWIGPendingException.Retrieve(); } return(ret); }
public void Oneeq17() { // Test case from http://www.polymath-software.com/library/nle/Oneeq17.htm Func <double, double> f1 = rp => rp - 0.327 * Math.Pow(0.06 - 161 * rp, 0.804) * Math.Exp(-5230 / (1.987 * (373 + 1.84e6 * rp))); double x = Bisection.FindRoot(f1, 0, 0.00035); Assert.AreEqual(0.000340568862275, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-14); }
// This is the main loop where the menu selection and branching occurs from static public bool EventLoop() { bool finished = false; do { string[] menuItems = new string[] { "1) The computer guesses your number from 1 to 10", "2) You guess the computer's number from 1 to 1000", "3) The computer guesses your number from 1 to 100", "4) You enter the numbers you want the computer to guess between", "5) You enter the limit numbers, computer picks and you guess", "6) Quit" }; Console.Clear(); Output.Intro(); int userChoice = Menu.Selection(menuItems, 0, Console.CursorTop + 1); // the two digits are to place the menu on the x and y axis switch (userChoice) { case 0: Bisection.CompGuess(1, 10); break; case 1: Bisection.HumanGuesses(1, 1000); break; case 2: Bisection.CompGuess(1, 100); break; case 3: Bisection.UserValues(); break; case 4: Bisection.UserValuesHumanGuess(); break; case 5: Console.Clear(); Console.WriteLine("Quit"); finished = true; break; default: Console.Clear(); Console.WriteLine("Default case"); break; } } while (!finished); return(finished); }
public static Triangle[] GetTriangles(Mesh m, out Bisection[] bs, bool useColor = false) { Triangle[] ts = GetTriangles(m); bs = new Bisection[ts.Length]; for (int i = 0; i < bs.Length; i++) { //use vertex color for bisection value. bs [i] = new Bisection(ts [i].area * (useColor ? m.colors [m.triangles [i * 3]].r : 1f)); } Bisection.SetBisectionVal(bs); return(ts); }
public void Oneeq10() { // Test case from http://www.polymath-software.com/library/nle/Oneeq10.htm Func <double, double> f1 = x => (1.0 / 63.0) * Math.Log(x) + (64.0 / 63.0) * Math.Log(1 / (1 - x)) + Math.Log(0.95 - x) - Math.Log(0.9); double r = Bisection.FindRoot(f1, .01, 0.35); Assert.AreEqual(0.036210083704, r, 1e-5); Assert.AreEqual(0, f1(r), 1e-14); r = Bisection.FindRoot(f1, 0.35, 0.7); Assert.AreEqual(0.5, r, 1e-5); Assert.AreEqual(0, f1(r), 1e-14); }
public void Oneeq21b() { // Test case from http://www.polymath-software.com/library/nle/Oneeq21b.htm Func <double, double> f1 = h => { double sg = Math.Acos(2 * h - 1); double si = Math.Pow(1 - Math.Pow(2 * h - 1, 2), 0.5); double ag = (sg - (2 * h - 1) * si) / 4; double al = 3.1415927 / 4 - ag; const double d = 1.44; double dg = 4 * ag / (sg + si) * d; const double ugs = -0.029; double ug = ugs * 3.1415927 / 4 / ag; const double uls = -0.00001; double ul = uls * 3.1415927 / 4 / al; double sl = 3.1415927 - sg; double dl = 4 * al / (sl) * d; const double rol = 1; const double rog = 0.835; const double bt = .6; double g = 980 * Math.Sin(bt * 3.1415927 / 180); double dpg = (rol * al + rog * ag) * g / 3.1415927 * 4; const double vig = 1.0; double reg = Math.Abs(ug) * rog * dg / vig; double tg = -16 / reg * (.5 * rog * ug * Math.Abs(ug)); const double vil = .01; double rel = Math.Abs(ul) * rol * dl / vil; double tl = -16 / rel * (.5 * rol * ul * Math.Abs(ul)); const double it = 1; double ti = -16 / reg * rog * (.5 * (ug - ul) * Math.Abs(ug - ul)) * it; double dpf = (tl * sl + tg * sg) * d / (al + ag) / (d * d) * 4; double dp = dpg + dpf; double tic = (Math.Abs(ul) < Math.Abs(ug)) ? (rog / reg) : (rol / rel); double y = (rol - rog) * g * Math.Pow(d / 2, 2) / (8 * vig * ugs); return(-tg * sg * al + tl * sl * ag - ti * si * (al + ag) + d * (rol - rog) * al * ag * g); }; double x = Bisection.FindRoot(f1, 0, 0.1); Assert.AreEqual(0.00602268958797, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-14); x = Bisection.FindRoot(f1, 0.1, 0.24); Assert.AreEqual(0.19925189173123, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-14); x = Bisection.FindRoot(f1, 0.24, 0.3); Assert.AreEqual(0.26405262123160, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-14); }
public void Oneeq4() { // Test case from http://www.polymath-software.com/library/nle/Oneeq4.htm Func <double, double> f1 = x => { const double A = 0.38969; const double B = 0.55954; return(A * B * (B * Math.Pow(1 - x, 2) - A * x * x) / Math.Pow(x * (A - B) + B, 2) + 0.14845); }; double r = Bisection.FindRoot(f1, 0, 1); Assert.AreEqual(0.691473747542323, r, 1e-5); Assert.AreEqual(0, f1(r), 1e-14); }
/// <summary>Find a solution of the equation f(x)=0.</summary> /// <param name="f">The function to find roots from.</param> /// <param name="df">The first derivative of the function to find roots from.</param> /// <param name="lowerBound">The low value of the range where the root is supposed to be.</param> /// <param name="upperBound">The high value of the range where the root is supposed to be.</param> /// <param name="accuracy">Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Example: 1e-14.</param> /// <param name="maxIterations">Maximum number of iterations. Example: 100.</param> public static double OfFunctionDerivative(Func <double, double> f, Func <double, double> df, double lowerBound, double upperBound, double accuracy = 1e-8, int maxIterations = 100) { double root; if (RobustNewtonRaphson.TryFindRoot(f, df, lowerBound, upperBound, accuracy, maxIterations, 20, out root)) { return(root); } if (Bisection.TryFindRoot(f, lowerBound, upperBound, accuracy, maxIterations, out root)) { return(root); } throw new NonConvergenceException(Resources.RootFindingFailed); }
public void Oneeq15a() { // Test case from http://www.polymath-software.com/library/nle/Oneeq15a.htm Func <double, double> f1 = h => h * (1 + h * (3 - h)) - 2.4 - h; double x = Bisection.FindRoot(f1, -2, 0); Assert.AreEqual(-0.795219754733581, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-13); x = Bisection.FindRoot(f1, 0, 2); Assert.AreEqual(1.13413784566692, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-13); x = Bisection.FindRoot(f1, 2, 5); Assert.AreEqual(2.66108192383197, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-13); }
public void Oneeq15b() { // Test case from http://www.polymath-software.com/library/nle/Oneeq15b.htm Func <double, double> f1 = h => 2.4 / (h * (3 - h)) - h; double x = Bisection.FindRoot(f1, -2, -0.5); Assert.AreEqual(-0.795219745444464, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-14); x = Bisection.FindRoot(f1, 0.5, 2); Assert.AreEqual(1.134137843811, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-14); x = Bisection.FindRoot(f1, 2, 2.9); Assert.AreEqual(2.66108190354552, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-14); }
public void Oneeq8() { // Test case from http://www.polymath-software.com/library/nle/Oneeq8.htm Func <double, double> f1 = v => { const double a = 240; const double b = 40; const double c = 200; return(a * v * v + b * Math.Pow(v, 7 / 4) - c); }; double x = Bisection.FindRoot(f1, 0.01, 1, 1e-10); Assert.AreEqual(0.842524411168525, x, 1e-2); // Hmm, we do not find the correct root within requested accuracy here. Assert.AreEqual(0, f1(x), 1e-10); }
public void Oneeq8() { // Test case from http://www.polymath-software.com/library/nle/Oneeq8.htm Func <double, double> f1 = v => { const double a = 240; const double b = 40; const double c = 200; return(a * v * v + b * Math.Pow(v, 7 / 4) - c); }; double x = Bisection.FindRoot(f1, 0.01, 1); Assert.AreEqual(0.842524411168525, x, 1e-2); Assert.AreEqual(0, f1(x), 1e-12); }
public void Oneeq5() { // Test case from http://www.polymath-software.com/library/nle/Oneeq5.htm Func <double, double> f1 = TR => { const double ALPHA = 7.256; const double BETA = 2.298E-3; const double GAMMA = 0.283E-6; const double DH = -57798.0; return(DH + TR * (ALPHA + TR * (BETA / 2 + TR * GAMMA / 3)) - 298.0 * (ALPHA + 298.0 * (BETA / 2 + 298.0 * GAMMA / 3))); }; double x = Bisection.FindRoot(f1, 3000, 5000); Assert.AreEqual(4305.30991366774, x, 1e-5); Assert.AreEqual(0, f1(x), 1e-10); }
public void Oneeq18b() { // Test case from http://www.polymath-software.com/library/nle/Oneeq18b.htm Func <double, double> f1 = T => { double Tp = (269 * T - 1752) / 267; double k = 0.0006 * Math.Exp(20.7 - 15000 / Tp); double Pp = -(0.1 / (321 * (320.0 / 321.0 - (1 + k)))); double P = (320 * Pp + 0.1) / 321; return(1.3 * (T - Tp) + 1.04e4 * k * Pp); }; double x = Bisection.FindRoot(f1, 500, 1500); Assert.AreEqual(1208.2863599396200, x, 1e-4); Assert.AreEqual(0, f1(x), 1e-12); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Bisection obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; }