CVX配合MATLAB的使用心得

本人最近自学CVX,发现在CVX中,很多MATLAB自带的函直接调用并不能正常运行,仔细模仿别人写的函数,发现很多常用函数在cvx中,被重写了,更方便使用。

具体有哪些函数,请查阅cvx工具包中doc文件夹下的funcref文件,为方便大家浏览,我将其黏贴在文尾。

Reference guide

In this section we describe each operator, function, set, and command that you are likely to encounter in CVX. In some cases, limitations of the underlying solver place certain restrictions or caveats on their use:

  • Functions marked with a dagger (†) are not supported natively by the solvers that CVX uses. They are handled using a successive approximation method which makes multiple calls to the underlying solver, achieving the same final precision. If you use one of these functions, you will be warned that successive approximation will be used. This technique is discussed further in The successive approximation method. As this section discusses, this is an experimental approach that works well in many cases, but cannot be guaranteed.
  • Functions involving powers (e.g., x^p) and pp -norms (e.g., norm(x,p)) are marked with a double dagger (‡). CVX represents these functions exactly when pp is a rational number. For irrational values of p, a nearby rational is selected instead. See Power functions and p-norms for details on how both cases are handled.

Arithmetic operators

Matlab’s standard arithmetic operations for addition +, subtraction -, multiplication, * .*, division / ./ \ .\, and exponentiation ^ .^ have been overloaded to work in CVX whenever appropriate—that is, whenever their use is consistent with both standard mathematical and Matlab conventions and the DCP ruleset. For example:

  • Two CVX expressions can be added together if they are of the same dimension (or one is scalar) and have the same curvature (i.e., both are convex, concave, or affine).
  • A CVX expression can be multiplied or divided by a scalar constant. If the constant is positive, the curvature is preserved; if it is negative, curvature is reversed.
  • An affine column vector CVX expression can be multiplied by a constant matrix of appropriate dimensions; or it can be left-divided by a non-singular constant matrix of appropriate dimension.

Numerous other combinations are possible, of course. The use of the exponentiation operators ^ .^ are somewhat limited; see the definitions of power in Nonlinear below.

Matlab’s basic matrix manipulation and arithmetic operations have been extended to work with CVX expressions as well, including:

  • Concatenation: [ A, B ; C, D ]
  • Indexing: x(n+1:end), X([3,4],:), etc.
  • Indexed assignment, including deletion: y(2:4) = 1, Z(1:4,:) = [], etc.
  • Transpose and conjugate transpose: Z.', y'

Built-in functions

Linear

A number of Matlab’s basic linear and bilinear functions either work automatically with cvx expressions or have been extended to do so, including: conj, conv, cumsum, diag, dot, find, fliplr, flipud, flipdim, horzcat, hankel, ipermute, kron, mean, permute, repmat, reshape, rot90, sparse, sum, trace, tril, triu, toeplitz, vertcat.

Most should behave identically with CVX expressions as they do with numeric expressions. Those that perform some sort of summation, such as cumsum, sum, or multiplication, such as conv, dot or kron, can only be used in accordance with the disciplined convex programming rules. For example, kron(X,Y) is valid only if either X or Y is constant; and trace(Z) is valid only if the elements along the diagonal have the same curvature.

Nonlinear

abs

absolute value for real and complex arrays. Convex.

† exp

exponential. Convex and nondecreasing.

† log

logarithm. Concave and nondecreasing.

max

maximum. Convex and nondecreasing.

min

minimum. Concave and nondecreasing.

norm

norms for real and complex vectors and matrices. Convex. Thus function follows the Matlab conventions closely. Thus the one-argument version norm(x) computes the 2-norm for vectors, and the 2-norm (maximum singular value) for matrices. The two-argument version norm(x,p) is supported as follows:

  • ‡ For vectors, all values p≥1p≥1 are accepted.
  • For matrices, p must be 1, 2, Inf, or 'Fro'.

polyval

polynomial evaluation. polyval(p,x), where p is a vector of length n, computes

p(1) * x.^(n-1) + p(2) * x.^(n-2) + ... + p(n-1) * x + p(n)

This function can be used in CVX in two ways:

  • If p is a variable and x is a constant, then polyval(x,p) computes a linear combination of the elements of p. The combination must satisfy the DCP rules for addition and scaling.
  • If p is a constant and x is a variable, then polyval(x,p) constructs a polynomial function of the variable x. The polynomial must be affine, convex, or concave, and x must be real and affine.

‡ power(x,p)

