### Running the CVX Quick Start Script Source: https://cvxr.com/cvx/doc/quickstart.html Navigate to the CVX examples directory and run the `quickstart` script to follow along with the examples. ```matlab cd D:\Matlab\cvx\examples quickstart ``` -------------------------------- ### Install CVX Professional License Source: https://cvxr.com/cvx/doc/_sources/install.txt When installing CVX, include the path to your license file as an argument to `cvx_setup` to install a CVX Professional license. This example is for Mac. ```matlab cd ~/MATLAB/cvx cvx_setup ~/licenses/cvx_license.mat ``` -------------------------------- ### Run cvx_setup on Windows Source: https://cvxr.com/cvx/doc/_sources/install.txt Navigate to the CVX directory and run the setup command in MATLAB. This is for Windows users. ```matlab cd C:\personal\cvx cvx_setup ``` -------------------------------- ### Run CVX Setup Script Source: https://cvxr.com/cvx/doc/_sources/funcref.txt The setup script used to install and configure CVX. ```matlab cvx_setup ``` -------------------------------- ### Install CVX with Professional License Source: https://cvxr.com/cvx/doc/install.html Install CVX with a professional license by providing the license file path to the setup command. ```matlab cd ~/MATLAB/cvx cvx_setup ~/licenses/cvx_license.mat ``` -------------------------------- ### Run cvx_setup on Linux/Mac Source: https://cvxr.com/cvx/doc/_sources/install.txt Navigate to the CVX directory and run the setup command in MATLAB. This is for Linux or Mac users. ```matlab cd ~/MATLAB/cvx cvx_setup ``` -------------------------------- ### Get CVX Installation Directory Source: https://cvxr.com/cvx/doc/_sources/funcref.txt Returns the directory where CVX is installed. ```matlab cvx_where ``` -------------------------------- ### Begin CVX Model Source: https://cvxr.com/cvx/doc/_sources/funcref.txt Starts a new CVX model. If a model is already in progress, it will issue a warning and clear it. ```matlab cvx_begin ``` -------------------------------- ### Begin CVX Model Quietly Source: https://cvxr.com/cvx/doc/_sources/solver.txt Start a CVX model while suppressing screen output from the solver. This is useful for integrating CVX into larger algorithms. ```matlab cvx_begin quiet ... cvx_end ``` -------------------------------- ### Objective with log_det and trace Source: https://cvxr.com/cvx/doc/advanced.html This example shows an objective function that cannot be simplified by removing the logarithm from log_det due to the presence of other terms like trace(C*X). ```matlab minimize( log_det(X) + trace(C*X) ) ``` -------------------------------- ### Minimize L1 Norm using linprog (MATLAB) Source: https://cvxr.com/cvx/doc/_sources/quickstart.txt This snippet shows the manual setup for minimizing the L1 norm using MATLAB's linprog function. It defines the objective function, equality constraints, and bounds. ```matlab f = [ zeros(n,1); ones(m,1); ones(m,1) ]; Aeq = [ A, -eye(m), +eye(m) ]; lb = [ -Inf(n,1); zeros(m,1); zeros(m,1) ]; xzz = linprog(f,[],[],Aeq,b,lb,[]); x_l1 = xzz(1:n,:) - xzz(n+1:end,:); ``` -------------------------------- ### Switch Between Multiple Gurobi Versions Source: https://cvxr.com/cvx/doc/_sources/gurobi.txt If multiple Gurobi versions are installed, CVX appends a numeral to the solver name, allowing you to switch between them. Use these commands to select specific versions. ```matlab cvx_solver gurobi cvx_solver gurobi_2 cvx_solver gurobi_3 ``` -------------------------------- ### Implementing a Convex Function via DCP Rules Source: https://cvxr.com/cvx/doc/_sources/advanced.txt This example shows how to define a new convex function, 'deadzone', within CVX by composing existing DCP-compliant functions like 'abs' and 'max'. The function works both inside and outside CVX if its operations adhere to DCP rules. ```matlab function y = deadzone( x ) y = max( abs( x ) - 1, 0 ) ``` -------------------------------- ### Declare and Use Indexed Dual Variables Source: https://cvxr.com/cvx/doc/_sources/advanced.txt Model a semidefinite program using indexed dual variables to handle a variable number of constraints. This example uses a for loop to assign dual variables. ```matlab cvx_begin variable X( n, n ) symmetric minimize( ( n - 1 : -1 : 0 ) * diag( X ) ); for k = 0 : n-1, sum( diag( X, k ) ) == b( k+1 ); end X == semidefinite(n); cvx_end ``` -------------------------------- ### CVX Constraint: Valid Convex Form Source: https://cvxr.com/cvx/doc/_sources/support.txt This example shows a mathematically equivalent form of a constraint that is accepted by CVX because it adheres to the Disciplined Convex Programming (DCP) ruleset. It demonstrates how to correctly express convex constraints. ```matlab sqrt( sum( square( x ) ) ) <= 1 ``` -------------------------------- ### Set Gurobi as Default Solver Source: https://cvxr.com/cvx/doc/_sources/gurobi.txt Change the active solver to Gurobi for the current session and save this preference for future MATLAB sessions. This ensures Gurobi is automatically selected when CVX starts. ```matlab cvx_solver gurobi cvx_save_prefs ``` -------------------------------- ### Get Current Precision Levels Source: https://cvxr.com/cvx/doc/solver.html Call `cvx_precision` with no arguments to print the current tolerance levels to the screen, or as a function to return them in a 3-element row vector. ```matlab cvx_precision ``` ```matlab tol = cvx_precision; ``` -------------------------------- ### List Custom Settings for All Solvers Source: https://cvxr.com/cvx/doc/solver.html This command displays all custom settings that have been provided for every solver supported by CVX. It is useful for a comprehensive overview of all potential customizations. ```matlab cvx_solver_settings -all ``` -------------------------------- ### Clear CVX Model Source: https://cvxr.com/cvx/doc/_sources/funcref.txt Clears any model being constructed. Useful when an error has been made and it is necessary to start from the beginning. ```matlab cvx_clear ``` -------------------------------- ### List Custom Settings for Active Solver Source: https://cvxr.com/cvx/doc/solver.html Use this command to view the custom settings currently applied to the active solver. This helps in understanding the current configuration before making changes. ```matlab cvx_solver_settings ``` -------------------------------- ### Basic Semidefinite Program in CVX Source: https://cvxr.com/cvx/doc/advanced.html This snippet demonstrates a basic semidefinite program in CVX without indexed dual variables. It sets up the optimization variable, objective function, and constraints. ```matlab cvx_begin variable X( n, n ) symmetric minimize( ( n - 1 : -1 : 0 ) * diag( X ) ); for k = 0 : n-1, sum( diag( X, k ) ) == b( k+1 ); end X == semidefinite(n); cvx_end ``` -------------------------------- ### Maximize Norm Error in CVX Source: https://cvxr.com/cvx/doc/quickstart.html Example of an invalid CVX specification attempting to maximize a convex function, resulting in an error. ```matlab maximize( norm(A*x-b) ); ``` -------------------------------- ### Convexity of Square Root of Sum of Squares Source: https://cvxr.com/cvx/doc/_sources/dcp.txt An example of an expression that is convex but may not satisfy the DCP rules directly, illustrating the limitations of the ruleset. ```Python sqrt( sum( square( x ) ) ) ``` -------------------------------- ### List Custom Solver Settings Source: https://cvxr.com/cvx/doc/_sources/solver.txt Displays custom settings for the active CVX solver or all solvers. ```matlab cvx_solver_settings ``` ```matlab cvx_solver_settings -all ``` -------------------------------- ### Retrieve Gurobi License Key Source: https://cvxr.com/cvx/doc/_sources/gurobi.txt Use this command to retrieve your Gurobi license key. This is a wrapper around Gurobi's own grbgetkey script. Ensure you are connected to your university network if you are an academic user. ```matlab cvx_grbgetkey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ``` -------------------------------- ### Get current CVXPY precision levels Source: https://cvxr.com/cvx/doc/_sources/solver.txt Call `cvx_precision` without arguments to print the current tolerance levels to the screen. This is useful for inspecting the active precision settings. ```matlab cvx_precision ``` -------------------------------- ### Using Assignment for Intermediate Calculations Source: https://cvxr.com/cvx/doc/basics.html Demonstrates using assignment to store an intermediate affine expression 'z' which is then used in multiple constraints. ```matlab variables x y z = 2 * x - y; square( z ) <= 3; quad_over_lin( x, z ) <= 1; ``` -------------------------------- ### Maximize Volume Box using GP Mode Source: https://cvxr.com/cvx/doc/_sources/gp.txt This snippet demonstrates how to define a geometric program to find the maximum volume of a box subject to area and ratio constraints using CVX's GP mode. It requires `cvx_begin gp` to activate the mode. ```matlab cvx_begin gp variables w h d maximize( w * h * d ) subject to 2*(h*w+h*d) <= Awall; w*d <= Afloor; alpha <= h/w >= beta; gamma <= d/w <= delta; cvx_end ``` -------------------------------- ### CVX Constraint Violation Example Source: https://cvxr.com/cvx/doc/support.html This code demonstrates a common scenario where a constraint is rejected by CVX due to violating the Disciplined Convex Programming (DCP) ruleset. The error indicates an invalid comparison between a convex expression and a real constant. ```matlab variable x(10) norm(x) == 1 ``` -------------------------------- ### Retrieve Gurobi License Key Source: https://cvxr.com/cvx/doc/gurobi.html Use this command to retrieve your Gurobi license key. Ensure you replace the placeholder with your actual 32-digit Gurobi key. This step is crucial for enabling Gurobi within CVX. ```bash cvx_grbgetkey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ``` -------------------------------- ### Using Assignments for Intermediate Calculations Source: https://cvxr.com/cvx/doc/_sources/basics.txt Demonstrates how to use assignments to store intermediate affine expressions that are then used in multiple constraints. This is useful for breaking down complex expressions. ```cvxpy variables x y z = 2 * x - y; square( z ) <= 3; quad_over_lin( x, z ) <= 1; ``` -------------------------------- ### Chebyshev Norm Minimization with MATLAB linprog Source: https://cvxr.com/cvx/doc/_sources/quickstart.txt Solves the Chebyshev (infinity) norm minimization problem using MATLAB's linprog. This requires reformulating the problem into a linear program. ```matlab f = [ zeros(n,1); 1 ]; Ane = [ +A, -ones(m,1) ; ... -A, -ones(m,1) ]; bne = [ +b; -b ]; xt = linprog(f,Ane,bne); x_cheb = xt(1:n,:); ``` -------------------------------- ### Display CVX Version Information Source: https://cvxr.com/cvx/doc/_sources/funcref.txt Prints information about the current versions of CVX, Matlab, and the operating system. When submitting bug reports, please include the output of this command. ```matlab cvx_version ``` -------------------------------- ### Convexity of Affine Composition Source: https://cvxr.com/cvx/doc/_sources/dcp.txt Demonstrates how an affine transformation of a convex function results in a convex function. This is a fundamental rule in DCP. ```Python square( a' * x + b ) ``` -------------------------------- ### Commercial Solver Directories Source: https://cvxr.com/cvx/doc/license.html Files within these directories are licensed by third-party providers for inclusion in the CVX package and are subject to the CVX Professional License. ```text gurobi/ mosek/ ``` -------------------------------- ### Minimize L2 Norm with Equality and L-infinity Constraints using CVX Source: https://cvxr.com/cvx/doc/_sources/quickstart.txt This snippet demonstrates minimizing the L2 norm while satisfying an equality constraint and an L-infinity norm constraint. CVX converts this into an SOCP. ```matlab cvx_begin variable x(n); minimize( norm(A*x-b) ); subject to C*x == d; norm(x,Inf) <= 1; cvx_end ``` -------------------------------- ### Constrain Matrix with Semidefinite Cone using '' Source: https://cvxr.com/cvx/doc/basics.html An alternative syntax using the '' pseudo-operator to constrain a matrix to the semidefinite cone. This is equivalent to using the equality constraint. ```matlab X semidefinite(n); ``` -------------------------------- ### Create a New Custom Solver Setting Source: https://cvxr.com/cvx/doc/solver.html This syntax allows you to define a new custom setting for the current solver. The `{name}` must be a valid MATLAB variable name, and `{value}` can be any valid MATLAB object. CVX does not validate the value itself. ```matlab cvx_solver_settings( '{name}', {value} ) ``` -------------------------------- ### BibTeX Citation for Graph Implementations Paper Source: https://cvxr.com/cvx/doc/_sources/citing.txt This BibTeX entry is for citing the paper on graph implementations for nonsmooth convex programs by M. Grant and S. Boyd. It includes details about the book, series, editors, publisher, pages, year, and a URL. ```bibtex @incollection{gb08, author = {M. Grant and S. Boyd}, title = {Graph implementations for nonsmooth convex programs}, booktitle = {Recent Advances in Learning and Control}, series = {Lecture Notes in Control and Information Sciences}, editor = {V. Blondel and S. Boyd and H. Kimura}, publisher = {Springer-Verlag Limited}, pages = {95--110}, year = 2008, note = {\url{http://stanford.edu/~boyd/graph_dcp.html}} } ``` -------------------------------- ### Formulating a Positive Semidefinite Constraint Source: https://cvxr.com/cvx/doc/_sources/advanced.txt This snippet demonstrates how to express a problem of making a matrix positive semidefinite by adding a diagonal matrix. It highlights a common pitfall with unsymmetric matrices leading to 'Overdetermined' status. ```matlab n = size(W,1); cvx_begin variable D(n,n) diagonal; minimize( trace( D ) ); subject to W + D == semidefinite(n); cvx_end ``` -------------------------------- ### Sum of Convex and Concave Functions Source: https://cvxr.com/cvx/doc/dcp.html Illustrates the sum of a convex function (square) and a concave function (sqrt). CVX classifies these based on the properties of the sum. ```python sum( square( x ) ) ``` ```python sum( sqrt( x ) ) ``` -------------------------------- ### Minimize L1 Norm using CVX Source: https://cvxr.com/cvx/doc/_sources/quickstart.txt This snippet demonstrates how to minimize the L1 norm using CVX. CVX automatically transforms this into an LP. ```matlab cvx_begin variable x(n) minimize( norm(A*x-b,1) ) cvx_end ``` -------------------------------- ### Set MOSEK as Default Solver Source: https://cvxr.com/cvx/doc/_sources/mosek.txt Change the active solver to MOSEK for the current session and save this preference for future MATLAB sessions. ```matlab cvx_solver mosek cvx_save_prefs ``` -------------------------------- ### SDP Mode LMI Constraint in CVX Source: https://cvxr.com/cvx/doc/sdp.html This snippet demonstrates how the same Hermitian semidefinite constraint can be expressed more concisely using CVX's SDP mode with the '>=' operator. ```matlab cvx_begin sdp variable Z(n,n) hermitian toeplitz dual variable Q minimize( norm( Z - P, 'fro' ) ) Z >= 0 : Q; cvx_end ``` -------------------------------- ### Product of Inverses Source: https://cvxr.com/cvx/doc/_sources/funcref.txt Calculates the product of the inverses of elements in x (product(x_i^-1)). Returns +Inf if x is not positive. ```python prod_inv(x) ``` -------------------------------- ### Equality Constraint with Non-Affine Sides Source: https://cvxr.com/cvx/doc/_sources/quickstart.txt Demonstrates an equality constraint where both sides are non-affine, which CVX rejects with an error. ```matlab norm(x,Inf) == 1; ``` -------------------------------- ### Chebyshev Norm Minimization with CVX Source: https://cvxr.com/cvx/doc/_sources/quickstart.txt Solves the Chebyshev (infinity) norm minimization problem directly using CVX. CVX handles the underlying transformation automatically. ```matlab cvx_begin variable x(n) minimize( norm(A*x-b,Inf) ) cvx_end ``` -------------------------------- ### Save CVX Preferences Source: https://cvxr.com/cvx/doc/solver.html This command saves the current solver choice and other settings (like cvx_expert, cvx_power_warning, cvx_precision) permanently. Ensure preferences are set correctly before issuing this command. ```matlab cvx_save_prefs ``` -------------------------------- ### Display Current Solver Source: https://cvxr.com/cvx/doc/_sources/solver.txt Use this command to check which solver is currently selected in CVX. ```matlab cvx_solver ``` -------------------------------- ### Control CVX Screen Output with cvx_quiet Source: https://cvxr.com/cvx/doc/solver.html Use 'cvx_quiet true' to suppress output or 'cvx_quiet false' to restore it. Entering the command with no arguments returns the current setting. Changes made outside a model affect subsequent models. ```matlab cvx_quiet true ``` ```matlab cvx_quiet false ``` -------------------------------- ### Declare Multiple Variables Source: https://cvxr.com/cvx/doc/basics.html Use the 'variables' command to declare multiple variables at once. This is a shorthand for declaring several variables with basic properties. ```matlab variables x1 x2 x3 y1(10) y2(10,10,10); ``` -------------------------------- ### Generate Least Squares Data in Matlab Source: https://cvxr.com/cvx/doc/quickstart.html Sets up the dimensions and generates random matrices A and b for a least-squares problem. ```matlab m = 16; n = 8; A = randn(m,n); b = randn(m,1); ``` -------------------------------- ### Standard LMI Constraint in CVX Source: https://cvxr.com/cvx/doc/sdp.html This snippet shows a standard way to express a Hermitian semidefinite constraint in CVX using the `hermitian_semidefinite` function. ```matlab cvx_begin variable Z(n,n) hermitian toeplitz dual variable Q minimize( norm( Z - P, 'fro' ) ) Z == hermitian_semidefinite( n ) : Q; cvx_end ``` -------------------------------- ### Corrected Array Assignment with Expression Holder Source: https://cvxr.com/cvx/doc/basics.html Shows the correct way to build an array of CVX expressions by first declaring 'x' as an expression holder. ```matlab variable u(9); expression x(10); x(1) = 1; for k = 1 : 9, x(k+1) = sqrt( x(k) + u(k) ); end ``` -------------------------------- ### Original Log-Determinant Objective Source: https://cvxr.com/cvx/doc/_sources/advanced.txt This represents the standard way a log-determinant maximization problem might be initially formulated. ```matlab minimize( log_det(X) ) ``` -------------------------------- ### L1 Norm Minimization with MATLAB linprog Source: https://cvxr.com/cvx/doc/_sources/quickstart.txt Solves the L1 norm minimization problem using MATLAB's linprog. This requires reformulating the problem into a linear program. ```matlab f = [ zeros(n,1); ones(m,1) ]; Ane = [ +A, -eye(m); -A, -eye(m) ]; bne = [ +b; -b ]; xt = linprog(f,Ane,bne); x_l1 = xt(1:n,:); ``` -------------------------------- ### Equivalent Constraint using geo_mean Source: https://cvxr.com/cvx/doc/advanced.html This snippet shows an alternative formulation for a sum_log constraint using the geo_mean function. It is preferred when possible to avoid functions requiring successive approximation. ```matlab sum_log(x) >= 10 ``` ```matlab geo_mean(x) >= 10^(1/length(x)) ``` -------------------------------- ### Bound-Constrained Least Squares with MATLAB quadprog Source: https://cvxr.com/cvx/doc/_sources/quickstart.txt Solves a least-squares problem with upper and lower bounds on x using MATLAB's quadprog. This approach requires transforming the problem into a quadratic program. ```matlab bnds = randn(n,2); l = min( bnds, [], 2 ); u = max( bnds, [], 2 ); ``` ```matlab x_qp = quadprog( 2*A'*A, -2*A'*b, [], [], [], [], l, u ); ``` -------------------------------- ### Declare Integer and Binary Variables Source: https://cvxr.com/cvx/doc/basics.html Declare variables for Mixed-Integer Convex Programming (MIDCPs). 'integer' restricts the variable to integer values, and 'binary' restricts it to 0 or 1. ```matlab variable p(10) integer variable q binary ``` -------------------------------- ### Correcting Overdetermined Constraints with Symmetric Part Source: https://cvxr.com/cvx/doc/_sources/advanced.txt This snippet shows the corrected formulation for the positive semidefinite problem, using the 'sym' function to ensure the constraint is well-posed when the original matrix W is unsymmetric. ```matlab sym( W ) + D == semidefinite(n); ``` -------------------------------- ### Declare and Associate Dual Variable with Inequality Constraint Source: https://cvxr.com/cvx/doc/basics.html Declare a dual variable `y` and associate it with the inequality constraint `A * x <= b` in an LP. This syntax is used to retrieve sensitivity information for the constraint. ```matlab n = size(A,2); cvx_begin variable x(n); dual variable y; minimize( c' * x ); subject to y : A * x <= b; cvx_end ``` -------------------------------- ### Define Matrices for Constraints (MATLAB) Source: https://cvxr.com/cvx/doc/_sources/quickstart.txt This snippet shows how to define matrices C and d in MATLAB, which will be used for adding constraints to a CVX problem. ```matlab p = 4; C = randn(p,n); d = randn(p,1); ``` -------------------------------- ### Using square_pos for Monotonic Compositions Source: https://cvxr.com/cvx/doc/_sources/dcp.txt CVX rejects compositions where a non-monotonic function is applied to a convex function. Using monotonic extensions like `square_pos` can resolve this, as shown when the argument is always positive. ```python square_pos( square( x ) + 1 ) ``` -------------------------------- ### Quadratic Over Linear Source: https://cvxr.com/cvx/doc/_sources/funcref.txt Computes x'x/y for real x and y > 0, or x*x/y for complex x and y > 0. Adds a constraint y > 0 in CVX specification. ```python quad_over_lin(x,y) ``` -------------------------------- ### BibTeX Citation for CVX Source: https://cvxr.com/cvx/doc/_sources/citing.txt Use this BibTeX entry when citing the CVX software in academic publications. It includes author, title, how it was published, and the year. ```bibtex @misc{cvx, author = {CVX Research, Inc.}, title = {{CVX}: Matlab Software for Disciplined Convex Programming, version 2.0}, howpublished = {\url{https://cvxr.com/cvx}}, month = aug, year = 2012 } ``` -------------------------------- ### CVX Professional License Files Source: https://cvxr.com/cvx/doc/license.html These files are governed by the CVX Professional License. Their presence in a package subjects the entire package to this license. ```text shims/cvx_mosek.p shims/cvx_gurobi.p cvx_license.p ``` -------------------------------- ### Create Expression Holders Source: https://cvxr.com/cvx/doc/_sources/funcref.txt Creates one or more expression holders. ```matlab expression ``` ```matlab expressions ``` -------------------------------- ### Set CVX Solver Settings Source: https://cvxr.com/cvx/doc/_sources/funcref.txt Allows the user to deliver advanced, solver-specific settings to the solver that CVX does not otherwise support. ```matlab cvx_solver_settings ``` -------------------------------- ### Clear All Custom Settings for All Solvers Source: https://cvxr.com/cvx/doc/solver.html This command removes all custom settings for all solvers. It is a comprehensive way to reset all solver configurations to their default states. ```matlab cvx_solver_settings -clearall ``` -------------------------------- ### Simplified Objective without log_det Source: https://cvxr.com/cvx/doc/advanced.html This demonstrates how to simplify a log_det objective by removing the logarithm and solving for the root-n determinant instead. The log_det value can be computed post-optimization. ```matlab minimize( log_det(X) ) ``` ```matlab minimize( n*log(det_rootn(X)) ) ``` ```matlab minimize( det_rootn(X) ) ``` -------------------------------- ### Control Solver Screen Output Source: https://cvxr.com/cvx/doc/_sources/solver.txt Toggle screen output for the solver. 'cvx_quiet true' suppresses output, while 'cvx_quiet false' restores it. Commands issued within a cvx_begin/cvx_end block affect only that model. ```matlab cvx_quiet true ``` ```matlab cvx_quiet false ```