Solving systems of nonlinear equations¶
This example demonstrates how to solve systems of nonlinear equations using a problem-based approach.
Installing the libraries¶
If your environment does not have the latest version of the JuMP
package installed , uncomment and run the box below:
Pkg.add(["Ipopt", "JuMP"])
#Pkg.add("JuMP");
To launch a new version of the library after the installation is complete, click on the "My Account" button:
Then click on the "Stop" button:
Restart the session by pressing the "Start Engee" button:
Task Description¶
Let's find solutions $x_1$ and $x_2$ for the system of nonlinear equations presented below, using the problem-oriented approach:
\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}
Connecting libraries¶
Connect the library JuMP
:
using JuMP;
Connect the nonlinear solver library Ipopt
:
using Ipopt;
Creating an optimisation problem¶
Create an optimisation problem using the function Model()
and specify the name of the solver in brackets:
sys_prob = Model(Ipopt.Optimizer)
Create a variable x
, containing two values - $x_1$ and $x_2$:
@variable(sys_prob, x[1:2]);
Set the first non-linear condition using the macro @NLconstraint
:
@NLconstraint(sys_prob, exp(-exp(-(x[1] + x[2]))) == x[2] * (1 + x[1]^2))
Set the second non-linear condition using the macro @NLconstraint
:
@NLconstraint(sys_prob, x[1] * cos(x[2]) + x[2] * sin(x[1]) == 1/2)
Setting initial values for the optimisation variables can help to control the optimisation process more efficiently. Solve the problem starting from the point [0,0]
. To do this, use the function set_start_value()
and set initial values for $x_1$
и $x_2$
:
set_start_value(x[1], 0.0);
set_start_value(x[2], 0.0);
Solving an optimisation problem¶
Solve the optimisation problem:
optimize!(sys_prob)
Store the values of x
in a variable:
sol_x = value.(x);
Output the results of the optimisation:
println("Solution: x = ", sol_x)
The solutions we found for the system of nonlinear equations:
$$x_1 = 0.35324661959670156$$ $$x_2 = 0.6060817366414603$$
Conclusion¶
In this example, we found solutions to a system of nonlinear equations using a problem-oriented approach. We used the library JuMP
to formulate our problem and the nonlinear solver library Ipopt
to find solutions to the problem.