Engee 文档
Notebook

求解非线性方程组

这个例子演示了如何使用面向问题的方法求解非线性方程组。

我们将使用[跳转的功能。jl]图书馆(https://github.com/jump-dev/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

任务说明

我们会找到解决方案的 对于下面介绍的非线性方程组,使用面向问题的方法:

\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}}\结束

连接图书馆

连接库 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 包含两个值 – :

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]

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

结论

在这个例子中,我们使用面向问题的方法找到了非线性方程组的解决方案。 我们用了图书馆 JuMP 用于我们的问题的公式化和非线性求解器的库 Ipopt 找到问题的解决方案。