Main Content

solvers

Determine default and valid solvers for optimization problem or equation problem

Since R2022b

Description

example

autosolver = solvers(prob) returns the default solver for prob, where prob is an OptimizationProblem object or an EquationProblem object. To solve prob, call solve, which calls the default solver.

example

[autosolver,validsolvers] = solvers(prob) also returns a list of the valid solvers for prob.

Examples

collapse all

Create an optimization problem with Rosenbrock's function as the objective (see Solve a Constrained Nonlinear Problem, Problem-Based).

x = optimvar("x",LowerBound=-3,UpperBound=3);
y = optimvar("y",LowerBound = 0,UpperBound=9);
obj = 100*(y - x^2)^2 + (1 - x)^2;
prob = optimproblem(Objective=obj);

Find the default and valid solvers for the problem.

[autosolver,validsolvers] = solvers(prob)
autosolver = 
"lsqnonlin"
validsolvers = 1x10 string
    "lsqnonlin"    "lsqcurvefit"    "fmincon"    "ga"    "patternsearch"    "surrogateopt"    "particleswarm"    "simulannealbnd"    "gamultiobj"    "paretosearch"

Solve the problem using the default solver.

x0.x = -2.1;
x0.y = 2.2;
[sol,fval] = solve(prob,x0)
Solving problem using lsqnonlin.

Local minimum possible.

lsqnonlin stopped because the final change in the sum of squares relative to 
its initial value is less than the value of the function tolerance.
sol = struct with fields:
    x: 1.0000
    y: 1.0000

fval = 4.9503e-16

Solve the problem using fmincon instead of the default lsqnonlin.

[sol,fval] = solve(prob,x0,Solver="fmincon")
Solving problem using fmincon.

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
sol = struct with fields:
    x: 1.0000
    y: 1.0000

fval = 1.9355e-13

Input Arguments

collapse all

Optimization problem or equation problem, specified as an OptimizationProblem object or an EquationProblem object. Create an optimization problem by using optimproblem; create an equation problem by using eqnproblem.

Warning

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

Example: prob = optimproblem; prob.Objective = obj; prob.Constraints.cons1 = cons1;

Example: prob = eqnproblem; prob.Equations = eqs;

Output Arguments

collapse all

Default solver for prob, returned as a string. The default solver is used by solve or prob2struct when you call these functions without passing the Solver name-value argument.

Valid solvers for prob, returned as a string vector. You can use any of the valid solvers for prob when you specify the Solver name-value argument in the call to solve or prob2struct.

The list of valid solvers depends on your toolbox licenses. For example, if you have a Global Optimization Toolbox license, the list of valid solvers includes ga, the genetic algorithm solver.

Version History

Introduced in R2022b