Exemplo n.º 1
0
        public double[] LinearSystemSolver(string[] variables, string[] functions)
        {
            // creating nonlinear system instance - Analytical System
            NonlinearSystem system = new AnalyticalSystem(variables, functions);

            double[]      x0;
            SolverOptions options;

            double[]       result;
            SolutionResult actual;

            // creating nonlinear solver instance - Newton-Raphson solver.
            NonlinearSolver solver = new NewtonRaphsonSolver();

            x0      = new double[] { 0.2, 0.2 }; // initial guess for variable values
            options = new SolverOptions()        // options for solving nonlinear problem
            {
                MaxIterationCount = 100,
                SolutionPrecision = 1e-5
            };

            result = null;
            // solving the system
            actual = solver.Solve(system, x0, options, ref result);

            return(result);
        }
        private static void SolverTest()
        {
            Equation[] equations = new Equation[2];
            // Normal forms eq(x) - c = 0
            equations[0] = arg => arg[0] * arg[0] + arg[1] * arg[1] - 1;
            equations[1] = arg => arg[0] * arg[0] - arg[1];
            NonlinearSystem system = new AnalyticalEquationSystem(equations);


            double[]      x0;
            SolverOptions options;

            double[]       result;
            double[]       expected;
            SolutionResult actual;

            // creating nonlinear solver instance - Newton-Raphson solver.
            NonlinearSolver solver = new NewtonRaphsonSolver();

            x0      = new double[] { 0.2, 0.2 }; // initial guess for variable values
            options = new SolverOptions()        // options for solving nonlinear problem
            {
                MaxIterationCount = 100,
                SolutionPrecision = 1e-6
            };

            result = null;
            // solving the system
            actual = solver.Solve(system, x0, options, ref result);

            expected = new double[] { 0.7861513778, 0.6180339887 }; // expected values
                                                                    // printing solution result into console out
            PrintNLResult(system, actual, result, expected, options.SolutionPrecision);
        }
Exemplo n.º 3
0
        private double CalculateAnnualPercentageRate(double loanAmount, double monthlyPayment, double administrationFee, int durationMonths)
        {
            loanAmount -= administrationFee;

            Func <double, double> f = r => loanAmount *Math.Pow(r, durationMonths + 1) - (loanAmount + monthlyPayment) * Math.Pow(r, durationMonths) + monthlyPayment;

            Func <double, double> df = r => loanAmount * (durationMonths + 1) * Math.Pow(r, durationMonths) - (loanAmount + monthlyPayment) * durationMonths * Math.Pow(r, durationMonths - 1);

            var solver = new NewtonRaphsonSolver()
            {
                TargetFunction             = f,
                DerivativeOfTargetFunction = df,
                InitialGuess = _options.Interest,
            };

            var result = solver.Solve();

            return(solver.Status == AlgorithmStatus.Converged ? Math.Round(12 * (result - 1) * 100, 2) : _options.Interest);
        }