Main Content


Optimize factor graph

Since R2022a



solnInfo = optimize(graph,solverOptions) optimizes the factorGraph object graph using the specified factor graph solver options, solverOptions, and returns the resulting solution info solnInfo.


collapse all

Create and optimize a factor graph with custom solver options.

Create Factor Graph and Solver Settings

Create a factor graph and solver options with custom settings. Set the maximum number of iterations to 1000 and set the verbosity of the optimize output to 2.

G = factorGraph;
optns = factorGraphSolverOptions(MaxIterations=1000,VerbosityLevel=2)
optns = 
  factorGraphSolverOptions with properties:

              MaxIterations: 1000
          FunctionTolerance: 1.0000e-06
          GradientTolerance: 1.0000e-10
              StepTolerance: 1.0000e-08
             VerbosityLevel: 2
    TrustRegionStrategyType: 1

Add GPS Factor

Create a GPS factor with node identification number of 1 with NED ReferenceFrame and add it to the factor graph.

fgps = factorGPS(1,ReferenceFrame="NED");

Optimize Factor Graph

Optimize the factor graph with the custom settings. The results of the optimization are displayed with the level of detail depending on the VerbosityLevel.

iter      cost      cost_change  |gradient|   |step|    tr_ratio  tr_radius  ls_iter  iter_time  total_time
   0  0.000000e+00    0.00e+00    0.00e+00   0.00e+00   0.00e+00  1.00e+04        0    1.08e-03    8.65e-03
Terminating: Gradient tolerance reached. Gradient max norm: 0.000000e+00 <= 1.000000e-10

Solver Summary (v 2.0.0-eigen-(3.3.4)-no_lapack-eigensparse-no_openmp-no_custom_blas)

                                     Original                  Reduced
Parameter blocks                            1                        1
Parameters                                  7                        7
Effective parameters                        6                        6
Residual blocks                             1                        1
Residuals                                   3                        3

Minimizer                        TRUST_REGION

Sparse linear algebra library    EIGEN_SPARSE
Trust region strategy                  DOGLEG (TRADITIONAL)

                                        Given                     Used
Threads                                     1                        1
Linear solver ordering              AUTOMATIC                        1

Initial                          0.000000e+00
Final                            0.000000e+00
Change                           0.000000e+00

Minimizer iterations                        1
Successful steps                            1
Unsuccessful steps                          0

Time (in seconds):
Preprocessor                         0.007575

  Residual only evaluation           0.000000 (0)
  Jacobian & residual evaluation     0.000983 (1)
  Linear solver                      0.000000 (0)
Minimizer                            0.010046

Postprocessor                        0.000010
Total                                0.017631

Termination:                      CONVERGENCE (Gradient tolerance reached. Gradient max norm: 0.000000e+00 <= 1.000000e-10)

Input Arguments

collapse all

Factor graph, specified as a factorGraph object.

Solver options for the factor graph, specified as a factorGraphSolverOptions object.

Output Arguments

collapse all

Results of the optimization, returned as a structure containing:

  • InitialCost — Initial cost of the non-linear least squares problem formulated by the factor graph before the optimization.

  • FinalCost — Final cost of the non-linear least squares problem formulated by the factor graph after the optimization.


    Cost is the sum of error terms, known as residuals, where each residual is a function of a subset of factor measurements.

  • NumSuccessfulSteps — Number of iterations in which the solver decreases the cost. This value includes the initialization iteration at 0 in addition to the minimizer iterations.

  • NumUnsuccessfulSteps — Number of iterations in which the iteration is numerically invalid or the solver does not decrease the cost.

  • TotalTime — Total solver optimization time in seconds.

  • TerminationType — Termination type as an integer in the range [0, 2]:

    • 0 — Solver found a solution that meets convergence criterion and decreases in cost after optimization.

    • 1 — Solver could not find a solution that meets convergence criterion after running for the maximum number of iterations.

    • 2 — Solver terminated due to an error.

  • IsSolutionUsable — Solution is usable if 1 (true), not usable if 0 (false).

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2022a