optimvar — Create named optimization-variable arrays and indexed references.
optimvar creates symbolic variable definitions, not numerical decision values. Copies and indexed references retain the same original definition. Bound edits through a reference update the corresponding original coordinates.
Syntax
x = optimvar(name,dimensions...,Name,Value,...)Inputs
name,dimensions,properties- Type
Any- Required
- Variadic
- Default
- —
- Description
- Variable name; positive double dimensions, dimension vector or named axes; Type, LowerBound and UpperBound property pairs.
Returns
variable- Type
Any- Description
- OptimizationVariable definition view or indexed reference retaining its original definition.
Errors
RunMat:optimvar:InvalidInput- When
- Invalid variable name, dimensions, labels, type, bounds, reference or property operation.
- Message
optimvar: invalid optimization-variable input
RunMat:PropertyReadOnly- When
- Name assignment or an array-wide Type/IndexNames edit through an indexed reference.
- Message
optimization-variable property is read-only on this reference
Dimensions and names
optimvar(name) creates a scalar; optimvar(name,n) creates n-by-1. Supply separate positive double dimensions, one dimension vector, or a cell of numeric and named axes for N-D arrays. Named axes use nonempty string vectors or cell vectors of text, with unique nonempty labels per axis. A lone named vector preserves its row/column orientation. A scalar string is not an axis-name vector; wrap a single label in a cell.
The first name is a valid variable identifier. It remains read-only. Different definitions can have the same displayed name; model assembly must resolve conflicting definition names rather than merging their identities.
Types and bounds
Type defaults to continuous; integer, semi-continuous and semi-integer are also supported. The original array has one type. Semi domains admit zero or values inside positive active bounds, both at most 1e5. Their mixed-integer solver interpretation remains distinct from an ordinary convex interval.
LowerBound and UpperBound default to -Inf and Inf. Supply a real double scalar or an elementwise double array; row/column vectors of matching length are accepted for vector variables. Scalars stay compact until an array presentation or partial edit needs element storage. NaN, complex, single, logical, native integer, sparse and device bound carriers are not admitted.
Copies, references and properties
Numeric/logical selectors, colon selectors and named axes create references to original coordinates. Chained indexing composes that original mapping. No new variable definition is created. LowerBound and UpperBound edits, including edits on an indexed reference, update the original definition atomically. Repeated indices use normal assignment ordering.
The RunMat getfield/setfield extensions also support native variable properties. In a setfield index cell, textual end is resolved against the current retained property value, once that child is reached; it is not a named-axis label or a second getter call.
Name is read-only on all views. Type and IndexNames can be changed on an original definition view, but are read-only on an indexing-created reference, including an identity selection. IndexNames has one cell entry per axis; empty entries select numeric indexing. Reference labels project the current original labels through their stored source-axis mapping.
Execution and current scope
Construction and property operations use host double data and the original invocation context. Numeric work cooperates with cancellation. Invalid carriers do not trigger numeric gathering or callback-capture conversion. Native variable references retain their GC definition through normal rooted publication. No random, filesystem, network or user-object callback is used to create a definition.
With a finite managed host workspace, construction is available when the original session has already registered the canonical variable class. Names, named-axis metadata, shapes, native definition/reference headers and root leases use retained allocation owners; bound arrays share their original double storage. Cold class registration, property/index operations, edits, expression transformations, numerical lowering and native-object transport still require their separate unbounded workspace paths.
Reference shapes and selected offsets use checked platform-sized extents. A selection retains only its addressed offsets, without allocating the whole virtual variable array or imposing a separate 32-bit physical-offset limit. The per-selection materialization limit still applies. Reading full bounds or replacing a physical bound generation requires storage for that actual result; a small reference does not make full-array property operations cheap.
Arithmetic and supported numerical functions build retained optimization expressions; optimexpr creates expression slots and evaluate obtains numerical values at a supplied point. Problem construction, solver lowering and export remain separate families; a variable constructor is not a numerical solver. Public handle superclass/deletion behavior is not inferred from alias-copy behavior.
Examples
Change selected original bounds through a reference
x=optimvar('x',3,'LowerBound',0);
y=x([1,3]);
y.LowerBound=2;
b=x.LowerBoundUse named coordinates in an integer array
x=optimvar('stock',{'red','blue'},2,'Type','integer','LowerBound',0,'UpperBound',10);
x('blue',2).UpperBound=4;
b=x.UpperBoundUsing optimvar with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how optimvar changes the result.
Run a small optimvar example, explain the result, then change one input and compare the output.
Related Math functions
Optim
acceptancesa · annealingboltz · annealingfast · coneprog · crossoverarithmetic · crossoverheuristic · crossoverintermediate · crossoverlaplace · crossoverscattered · crossoversinglepoint · crossovertwopoint · eqnproblem · EquationProblem.solve · EquationProblem.subsasgn · EquationProblem.subsref · evaluate · fgoalattain · fitscalingprop · fitscalingrank · fitscalingshiftlinear · fitscalingtop · fminbnd · fmincon · fminimax · fminsearch · fminunc · fseminf · fsolve · fzero · ga · gacreationlinearfeasible · gacreationnonlinearfeasible · gacreationsobol · gacreationuniform · gacreationuniformint · gaplotbestf · gaplotbestindiv · gaplotdistance · gaplotexpectation · gaplotgenealogy · gaplotmaxconstr · gaplotrange · gaplotscorediversity · gaplotscores · gaplotselection · gaplotstopping · integerConstraint · integral · intlinprog · linprog · lsqcurvefit · lsqlin · LsqlinWarmStart.subsasgn · LsqlinWarmStart.subsref · lsqnonlin · lsqnonneg · mutationadaptfeasible · mutationgaussian · mutationpositivebasis · mutationpower · mutationuniform · optimexpr · optimget · OptimizationConstraint.evaluate · OptimizationConstraint.subsasgn · OptimizationConstraint.subsref · OptimizationExitflag.double · OptimizationExitflag.eq · OptimizationExitflag.ne · OptimizationExitflag.string · OptimizationExpression.acos · OptimizationExpression.acosh · OptimizationExpression.acot · OptimizationExpression.acoth · OptimizationExpression.acsc · OptimizationExpression.acsch · OptimizationExpression.asec · OptimizationExpression.asech · OptimizationExpression.asin · OptimizationExpression.asinh · OptimizationExpression.atan · OptimizationExpression.atanh · OptimizationExpression.cat · OptimizationExpression.cos · OptimizationExpression.cosh · OptimizationExpression.cot · OptimizationExpression.coth · OptimizationExpression.csc · OptimizationExpression.csch · OptimizationExpression.ctranspose · OptimizationExpression.cumprod · OptimizationExpression.cumsum · OptimizationExpression.diag · OptimizationExpression.diff · OptimizationExpression.dot · OptimizationExpression.eq · OptimizationExpression.evaluate · OptimizationExpression.exp · OptimizationExpression.ge · OptimizationExpression.horzcat · OptimizationExpression.ldivide · OptimizationExpression.le · OptimizationExpression.log · OptimizationExpression.mean · OptimizationExpression.minus · OptimizationExpression.mldivide · OptimizationExpression.mpower · OptimizationExpression.mrdivide · OptimizationExpression.mtimes · OptimizationExpression.norm · OptimizationExpression.plus · OptimizationExpression.power · OptimizationExpression.prod · OptimizationExpression.rdivide · OptimizationExpression.reshape · OptimizationExpression.sec · OptimizationExpression.sech · OptimizationExpression.sin · OptimizationExpression.sinh · OptimizationExpression.sqrt · OptimizationExpression.subsasgn · OptimizationExpression.subsref · OptimizationExpression.sum · OptimizationExpression.tan · OptimizationExpression.tanh · OptimizationExpression.times · OptimizationExpression.trace · OptimizationExpression.transpose · OptimizationExpression.uminus · OptimizationExpression.uplus · OptimizationExpression.vertcat · OptimizationExpression.zeros · OptimizationExpression.zerosLike · OptimizationProblem.solve · OptimizationProblem.subsasgn · OptimizationProblem.subsref · OptimizationVariable.subsasgn · OptimizationVariable.subsref · optimoptions · optimplotconstrviolation · optimplotfirstorderopt · optimplotfunccount · optimplotfval · optimplotfvalconstr · optimplotmilp · optimplotstepsize · optimplotx · optimproblem · optimset · optimwarmstart · particleswarm · patternsearch · psplotbestf · psplotbestx · psplotfuncount · psplotmaxconstr · psplotmeshsize · pswcreationuniform · pswplotbestf · quad · quadprog · QuadprogWarmStart.subsasgn · QuadprogWarmStart.subsref · saplotbestf · saplotbestx · saplotf · saplotstopping · saplottemperature · saplotx · savemilpsolutions · secondordercone · selectionremainder · selectionroulette · selectionstochunif · selectiontournament · selectionuniform · simulannealbnd · solve · surrogateopt · surrogateoptplot · temperatureboltz · temperatureexp · temperaturefast
Elementwise
abs · angle · besselj · bsxfun · complex · conj · double · eps · erf · erfcinv · exp · expm1 · flintmax · gamma · gammaln · heaviside · hypot · imag · int16 · int32 · int64 · int8 · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · realmax · realmin · realsqrt · rescale · sign · single · sqrt · times · typecast · uint16 · uint32 · uint64 · uint8
Trigonometry
acos · acosd · acosh · acotd · acscd · asecd · asin · asind · asinh · atan · atan2 · atan2d · atand · atanh · cos · cosd · cosh · cospi · deg2rad · pol2cart · rad2deg · sin · sind · sinh · sinpi · tan · tand · tanh
Reduction
all · any · bounds · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · maxk · mean · median · min · mink · movmax · movmean · movmedian · movmin · movprod · movstd · movsum · movvar · nnz · prod · rms · std · sum · trapz · var
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
Signal
blackman · butter · buttord · cheb2ord · conv · conv2 · deconv · downsample · envelope · filter · filtfilt · fir1 · freqz · gauspuls · hamming · hann · hilbert · periodogram · pulstran · pwelch · rectpuls · resample · sawtooth · sinc · spectrogram · square · tripuls · unwrap · upsample · zplane
Factor
chol · decomposition · decomposition.ctranspose · decomposition.mldivide · decomposition.mrdivide · decomposition.mtimes · decomposition.rdivide · decomposition.subsref · decomposition.times · decomposition.uminus · decomposition.uplus · eig · eigs · gmres · ichol · ilu · lu · ordeig · ordqz · ordschur · pcg · qr · qz · rsf2csf · schur · svd
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Interpolation
griddedInterpolant · griddedInterpolant.subsref · interp1 · interp1q · interp2 · pchip · ppval · spline
Integer Division
Source you can read
Every RunMat function's source is public. Read exactly how optimvar is executed, line by line, in Rust.
- View the source for optimvar in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.