Engee 文档
Notebook

求解非线性方程组

本例演示如何使用基于问题的方法求解非线性方程组。

我们将使用库 JuMP.jl 的函数来提出优化问题,并使用非线性优化库 Ipopt.jl

安装库

如果您的环境中没有安装最新版本的JuMP 软件包,请取消注释并运行下面的方框:

In [ ]:
Pkg.add(["Ipopt", "JuMP"])
In [ ]:
#Pkg.add("JuMP");

要在安装完成后启动新版本的程序库,请单击 "我的账户 "按钮:

screenshot_20240710_134852.png 然后点击 "停止 "按钮:

screenshot_20240710_2.png

按下 "Start Engee "按钮重新启动会话:

screenshot_20240710_135431.png

任务描述

让我们采用问题导向法,为下面的非线性方程组找出解$x_1$ 和$x_2$ :

\begin{cases} exp(-exp(-(x_1+x_2)))=x_2(1+x_1^2) \\ \\x_1cos(x_2)+x_2sin(x_1)={\frac{{1}}{2}}\end{cases}

连接库

连接图书馆JuMP

In [ ]:
using JuMP;

连接非线性求解器库Ipopt

In [ ]:
using Ipopt;

创建优化问题

使用函数Model() 创建一个优化问题,并在括号中指定求解器的名称:

In [ ]:
sys_prob = Model(Ipopt.Optimizer)
Out[0]:
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: EMPTY_OPTIMIZER
Solver name: Ipopt

创建一个变量x ,其中包含两个值 -$x_1$ 和$x_2$ :

In [ ]:
@variable(sys_prob, x[1:2]);

使用宏@NLconstraint 设置第一个非线性条件:

In [ ]:
@NLconstraint(sys_prob, exp(-exp(-(x[1] + x[2]))) == x[2] * (1 + x[1]^2))
Out[0]:
$$ (exp(-(exp(-((x[1] + x[2]))))) - x[2] * (1.0 + x[1] ^ {2.0})) - 0.0 = 0 $$

使用宏设置第二个非线性条件@NLconstraint

In [ ]:
@NLconstraint(sys_prob, x[1] * cos(x[2]) + x[2] * sin(x[1]) == 1/2)
Out[0]:
$$ ((x[1] * cos(x[2]) + x[2] * sin(x[1])) - 1.0 / 2.0) - 0.0 = 0 $$

为优化变量设置初始值有助于更有效地控制优化过程。从[0,0] 点开始解决问题。为此,请使用函数set_start_value() 并为以下变量设置初始值 $x_1$и $x_2$:

In [ ]:
set_start_value(x[1], 0.0);
set_start_value(x[2], 0.0);

解决优化问题

解决优化问题

In [ ]:
optimize!(sys_prob)
This is Ipopt version 3.14.13, running with linear solver MUMPS 5.6.1.

Number of nonzeros in equality constraint Jacobian...:        4
Number of nonzeros in inequality constraint Jacobian.:        0
Number of nonzeros in Lagrangian Hessian.............:        6

Total number of variables............................:        2
                     variables with only lower bounds:        0
                variables with lower and upper bounds:        0
                     variables with only upper bounds:        0
Total number of equality constraints.................:        2
Total number of inequality constraints...............:        0
        inequality constraints with only lower bounds:        0
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0  0.0000000e+00 5.00e-01 0.00e+00  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0
   1  0.0000000e+00 3.15e-01 0.00e+00  -1.7 8.73e-01    -  1.00e+00 1.00e+00h  1
   2  0.0000000e+00 5.73e-02 0.00e+00  -1.7 1.98e-01    -  1.00e+00 1.00e+00h  1
   3  0.0000000e+00 8.66e-04 0.00e+00  -2.5 6.84e-02    -  1.00e+00 1.00e+00h  1
   4  0.0000000e+00 2.66e-07 0.00e+00  -5.7 6.23e-04    -  1.00e+00 1.00e+00h  1
   5  0.0000000e+00 2.28e-14 0.00e+00  -8.6 3.20e-07    -  1.00e+00 1.00e+00h  1

Number of Iterations....: 5

                                   (scaled)                 (unscaled)
Objective...............:   0.0000000000000000e+00    0.0000000000000000e+00
Dual infeasibility......:   0.0000000000000000e+00    0.0000000000000000e+00
Constraint violation....:   2.2759572004815709e-14    2.2759572004815709e-14
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   0.0000000000000000e+00    0.0000000000000000e+00
Overall NLP error.......:   2.2759572004815709e-14    2.2759572004815709e-14


Number of objective function evaluations             = 6
Number of objective gradient evaluations             = 6
Number of equality constraint evaluations            = 6
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 6
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 5
Total seconds in IPOPT                               = 0.003

EXIT: Optimal Solution Found.

x 的值存储在一个变量中:

In [ ]:
sol_x = value.(x);

输出优化结果

In [ ]:
println("Solution: x = ", sol_x)
Solution: x = [0.35324661959670156, 0.6060817366414603]

我们为非线性方程组找到的解:

$$x_1 = 0.35324661959670156$$ $$x_2 = 0.6060817366414603$$

结论

在本例中,我们采用了一种以问题为导向的方法,找到了一个非线性方程组的解。我们使用JuMP 库提出问题,并使用非线性求解器库Ipopt 找到问题的解决方案。