x^p and x.^p, where x is a real variable and and p is a real constant. For x^p, both x and p must be scalars. Only those values of p which can reasonably and unambiguously interpreted as convex or concave are accepted:

  • p=0p=0 . Constant. x.^p is treated as identically 1.
  • 0<p<10<p<1 . Concave. The argument xx must be concave (or affine), and is implicitly constrained to be nonnegative.
  • p=1p=1 . Affine. x.^p is simply x.
  • p∈{2,4,6,8,...}p∈{2,4,6,8,...} . Convex. Argument xx must be affine.
  • p>1p>1 , p∉{2,3,4,5,...}p∉{2,3,4,5,...} . Convex. Argument xx must be affine, and is implicitly constrained to be nonnegative.

Negative and odd integral values of pp are not permitted, but see the functions pow_p, pow_pos, and pow_abs in the next section for useful alternatives.

† power(p,x)

p.^x and p^x, where p is a real constant and x is a real variable. For p^x, both p and x must be scalars. Valid values of p include:

  • p∈{0,1}p∈{0,1} . Constant.
  • 0<p<10<p<1 . Convex and nonincreasing; x must be concave.
  • p>1p>1 . Convex and nondecreasing; x must be convex.

Negative values of p are not permitted.

std

Standard deviation. Convex.

sqrt

Square root. Implicitly constrains its argument to be nonnegative. Concave and nondecreasing.

var

Variance. Convex.

New functions

Even though these functions were developed specifically for CVX, they work outside of a CVX specification as well, when supplied with numeric arguments.

avg_abs_dev

The average absolute deviation about the mean μ(x)μ(x) of xx . Convex.

faad(x)=1n∑i=1n|xi−μ(x)|=1n∑i=1n∣∣xi−1n∑ixi∣∣=1n∥∥(I−1n11T)x∥∥1.faad(x)=1n∑i=1n|xi−μ(x)|=1n∑i=1n|xi−1n∑ixi|=1n‖(I−1n11T)x‖1.

avg_abs_dev_med

The average absolute deviation about the median m(x)m⁡(x) of xx . Convex.

faadm(x)=1n∑i=1n|xi−m(x)|=infy1n∑i=1n|xi−y|faadm(x)=1n∑i=1n|xi−m⁡(x)|=infy1n∑i=1n|xi−y|

berhu(x,M)

The reversed Huber function (hence, Berhu), defined as

