The problem-based approach produces the same optimal solution but with a much cleaner formulation. Here's a comparison:
Problem-Based vs Solver-Based Approach
| Aspect |
Solver-Based (fmincon) |
Problem-Based |
| Variable definition |
Implicit in vector x |
Explicit: optimvar('b', ...) |
| Objective |
Anonymous function @(x) x(1)*x(2) |
Direct expression: b * h |
| Constraints |
Separate function returning [c, ceq] |
Readable: 4*P*L^3 <= delta_max * E * b * h^3 |
| Bounds |
Separate lb, ub vectors |
Built into optimvar() |
| Readability |
Requires understanding indexing |
Self-documenting, math-like syntax |
Key Advantages of Problem-Based
1. Readable Formulation
% Problem-based - reads like the actual constraint
prob.Constraints.deflection = 4*P*L^3 <= delta_max * E * b * h^3;
% vs Solver-based - requires separate function
c1 = (P * L^3) / (3 * E * I) - delta_max;
2. Named Variables
b = optimvar('b', 'LowerBound', 0.05, 'UpperBound', 0.20);
h = optimvar('h', 'LowerBound', 0.10, 'UpperBound', 0.50);
% vs x(1), x(2) with separate lb/ub vectors
3. Self-Documenting Output
The show(prob) command displays the complete problem formulation in a human-readable format, making it easy to verify the setup.
4. Automatic Solver Selection
MATLAB analyzes the problem structure and selects an appropriate solver (it chose fmincon here since constraints are nonlinear).
5. Named Solution Access
sol.b, sol.h % vs x_opt(1), x_opt(2)
The problem-based approach is generally preferred for new code as it's more maintainable and less error-prone, especially as problems grow in complexity.