public void Can_Solve_Nonlinear_System_with_3_Variables() { var problem = new EquationSystem(); var x1 = new Variable("x1", 1); var x2 = new Variable("x2", 1); var x3 = new Variable("x3", 1); problem.AddVariables(x1, x2, x3); problem.AddConstraints(new Equation((3 * x1 - Sym.Cos(x2 * x3) - 3.0 / 2.0))); problem.AddConstraints(new Equation(4 * Sym.Pow(x1, 2) - 625 * Sym.Pow(x2, 2) + 2 * x2 - 1)); problem.AddConstraints(new Equation(Sym.Exp(-x1 * x2) + 20 * x3 + (10 * Math.PI - 3.0) / 3.0)); var solver = new Newton(); solver.Solve(problem); Assert.AreEqual(true, solver.IsConverged); Assert.AreEqual(8, solver.Iterations); }
/*Jacobian Density = 88,889% * Hessian Density = 0,000% * Solving NLP Problem(3 Variables, 3 Equations) * ITER SCALE D_NORM INF_PR ALG * 0001 1 1.6 620 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0002 1 0.48 153.0758 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0003 1 0.12 38.338978 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0004 1 0.055 9.1587665 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0005 1 0.02 1.9173079 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0006 1 0.0035 0.24865722 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0007 1 0.00011 0.0076250521 ___(NEWTON, LU/MinimumDegreeAtPlusA) * 0008 1 1.2E-07 8.137733E-06 ___(NEWTON, LU/MinimumDegreeAtPlusA) * [NewtonSolver] * Problem NLP Problem was successfully solved because constraint violation is below tolerance(8 iterations, 0,04 seconds, problem size: 3) ### Printing debugging information NLP Problem ###x1 = 0,833196581863439 ###x2 = 0,0549436583091183 ###x3 = -0,521361434378159 ### ### Generic 0,0000 ( ((4*(x1)^(2) - 625*(x2)^(2)) + 2*x2 - 1) == 0 ) ### Generic 0,0000 ( exp(-(x1)*x2) + 20*x3 + 9.471976 == 0 ) ### Generic 0,0000 ( ((3*x1 - cos(x2* x3)) - 1.5) == 0 )*/ static void Test1() { Console.WriteLine(); Console.WriteLine("### Test Problem: Difficult Function"); var problem = new EquationSystem(); var x1 = new Variable("x1", 1); var x2 = new Variable("x2", 1); var x3 = new Variable("x3", 1); problem.AddVariables(x1, x2, x3); problem.AddConstraints(new Equation((3 * x1 - Sym.Cos(x2 * x3) - 3.0 / 2.0))); problem.AddConstraints(new Equation(4 * Sym.Pow(x1, 2) - 625 * Sym.Pow(x2, 2) + 2 * x2 - 1)); problem.AddConstraints(new Equation(Sym.Exp(-x1 * x2) + 20 * x3 + (10 * Math.PI - 3.0) / 3.0)); var solver = new Newton(); solver.OnLog += Console.WriteLine; solver.OnLogError += Console.WriteLine; solver.OnLogSuccess += Console.WriteLine; solver.Solve(problem); }
Expression HandleExpression(DAEProblem problem, ModelicaExpression expr) { switch (expr) { case BinaryExpression b: { switch (b.Operator) { case BinaryOperator.Add: return(HandleExpression(problem, b.Left) + HandleExpression(problem, b.Right)); case BinaryOperator.Subtract: return(HandleExpression(problem, b.Left) - HandleExpression(problem, b.Right)); case BinaryOperator.Multiply: return(HandleExpression(problem, b.Left) * HandleExpression(problem, b.Right)); case BinaryOperator.Divide: return(HandleExpression(problem, b.Left) / HandleExpression(problem, b.Right)); case BinaryOperator.Power: return(Sym.Pow(HandleExpression(problem, b.Left), HandleExpression(problem, b.Right))); } } break; case UnaryExpression u: { switch (u.Operator) { case UnaryOperator.Negate: return(-HandleExpression(problem, u.Child)); case UnaryOperator.Parentheses: return(Sym.Par(HandleExpression(problem, u.Child))); case UnaryOperator.TimeDerivative: var y = HandleExpression(problem, u.Child) as Variable; if (y == null) { throw new InvalidOperationException("Cannot handle expressions inside der operator yet."); } var dery = new Variable("der(" + y.Name + ")", 0); var existingVar = problem.DifferentialVariables.FirstOrDefault(v => v.Name == dery.Name); if (existingVar == null) { problem.DifferentialVariables.Add(dery); problem.Mapping.Add(dery, y); return(dery); } else { return(existingVar); } default: throw new InvalidOperationException("Unknown unary operator " + u.Operator.ToString()); } } case BuiltinFunction u: { switch (u.Type) { case BuiltinFunctionType.Sin: return(Sym.Sin(HandleExpression(problem, u.Child))); case BuiltinFunctionType.Cos: return(Sym.Cos(HandleExpression(problem, u.Child))); case BuiltinFunctionType.Tan: return(Sym.Tan(HandleExpression(problem, u.Child))); case BuiltinFunctionType.Log: return(Sym.Ln(HandleExpression(problem, u.Child))); case BuiltinFunctionType.Exp: return(Sym.Exp(HandleExpression(problem, u.Child))); case BuiltinFunctionType.Sqrt: return(Sym.Sqrt(HandleExpression(problem, u.Child))); default: throw new InvalidOperationException("Unknown built-in fucntion " + u.Type.ToString()); } } case DoubleLiteral l: return(new Variable(l.Value.ToString(), l.Value)); case Reference r: { var vari = problem.ResolveReference(r.ToString()); if (vari != null) { return(vari); } else { throw new NullReferenceException("Variable " + r.ToString() + " not defined."); } } default: throw new InvalidOperationException("Unknown expression " + expr.ToString()); } return(null); }