fberhu(x,M)≜{|x|(|x|2+M2)/2M|x|≤M|x|≥Mfberhu(x,M)≜{|x||x|≤M(|x|2+M2)/2M|x|≥M

Convex. If MM is omitted, M=1M=1 is assumed; but if supplied, it must be a positive constant. Also callable with three arguments as berhu(x,M,t), which computes t+t*berhu(x/t,M), useful for concomitant scale estimation (see [Owen06]).

det_inv

determinant of inverse of a symmetric (or Hermitian) positive definite matrix, detX−1detX−1 , which is the same as the product of the inverses of the eigenvalues. When used inside a CVX specification, det_inv constrains the matrix to be symmetric (if real) or Hermitian (if complex) and positive semidefinite. When used with numerical arguments, det_inv returns +Inf if these constraints are not met. Convex.

det_rootn

nn -th root of the determinant of a semidefinite matrix, (detX)1/n(detX)1/n . When used inside a CVX specification, det_rootn constrains the matrix to be symmetric (if real) or Hermitian (if complex) and positive semidefinite. When used with numerical arguments, det_rootn returns -Inf if these constraints are not met. Concave.

det_root2n

the 2n2n -th root of the determinant of a semidefinite matrix; i.e., det_root2n(X)=sqrt(det_rootn(X)). Concave. Maintained solely for back-compatibility purposes.

† entr

the elementwise entropy function: entr(x)=-x.*log(x). Concave. Returns -Inf when called with a constant argument that has a negative entry.

geo_mean

the geometric mean of a vector, (∏nk=1xk)1/n(∏k=1nxk)1/n . When used inside a CVX specification, geo_mean constrains the elements of the vector to be nonnegative. When used with numerical arguments, geo_mean returns -Inf if any element is negative. Concave and increasing.

huber(x,M)

The Huber function, defined as

fhuber(x,M)≜{|x|22M|x|−M2|x|≤M|x|≥Mfhuber(x,M)≜{|x|2|x|≤M2M|x|−M2|x|≥M

Convex. If $x$ is a vector or array, the function is applied on an elementwise basis. If $M$ is omitted, then $M=1$ is assumed; but if it supplied, it must be a positive constant. Also callable as huber(x,M,t), which computes t+t*huber(x/t,M), useful for concomitant scale estimation (see [Owen06]).

huber_circ(x,M)

The circularly symmetric Huber function, defined as

fhuber\_circ(x,M)≜{∥x∥222M∥x∥2−M2∥x∥2≤M∥x∥2≥Mfhuber\_circ(x,M)≜{‖x‖22‖x‖2≤M2M‖x‖2−M2‖x‖2≥M

Convex. Same (and implemented) as huber_pos(norm(x),M).

huber_pos(x,M)

The same as the Huber function for nonnegative x; zero for negative x. Convex and nondecreasing.

inv_pos

The inverse of the positive portion, 1/max{x,0}1/max{x,0} . Inside CVX specification, imposes constraint that its argument is positive. Outside CVX specification, returns +∞+∞ if x≤0x≤0 . Convex and decreasing.

† kl_div

Kullback-Leibler distance:

fkl(x,y)≜⎧⎩⎨xlog(x/y)−x+y0+∞x,y>0x=y=0otherwisefkl(x,y)≜{xlog⁡(x/y)−x+yx,y>00x=y=0+∞otherwise

Convex. Outside CVX specification, returns +∞+∞ if arguments aren’t in the domain.

lambda_max

maximum eigenvalue of a real symmetric or complex Hermitian matrix. Inside CVX, imposes constraint that its argument is symmetric (if real) or Hermitian (if complex). Convex.

lambda_min

minimum eigenvalue of a real symmetric or complex Hermitian matrix. Inside CVX, imposes constraint that its argument is symmetric (if real) or Hermitian (if complex). Concave.

lambda_sum_largest(X,k)

sum of the largest kk values of a real symmetric or complex Hermitian matrix. Inside CVX, imposes constraint that its argument is symmetric (if real) or Hermitian (if complex). Convex.

lambda_sum_smallest(X,k)

sum of the smallest kk values of a real symmetric or complex Hermitian matrix. Inside CVX, imposes constraint that its argument is symmetric (if real) or Hermitian (if complex). Concave.

log_det

log of determinant of a positive definite matrix, logdet(X)log⁡det(X) . When used inside a CVX specification, log_det constrains its argument to be symmetric (if real) or Hermitian (if complex) and positive definite. With numerical argument, log_det returns -Inf if these constraints are not met. Concave.

‡ log_normcdf(x)

logarithm of cumulative distribution function of standard normal random variable. Concave and increasing. The current implementation is a fairly crude SDP-representable approximation, with modest accuracy over the interval [−4,4][−4,4] ; we intend to replace it with a much better approximation at some point.

† log_prod(x)

log∏ixilog⁡∏ixi if when xx is positive; −∞−∞ otherwise. Concave and nonincreasing. Equivalent to sum_log(x).

† log_sum_exp(x)

the logarithm of the sum of the elementwise exponentials of x. Convex and nondecreasing.

logsumexp_sdp

a polynomial approximation to the log-sum-exp function with global absolute accuracy. This can be used to estimate the log-sum-exp function without using the successive approximation method.

matrix_frac(x,Y)

matrix fractional function, xTY−1xxTY−1x . In CVX, imposes constraint that YY is symmetric (or Hermitian) and positive definite; outside CVX, returns +∞+∞ unless Y=YT≻0Y=YT≻0 . Convex.

norm_largest(x,k)

For real and complex vectors, returns the sum of the largest k magnitudes in the vector x. Convex.

norm_nuc(X)

The sum of the singular values of a real or complex matrix X. (This is the dual of the usual spectral matrix norm, i.e., the largest singular value.) Convex.

‡ norms(x,p,dim), norms_largest(x,k,dim)

Computes vector norms along a specified dimension of a matrix or N-d array. Useful for sum-of-norms and max-of-norms problems. Convex.

poly_env(p,x)

Computes the value of the convex or concave envelope of the polynomial described by p (in the polyval sense). p must be a real constant vector whose length n is 0, 1, 2, 3, or some other odd length; and x must be real and affine. The sign of the first nonzero element of p determines whether a convex (positive) or concave (negative) envelope is constructed. For example, consider the function p(x)≜(x2−1)2=x4−2x2+1p(x)≜(x2−1)2=x4−2x2+1 , depicted along with its convex envelope in the figure below.

The two coincide when |x|≥1|x|≥1 , but deviate when |x|<1|x|<1 . Attempting to call polyval([1,0,2,0,1],x) in a CVX model would yield an error, but a call to poly_env([1,0,2,0,1],x) yields a valid representation of the envelope. For convex or concave polynomials, this function produces the same result as polyval.

The polynomial function p(x)=x4−2x2+1p(x)=x4−2x2+1 and its convex envelope.

pos(x)

max{x,0}max{x,0} , for real xx . Convex and increasing.

‡ pow_abs(x,p)

|x|p|x|p for x∈Rx∈R or x∈Cx∈C and p≥1p≥1 . Convex.

‡ pow_pos(x,p)

max{x,0}pmax{x,0}p for x∈Rx∈R and p≥1p≥1 . Convex and nondecreasing.

‡ pow_p(x,p)

for x∈Rx∈R and real constant pp , computes nonnegative convex and concave branches of the power function:

p≤00<p≤1p≥1fp(x)≜{xp+∞x>0x≤0fp(x)≜{xp−∞x≥0x<0fp(x)≜{xp+∞x≥0x<0convex, nonincreasingconcave, nondecreasingconvex, nonmonotonicp≤0fp(x)≜{xpx>0+∞x≤0convex, nonincreasing0<p≤1fp(x)≜{xpx≥0−∞x<0concave, nondecreasingp≥1fp(x)≜{xpx≥0+∞x<0convex, nonmonotonic

prod_inv(x)

∏ix−1i∏ixi−1 when xx is positive; +∞+∞ otherwise. Convex and nonincreasing.

quad_form(x,P)

xTPxxTPx for real xx and symmetric PP , and xHPxxHPx for complex xx and Hermitian PP . Convex in xx for PP constant and positive semidefinite; concave in xx for PP constant and negative semidefinite.

Note

Quadratic functions such as quad_form, sum_square can often be replaced by the norm function without sacrificing equivalence. For numerical reasons, this alternate formulation is preferred. Please see Eliminating quadratic forms for more information.

quad_over_lin(x,y)

xTx/yxTx/y for x∈Rnx∈Rn , y>0y>0 ; for x∈Cnx∈Cn , y>0y>0 , x∗x/yx∗x/y . In CVX specification, adds constraint that y>0y>0 . Outside CVX specification, returns +∞+∞ if y≤0y≤0 . Convex, and decreasing in yy .

quad_pos_over_lin(x,y)

sum_square_pos( x )/y for x∈Rnx∈Rn , y>0y>0 . Convex, increasing in xx , and decreasing in yy .

† rel_entr(x)

Scalar relative entropy; rel_entr(x,y)=x.*log(x/y). Convex.

sigma_max

maximum singular value of real or complex matrix. Same as norm. Convex.

square

x2x2 for x∈Rx∈R . Convex.

square_abs

|x|2|x|2 for x∈Rx∈R or x∈Cx∈C .

square_pos

max{x,0}2max{x,0}2 for x∈Rx∈R . Convex and increasing.

sum_largest(x,k)

sum of the largest kk values, for real vector xx . Convex and nondecreasing.

† sum_log(x)

∑ilog(xi)∑ilog⁡(xi) when xx is positive; −∞−∞ otherwise. Concave and nondecreasing.

sum_smallest(x,k)

sum of the smallest kk values, i.e., equivalent to -sum_largest(-x,k). Concave and nondecreasing.

sum_square

Equivalent to sum(square(x)), but more efficient. Convex. Works only for real values.

sum_square_abs

Equivalent to sum(square_abs(x)), but more efficient. Convex.

sum_square_pos

Equivalent to sum(square_pos(x)), but more efficient. Works only for real values. Convex and increasing.

trace_inv(X)

trace of the inverse of an SPD matrix X, which is the same as the sum of the inverses of the eigenvalues. Convex. Outside of CVX, returns +Inf if argument is not positive definite.

trace_sqrtm(X)

trace of the matrix square root of a positive semidefinite matrix X. which is the same as the sum of the squareroots of the eigenvalues. Concave. Outside of CVX, returns +Inf if argument is not positive semidefinite.

Sets

CVX currently supports the following sets; in each case, n is a positive integer constant.

nonnegative(n)

Rn+≜{x∈Rn | xi≥0, i=1,2,…,n}R+n≜{x∈Rn | xi≥0, i=1,2,…,n}

simplex(n)

Rn1+≜{x∈Rn | xi≥0, i=1,2,…,n, ∑ixi=1}R1+n≜{x∈Rn | xi≥0, i=1,2,…,n, ∑ixi=1}

lorentz(n)

Qn≜{(x,y)∈Rn×R | ∥x∥2≤y}Qn≜{(x,y)∈Rn×R | ‖x‖2≤y}

rotated_lorentz(n)

Qnr≜{(x,y,z)∈Rn×R×R | ∥x∥2≤yz−−√, y,z≥0}Qrn≜{(x,y,z)∈Rn×R×R | ‖x‖2≤yz, y,z≥0}

complex_lorentz(n)

Qnc≜{(x,y)∈Cn×R | ∥x∥2≤y}Qcn≜{(x,y)∈Cn×R | ‖x‖2≤y}

rotated_complex_lorentz(n)

Qnrc≜{(x,y,z)∈Cn×R×R | ∥x∥2≤yz−−√, y,z≥0}Qrcn≜{(x,y,z)∈Cn×R×R | ‖x‖2≤yz, y,z≥0}

semidefinite(n)

Sn+≜{X∈Rn×n | X=XT, X⪰0}S+n≜{X∈Rn×n | X=XT, X⪰0}

hermitian_semidefinite(n)

Hn+≜{Z∈Cn×n | Z=ZH, Z⪰0}H+n≜{Z∈Cn×n | Z=ZH, Z⪰0}

nonneg_poly_coeffs(n)

The cone of all coefficients of nonnegative polynomials of degree nn ; nn must be even:

P+,n≜{p∈Rn[n+1] | ∑i=0npi+1xn−i≥0 ∀x∈R}P+,n≜{p∈Rn[n+1] | ∑i=0npi+1xn−i≥0 ∀x∈R}

convex_poly_coeffs(n)

The cone of all coefficients of convex polynomials of degree nn ; nn must be even:

P+,n≜{p∈Rn[n+1] | ∑i=0n−2(n−i)(n−i−1)pi+1xn−i−2≥0 ∀x∈R}P+,n≜{p∈Rn[n+1] | ∑i=0n−2(n−i)(n−i−1)pi+1xn−i−2≥0 ∀x∈R}

exp_cone

E≜cl{(x,y,z)∈R×R×R | y>0, yex/y≤z}E≜cl{(x,y,z)∈R×R×R | y>0, yex/y≤z}

geo_mean_cone(n)

Gn≜cl{(x,y)∈Rn×Rn×Rn | x≥0, (∏i=1nxi)1/n≥y}Gn≜cl{(x,y)∈Rn×Rn×Rn | x≥0, (∏i=1nxi)1/n≥y}

Commands

cvx_begin

Begins a new CVX model. If a model is already in progress, it will issue a warning and clear it. See cvx_begin and cvx_end for a full description, including the modifying keywords that control solver output, SDP mode, GDP mode, etc.

cvx_clear

Clears any model being constructed. Useful when an error has been made and it is necessary to start from the beginning. Whereas cvx_begin issues a warning if called with a model in progress, cvx_clear is silent.

cvx_end

Signals the end of a CVX model. In typical use, this instructs CVX to begin the solution process. See cvx_begin and cvx_end.

cvx_expert

Controls the issuance of warnings when models requiring the use of successive approximation are employed; see The successive approximation method more details.

cvx_power_warning

Controls if and when CVX issues warnings during the construction of models involving rational power functions (i.e., x^p, where x is a variable and p is a constant); see Power functions and p-norms.

cvx_precision

Controls solver precision; see Controlling precision.

cvx_quiet

Enables or disables screen output during the solution process; see Controlling screen output. Also see cvx_begin and cvx_end for the newer, preferred syntax cvx_begin quiet.

cvx_save_prefs

Saves the current states for cvx_expert, cvx_power_warning, cvx_precision, and cvx_solver to disk, so that their values are retained when quitting and re-starting MATLAB. The file is saved in MATLAB’s preference directory, which can be located by typing the prefdir command.

cvx_setup

The setup script used to install and configure CVX; see Installation.

cvx_solver

Selects the solver to be employed when solving CVX models; see Selecting a solver.

cvx_solver_settings

Allows the user to deliver advanced, solver-specific settings to the solver that CVX does not otherwise support; see Advanced solver settings.

cvx_version

Prints information about the current versions of CVX, Matlab, and the operating system. When submitting bug reports, please include the output of this command.

cvx_where

Returns the directory where CVX is installed.

dual variable, dual variables

Creates one or more dual variables to be connected to constraints in the current model; see Dual variables.

expression, expressions

Creates one or more expression holders; see Assignment and expression holders.

maximise, maximize

Specifies a maximization objective; see Objective functions.

minimise, minimize

Specifies a minimization objective; see Objective functions.

variable, variables

Creates one or more variables for use in the current CVX model; see Variables.

  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值