Optimization Toolbox™ provides two approaches for solving equations. This topic describes the problem-based approach. Solver-Based Optimization Problem Setup describes the solver-based approach.

To solve a system of equations, perform the following steps.

Create an equation problem object by using

`eqnproblem`

. A problem object is a container in which you define equations. The equation problem object defines the problem and any bounds that exist in the problem variables.For example, create an equation problem.

prob = eqnproblem;

Create named variables by using

`optimvar`

. An optimization variable is a symbolic variable that you use to describe the equations. Include any bounds in the variable definitions.For example, create a 15-by-3 array of variables named

`'x'`

with lower bounds of`0`

and upper bounds of`1`

.x = optimvar('x',15,3,'LowerBound',0,'UpperBound',1);

Define equations in the problem variables. For example:

sumeq = sum(x,2) == 1; prob.Equations.sumeq = sumeq;

### Note

If you have a nonlinear function that is not composed of polynomials, rational expressions, and elementary functions such as

`exp`

, then convert the function to an optimization expression by using`fcn2optimexpr`

. See Convert Nonlinear Function to Optimization Expression and Supported Operations on Optimization Variables and Expressions.For nonlinear problems, set an initial point as a structure whose fields are the optimization variable names. For example:

`x0.x = randn(size(x)); x0.y = eye(4); % Assumes y is a 4-by-4 variable`

Solve the problem by using

`solve`

.`sol = solve(prob); % Or, for nonlinear problems, sol = solve(prob,x0)`

In addition to these basic steps, you can review the problem definition before solving
the problem by using `show`

or
`write`

. Set
options for `solve`

by using `optimoptions`

, as explained in Change Default Solver or Options.

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.

For a basic equation-solving example with polynomials, see Solve Nonlinear System of Polynomials, Problem-Based. For a general nonlinear example, see Solve Nonlinear System of Equations, Problem-Based. For more extensive examples, see Systems of Nonlinear Equations.

`eqnproblem`

| `fcn2optimexpr`

| `optimoptions`

| `optimvar`

| `show`

| `solve`

| `write`