Documentation

# EquationProblem

System of nonlinear equations

## Description

Specify a system of equations using optimization variables, and solve the system using solve.

## Creation

Create an EquationProblem object by using the eqnproblem function. Add equations to the problem by creating OptimizationEquality objects and setting them as Equations properties of the EquationProblem object.

prob = eqnproblem;
x = optimvar('x');
eqn = x^5 - x^4 + 3*x == 1/2;
prob.Equations.eqn = eqn;

### Warning

The problem-based approach does not support complex values in an objective function, nonlinear equalities, or nonlinear inequalities. If a function calculation has a complex value, even as an intermediate value, the final result can be incorrect.

## Properties

expand all

Problem equations, specified as an OptimizationEquality array or structure with OptimizationEquality arrays as fields.

Example: sum(x.^2,2) == 4

Problem label, specified as a string or character vector. The software does not use Description for computation. Description is an arbitrary label that you can use for any reason. For example, you can share, archive, or present a model or problem, and store descriptive information about the model or problem in Description.

Example: "An iterative approach to the Traveling Salesman problem"

Data Types: char | string

Optimization variables in the object, specified as a structure of OptimizationVariable objects.

Data Types: struct

## Object Functions

 optimoptions Create optimization options prob2struct Convert optimization problem or equation problem to solver form show Display optimization object solve Solve optimization problem or equation problem varindex Map problem variables to solver-based variable index write Save optimization object description

## Examples

collapse all

To solve the nonlinear system of equations

$\begin{array}{l}\mathrm{exp}\left(-\mathrm{exp}\left(-\left({x}_{1}+{x}_{2}\right)\right)\right)={x}_{2}\left(1+{x}_{1}^{2}\right)\\ {x}_{1}\mathrm{cos}\left({x}_{2}\right)+{x}_{2}\mathrm{sin}\left({x}_{1}\right)=\frac{1}{2}\end{array}$

using the problem-based approach, first define x as a two-element optimization variable.

x = optimvar('x',2);

Create the left side of the first equation. Because this side is not a polynomial or rational function, process this expression into an optimization expression by using fcn2optimexpr.

ls1 = fcn2optimexpr(@(x)exp(-exp(-(x(1)+x(2)))),x);

Create the first equation.

eq1 = ls1 == x(2)*(1 + x(1)^2);

Similarly, create the left side of the second equation by using fcn2optimexpr.

ls2 = fcn2optimexpr(@(x)x(1)*cos(x(2))+x(2)*sin(x(1)),x);

Create the second equation.

eq2 = ls2 == 1/2;

Create an equation problem, and place the equations in the problem.

prob = eqnproblem;
prob.Equations.eq1 = eq1;
prob.Equations.eq2 = eq2;

Review the problem.

show(prob)
EquationProblem :

Solve for:
x

eq1:
arg_LHS == (x(2) .* (1 + x(1).^2))

where:

anonymousFunction1 = @(x)exp(-exp(-(x(1)+x(2))));
arg_LHS = anonymousFunction1(x);

eq2:
arg_LHS == 0.5

where:

anonymousFunction2 = @(x)x(1)*cos(x(2))+x(2)*sin(x(1));
arg_LHS = anonymousFunction2(x);

Solve the problem starting from the point [0,0]. For the problem-based approach, specify the initial point as a structure, with the variable names as the fields of the structure. For this problem, there is only one variable, x.

x0.x = [0 0];
[sol,fval,exitflag] = solve(prob,x0)
Solving problem using fsolve.

Equation solved.

fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
sol = struct with fields:
x: [2x1 double]

fval = struct with fields:
eq1: -2.4069e-07
eq2: -3.8253e-08

exitflag =
EquationSolved

View the solution point.

disp(sol.x)
0.3532
0.6061