matlab-solve-optimization

Formulate, solve, and validate MATLAB optimization problems using problem-based and solver-based workflows.

995|122|Updated Apr 3, 2026
One-click install
npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-solve-optimization
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: matlab-solve-optimization
Source: https://github.com/matlab/matlab-agentic-toolkit/tree/main/skills-catalog/math-and-optimization/matlab-solve-optimization
Command: npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-solve-optimization

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Writing MATLAB optimization code is error-prone: choosing the wrong solver, misconfiguring options, misinterpreting exitflags, or missing hidden problem structure leads to failed convergence and wrong answers. This Skill guides the full optimization lifecycle so problems are classified correctly, solvers are selected and tuned properly, and results are validated.

Core Features & Use Cases

  • Problem Classification & Formulation: Identify the problem class (LP, QP, MILP, NLP, least-squares, nonsmooth) and formulate it with optimproblem, optimvar, and fcn2optimexpr, or fall back to solver-based APIs when appropriate.
  • Solver Selection & Tuning: Choose the narrowest applicable solver (linprog, quadprog, intlinprog, fmincon, lsqnonlin, ga, surrogateopt), verify options with optimoptions, and apply per-solver tuning guidance.
  • Result Validation & Debugging: Interpret exitflags, check constraint violations with issatisfied/infeasibility, verify gradients with checkGradients, and follow a systematic checklist for failed or poor solutions.
  • Use Case: A user fitting parameters to a simulation model gets guidance to wrap the ode45-based objective with fcn2optimexpr as a black-box, select surrogateopt for expensive evaluations, and validate convergence with exitflag and constraint checks.

Quick Start

Ask the agent to formulate and solve your optimization problem in MATLAB, for example: minimize a nonlinear objective subject to constraints using the problem-based approach and validate the result.

Frequently Asked Questions about matlab-solve-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I choose the right MATLAB optimization solver?

Classify the problem first: use linprog for linear programs, quadprog for convex QPs, intlinprog for mixed-integer linear problems, lsqnonlin for nonlinear least squares, and fmincon for general constrained nonlinear problems. Always prefer the narrowest solver matching the problem structure rather than defaulting to fmincon.

When should I use problem-based vs solver-based optimization in MATLAB?

Use problem-based (optimproblem, optimvar) by default for readable formulations and automatic differentiation. Fall back to solver-based when you have pre-coded objectives with exact gradients, need solver features like CheckpointFile, or require C code generation, which problem-based does not support.

Why does fmincon fail to converge or return a negative exitflag?

Common causes are infeasible constraints, poor scaling of variables or objectives, inaccurate finite-difference gradients, or a bad initial point. Check feasibility at x0, rescale values to O(1), try the sqp algorithm, and test multiple starting points with MultiStart.

Can I use fcn2optimexpr with black-box functions like ode45?

Yes, fcn2optimexpr wraps functions containing operations unsupported by automatic differentiation, such as ode45 or MEX-files, as black-boxes. The solver then uses finite differences for gradients, and you can set Analysis="off" with OutputSize to skip tracing of expensive functions.

How do I check if my MATLAB optimization result is valid?

Check the exitflag (positive means success), print output.message, and verify output.firstorderopt is below tolerance for gradient-based solvers. For problem-based solutions, use issatisfied(prob, sol) and infeasibility() to confirm constraints are satisfied.

What solver should I use for expensive black-box optimization in MATLAB?

Use surrogateopt when each function evaluation takes more than 15-20 seconds, such as simulations or MEX-based objectives. It builds a surrogate model to minimize evaluations, supports checkpointing via CheckpointFile, and returns all evaluated points in the trials output.