linprog — Solve linear programming minimization problems with inequality constraints, equality constraints, and bounds.
linprog minimizes f' * x subject to A*x <= b, Aeq*x == beq, and lb <= x <= ub. Supported forms are x = linprog(f,A,b), x = linprog(f,A,b,Aeq,beq), x = linprog(f,A,b,Aeq,beq,lb,ub), and the two-, three-, and four-output variants.
Syntax
x = linprog(f, A, b)
x = linprog(f, A, b, Aeq, beq)
x = linprog(f, A, b, Aeq, beq, lb, ub)
[x, fval] = linprog(f, A, b)
[x, fval] = linprog(f, A, b, Aeq, beq)
[x, fval] = linprog(f, A, b, Aeq, beq, lb, ub)All supported linprog forms
x = linprog(f, A, b)
x = linprog(f, A, b, Aeq, beq)
x = linprog(f, A, b, Aeq, beq, lb, ub)
[x, fval] = linprog(f, A, b)
[x, fval] = linprog(f, A, b, Aeq, beq)
[x, fval] = linprog(f, A, b, Aeq, beq, lb, ub)
[x, fval, exitflag] = linprog(f, A, b)
[x, fval, exitflag] = linprog(f, A, b, Aeq, beq)
[x, fval, exitflag] = linprog(f, A, b, Aeq, beq, lb, ub)
[x, fval, exitflag, output] = linprog(f, A, b)
[x, fval, exitflag, output] = linprog(f, A, b, Aeq, beq)
[x, fval, exitflag, output] = linprog(f, A, b, Aeq, beq, lb, ub)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
f | NumericArray | Yes | — | Linear objective vector. |
A | NumericArray | Yes | — | Inequality constraint matrix. |
b | NumericArray | Yes | — | Inequality constraint right-hand side. |
Aeq | NumericArray | No | [] | Equality constraint matrix. |
beq | NumericArray | No | [] | Equality constraint right-hand side. |
lb | NumericArray | No | [] | Lower bounds. |
ub | NumericArray | No | [] | Upper bounds. |
Returns
| Name | Type | Description |
|---|---|---|
x | NumericArray | Optimal decision vector. |
fval | NumericScalar | Objective value f'*x at the solution. |
exitflag | NumericScalar | Solver status code. |
output | Any | Diagnostic metadata struct. |
Returned values from linprog depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:linprog:InvalidArgument | The argument count or optional argument grammar is invalid. | linprog: invalid argument |
RunMat:linprog:InvalidInput | Objective, constraint, or bound dimensions/types are invalid. | linprog: invalid input |
How linprog works
f,b,beq,lb, andubmust be numeric vectors with compatible lengths.AandAeqmust be numeric matrices with one column per element off.[]may be passed for omitted optionalAeq,beq,lb, orubarguments.exitflagis1when an optimum is found,-2when no feasible point is found, and-3when the objective is unbounded below.- The fourth output is a struct with
iterations,algorithm,constrviolation, andmessagefields. - Advanced MATLAB options, algorithms, problem structs, and lambda outputs are intentionally unsupported in this initial implementation.
Examples
Solve a bounded linear program
f = [-1; -2];
A = [1 1];
b = 4;
[x, fval, exitflag] = linprog(f, A, b, [], [], [0; 0], [])Expected output:
x =
0
4
fval =
-8
exitflag =
1Use equality constraints and lower bounds
f = [1; 2];
A = [];
b = [];
Aeq = [1 1];
beq = 3;
[x, fval] = linprog(f, A, b, Aeq, beq, [1; 0], [])Expected output:
x =
3
0
fval =
3Use a finite lower bound on one variable
f = [1; 0];
[x, fval, exitflag] = linprog(f, [], [], [], [], [2; -Inf], [])Expected output:
x =
2
0
fval =
2
exitflag =
1Optimize along an equality face
f = [-1; 0; 0];
A = [1 0 0];
b = 1;
Aeq = [0 0 1];
beq = 0;
[x, fval, exitflag] = linprog(f, A, b, Aeq, beq, [], [])Expected output:
x =
1
0
0
fval =
-1
exitflag =
1Inspect infeasible status
[x, fval, exitflag, output] = linprog(1, [], [], [], [], 2, 1);
exitflagExpected output:
exitflag =
-2Using linprog with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how linprog changes the result.
Run a small linprog example, explain the result, then change one input and compare the output.
FAQ
Does linprog support optimoptions or MATLAB problem structs?⌄
No. This implementation supports the common positional numeric call forms only.
Does linprog run on the GPU?⌄
No. It gathers GPU-resident inputs and runs the active-set solve on the host.
Related Math functions
Elementwise
abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh
Reduction
all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how linprog is executed, line by line, in Rust.
- View the source for linprog in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.