public void IterateIterationsExceededException() { Func <double, double> fx = x => Math.Pow(x, 8) - 100.0; Func <double, double> dydx = x => 8 * Math.Pow(x, 7); Assert.ThrowsException <IterationsExceededException <double> >(() => NewtonRaphson.Iterate(fx, dydx, iterations: 10)); }
public void NewtonRaphson() { Parameter E1 = new Parameter(0.0); Parameter E2 = new Parameter(0.0); Parameter E3 = new Parameter(0.0); Parameter E4 = new Parameter(0.0); Func <double>[] functions = new Func <double>[] { () => 0.005 * (100.0 - E1 - 2.0 * E2) * (1.0 - E1 - E3) - 100.0 * E1, () => 500.0 * Math.Pow(100.0 - E1 - 2.0 * E2, 2.0) - 100.0 * E2, () => 0.5 * (100.0 - E1 - E3 - 2.0 * E4) - 100.0 * E3, () => 10000.0 * Math.Pow(100.0 * E3 - 2.0 * E4, 2.0) - 100.0 * E4 }; Parameter[] parameters = new Parameter[] { E1, E2, E3, E4 }; NewtonRaphson nr = new NewtonRaphson(parameters, functions); for (int i = 0; i < 15; i++) { nr.Iterate(); } double[] result = nr.GetResult(); for (int i = 0; i <= result.Length - 1; i++) { System.Diagnostics.Debug.WriteLine("E" + i.ToString() + "\t" + result[i].ToString()); } }
public void Iterate() { Func <double, double> fx = x => Math.Pow(x, 8) - 100.0; Func <double, double> dydx = x => 8 * Math.Pow(x, 7); double actual = NewtonRaphson.Iterate(fx, dydx); double expected = 1.77827941003892; Assert.AreEqual(expected, actual); }
private static Complex Iterate(Func <Complex, Complex> fx, Func <Complex, Complex> dydx, Complex?guess = null) { try { return(guess.HasValue ? NewtonRaphson.Iterate(fx, dydx, guess) : NewtonRaphson.Iterate(fx, dydx)); } catch (IterationsExceededException <Complex> exc) { return(exc.LastValue); } }
public static double Rate(double periods, double presentValue, double futureValue, double monthlyPayment, PaymentType type) { double t = (int)type; double guess = ((monthlyPayment * periods) - presentValue) / presentValue; try { return(NewtonRaphson.Iterate( (double r0) => (((presentValue * Math.Pow(1 + r0, periods)) - futureValue) / ((1 + (r0 * t)) * ((Math.Pow(1 + r0, periods) - 1) / r0))) - monthlyPayment, (double r0) => ((futureValue * ((periods * r0 * r0 * t * Math.Pow(1 + r0, periods)) + (periods * r0 * Math.Pow(1 + r0, periods)) - (r0 * Math.Pow(1 + r0, periods)) - Math.Pow(1 + r0, periods) + r0 + 1)) + (presentValue * Math.Pow(1 + r0, periods) * ((-periods * r0 * r0 * t) + Math.Pow(1 + r0, periods) + (r0 * (Math.Pow(1 + r0, periods) - periods - 1)) - 1))) / ((r0 + 1) * (Math.Pow(1 + r0, periods) - 1) * (Math.Pow(1 + r0, periods) - 1) * ((r0 * t) + 1) * ((r0 * t) + 1)), guess)); } catch (IterationsExceededException <double> exc) { return(exc.LastValue); } }
public static BigInteger Sqrt(BigInteger input) { if (input < 0) { throw new ArgumentOutOfRangeException(nameof(input), ErrorMessages.MustBePositive); } if (input == 0) { return(0); } BigInteger t; try { t = NewtonRaphson.Iterate(x => (x * x) - input, x => 2 * x, 2147483648); } catch (IterationsExceededException <BigInteger> exc) { t = exc.LastValue; } if (t * t == input) { return(t); } --t; BigInteger prev = t * t; while (true) { ++t; BigInteger cur = t * t; if (cur < prev || cur > input) { break; } } return(t - 1); }
public static decimal Rate(int periods, decimal presentValue, decimal futureValue, decimal monthlyPayment, PaymentType type) { decimal t = (int)type; decimal N0 = periods; decimal guess = ((monthlyPayment * periods) - presentValue) / presentValue; try { return(NewtonRaphson.Iterate( (decimal r0) => (((presentValue * DecimalMath.Pow(1M + r0, periods)) - futureValue) / ((1M + (r0 * t)) * ((DecimalMath.Pow(1M + r0, periods) - 1) / r0))) - monthlyPayment, (decimal r0) => ((futureValue * ((N0 * r0 * r0 * t * DecimalMath.Pow(1M + r0, periods)) + (periods * r0 * DecimalMath.Pow(1M + r0, periods)) - (r0 * DecimalMath.Pow(1M + r0, periods)) - DecimalMath.Pow(1M + r0, periods) + r0 + 1)) + (presentValue * DecimalMath.Pow(1M + r0, periods) * ((-N0 * r0 * r0 * t) + DecimalMath.Pow(1M + r0, periods) + (r0 * (DecimalMath.Pow(1 + r0, periods) - periods - 1)) - 1))) / ((r0 + 1) * (DecimalMath.Pow(1 + r0, periods) - 1) * (DecimalMath.Pow(1 + r0, periods) - 1) * ((r0 * t) + 1) * ((r0 * t) + 1)), guess)); } catch (IterationsExceededException <decimal> exc) { return(exc.LastValue); } }
private static ulong SqrtInternal(ulong input) { if (input == 0) { return(0); } double i = input; double value; try { value = NewtonRaphson.Iterate(x => (x * x) - i, x => 2.0 * x, 2147483648); } catch (IterationsExceededException <double> exc) { value = exc.LastValue; } ulong t = (ulong)value; while (t * t < t) { --t; } ulong prev = t * t; while (true) { ++t; ulong cur = t * t; if (cur < prev || cur > input) { break; } } return(t - 1); }
public static double Root(double value, int root) { if (IntegerMath.IsPowerOfTwo(root) && value < 0.0) { throw new ArgumentOutOfRangeException(nameof(value), ErrorMessages.MustBePositive); } else if (value == 0.0) { return(0.0); } try { return(NewtonRaphson.Iterate(x => Math.Pow(x, root) - value, x => root * Math.Pow(x, root - 1), rounding: null)); } catch (IterationsExceededException <double> exc) { return(exc.LastValue); } }
public static decimal Root(decimal value, long root) { if (IntegerMath.IsPowerOfTwo(root) && value < 0.0M) { throw new ArgumentOutOfRangeException(nameof(value), ErrorMessages.MustBePositive); } else if (value == 0.0M) { return(0.0M); } try { return(NewtonRaphson.Iterate(x => PowSquareLaw(x, root) - value, x => root * PowSquareLaw(x, root - 1), rounding: null)); } catch (IterationsExceededException <decimal> exc) { return(exc.LastValue); } }