Basics{Tutorials}Thefollowingpieceofcodeintroducesessentiallyeverythingyoueverneedtolearn.Itdefinesvariables,constraints,objectives,options,checksresultandextractssolution(NotethatthecodespecifiesthesolvertoCPLEX.Ifyoudon'thaveCPLEXinstalled,simplyremovethatsolverselectioninthedefinitionofoptions.YALMIPwillpicksomeothersolveritfindsinstalled)%Definevariablesx=sdpvar(2,1);%DefineconstraintsandobjectiveConstraints=[sum(x)=1,x(1)==0,x(2)=0.5];Objective=x'*x+norm(x);%SetsomeoptionsforYALMIPandsolveroptions=sdpsettings('verbose',1,'solver','cplex','cplex.qpmethod',1);%Solvetheproblemsol=optimize(Constraints,Objective,options);%Analyzeerrorflagsifsol.problem==0%Extractanddisplayvaluesolution=value(x)elsedisplay('Hmm,somethingwentwrong!');sol.infoyalmiperror(sol.problem)end[$[GetCode]]Havingseenthat,letusstartfromthebeginning.YALMIPssymbolicvariableThemostimportantcommandinYALMIPissdpvar.Thiscommandisusedtothedefinedecisionvariables.Todefineamatrix(orscalar)Pwithnrowsandmcolumns,wewriteP=sdpvar(n,m)[$[GetCode]]Asquarematrixissymmetricbydefault!.Toobtainafullyparameterized(i.e.notnecessarilysymmetric)squarematrix,athirdargumentisneeded.P=sdpvar(3,3,'full')[$[GetCode]]Thethirdargumentcanbeusedtoobtainanumberofpre-definedtypesofvariables,suchasToeplitz,Hankel,diagonal,symmetricandskew-symmetricmatrices.Seethehelptextonsdpvarfordetails.Alternatively,theassociatedMATLABcommandscanbeappliedtoavector.x=sdpvar(n,1);D=diag(x);%DiagonalmatrixH=hankel(x);%HankelmatrixT=toeplitz(x);%Hankelmatrix[$[GetCode]]Scalarscanbedefinedinthreedifferentways.x=sdpvar(1,1);y=sdpvar(1,1);x=sdpvar(1);y=sdpvar(1);sdpvarxy[$[GetCode]]NotethatduetoabuginMATLAB,thelastcommand-linesyntaxfailsinsomecases(insidefunctions),ifthevariablenameisthesameassomebuilt-infunctionorvariable(i,j,e,beta,gamma).ThesdpvarobjectsaremanipulatedinMATLABasanyothervariableandmostfunctionsareoverloaded.Hence,thefollowingcommandsarevalidP=sdpvar(3,3)+diag(sdpvar(3,1));X=[PP;Peye(length(P))]+2*trace(P);Y=X+sum(sum(P*rand(length(P))))+P(end,end)+hankel(X(:,1));[$[GetCode]]Insomesituations,codingissimplifiedwithamulti-dimensionalvariable.ThisissupportedinYALMIPwithtwodifferentconstructs,cellarraysandmulti-dimensionalsdpvarobjects.Thecellarrayisnothingbutanabstractionofthefollowingcodefori=1:5X{i}=sdpvar(2,3);end[$[GetCode]]Byusingvectordimensionsinsdpvar,thesamecellarraycanbesetupasfollowsX=sdpvar([22222],[33333]);[$[GetCode]]ThecellarraycannowbeusedasusualinMATLAB.ThedrawbackwiththeapproachaboveisthatthevariableXnotcanbeuseddirectly,asastandardsdpvarobject.Asanalternative,acompletelygeneralmulti-dimensionalsdpvarisavailable.Wecancreateanessentiallyequivalentobjectwiththiscall.X=sdpvar(2,3,5);[$[GetCode]]Thedifferenceisthatwecanoperatedirectlyonthisobject,usingstandardMATLABcode.Y=sum(X,3)X((:,:,2)[$[GetCode]]Notethatthetwofirstslicesaresymmetric(ifthetwofirstdimensionsarethesame),accordingtostandardYALMIPsyntax.Tocreateafullyparamterizedhigher-dimensional,usetrailingflagsasinthestandardcase.X=sdpvar(2,2,2,2,'full');[$[GetCode]]Foranillustrationofmulti/dimensionalvariables,checkouttheSudokuexample.ConstraintsTodefineacollectionofconstraints,wesimplydefineandconcatenatethem.Themeaningofaconstraintiscontext-dependent.Iftheleft-handsideandright-handsideareHermitian,theconstraintisinterpretedintermsofpositivedefiniteness,otherwiseelement-wise.Hence,declaringasymmetricmatrixandapositivedefinitenessconstraintisdonewithn=3;P=sdpvar(n,n);C=[P=0];[$[GetCode]]whileasymmetricmatrixwithpositiveelementsisdefinedwith,e.g.,P=sdpvar(n,n);C=[P(:)=0];[$[GetCode]]Notethatthisdefinestheoff-diagnoalconstraintstwice.AgoodSDPsolverwillperhapsdetectthisduringpreprocessingandreducethemodel,butwecanof-coursedefineonlytheuniqueelementsmanuallyusingstandardMATLABcodeC=[triu(P)=0];[$[GetCode]]orC=[P(find(triu(ones(n))))=0];[$[GetCode]]Accordingtotherulesabove,anon-squarematrix(orgenerallyanon-symmetric)withpositiveelementscanbedefinedusingthe=operatorimmediatelyP=sdpvar(n,2*n);C=[P=0];[$[GetCode]]andsocanafullyparameterizedsquarematrixwithpositiveelementsP=sdpvar(n,n,'full');C=[P=0];[$[GetCode]]Alistofseveralconstraintsisdefinedbyjustaddingor,alternatively,concatenatingthem.P=sdpvar(n,n);C=[P=0]+[P(1,1)=2];C=[P=0,P(1,1)=2];[$[GetCode]]Ofcourse,theinvolvedexpressionscanbearbitrarysdpvarobjects,andequalityconstraints(==)canbedefined,aswellasconstraintsusing=.C=[P=0,P(1,1)=2,sum(sum(P))==10];[$[GetCode]]Aconvenientwaytodefineseveralconstraintsistousedouble-sidedconstraints.F=[0=P(1,1)=2];[$[GetCode]]Afterhavingdefinedvariablesandconstraints,youarereadytosolveproblems.Checkouttheremainingtutorialstolearnthis.