System of nonlinear equations
Specify a system of equations using optimization variables, and solve the system
For the full workflow, see Problem-Based Workflow for Solving Equations.
EquationProblem object by using the
function. Add equations to the problem by creating
objects and setting them as
Equations properties of the
prob = eqnproblem; x = optimvar('x'); eqn = x^5 - x^4 + 3*x == 1/2; prob.Equations.eqn = eqn;
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.
Equations— Problem equations
OptimizationEqualityarray | structure with
OptimizationEqualityarrays as fields
Problem equations, specified as an
OptimizationEquality array or structure with
OptimizationEquality arrays as fields.
sum(x.^2,2) == 4
Description— Problem label
''(default) | string | character vector
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
"An iterative approach to the Traveling Salesman problem"
Variables— Optimization variables in object
This property is read-only.
Optimization variables in the object, specified as a structure of
|Create optimization options|
|Convert optimization problem or equation problem to solver form|
|Display information about optimization object|
|Solve optimization problem or equation problem|
|Map problem variables to solver-based variable index|
|Save optimization object description|
To solve the nonlinear system of equations
using the problem-based approach, first define
x as a two-element optimization variable.
x = optimvar('x',2);
Create the first equation as an optimization equality expression.
eq1 = exp(-exp(-(x(1) + x(2)))) == x(2)*(1 + x(1)^2);
Similarly, create the second equation as an optimization equality expression.
eq2 = x(1)*cos(x(2)) + x(2)*sin(x(1)) == 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.
EquationProblem : Solve for: x eq1: exp(-exp(-(x(1) + x(2)))) == (x(2) .* (1 + x(1).^2)) eq2: ((x(1) .* cos(x(2))) + (x(2) .* sin(x(1)))) == 0.5
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,
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.4070e-07 eq2: -3.8255e-08
exitflag = EquationSolved
View the solution point.
Unsupported Functions Require
If your equation functions are not composed of elementary functions, you must convert the functions to optimization expressions using
fcn2optimexpr. For the present example:
ls1 = fcn2optimexpr(@(x)exp(-exp(-(x(1)+x(2)))),x); eq1 = ls1 == x(2)*(1 + x(1)^2); ls2 = fcn2optimexpr(@(x)x(1)*cos(x(2))+x(2)*sin(x(1)),x); eq2 = ls2 == 1/2;