MATLAB优化工具.docx
《MATLAB优化工具.docx》由会员分享,可在线阅读,更多相关《MATLAB优化工具.docx(17页珍藏版)》请在冰豆网上搜索。
MATLAB优化工具
Findminimumofconstrainednonlinearmultivariablefunction
EquationFindstheminimumofaproblemspecifiedby
bandbeqarevectors,AandAeqarematrices,c(x)andceq(x)arefunctionsthatreturnvectors,andf(x)isafunctionthatreturnsascalar.f(x),c(x),andceq(x)canbenonlinearfunctions
.x,lb,andubcanbepassedasvectorsormatrices;seeMatrixArguments.
Syntax
x=fmincon(fun,x0,A,b)
x=fmincon(fun,x0,A,b,Aeq,beq)
x=fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
x=fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
x=fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
x=fmincon(problem)
[x,fval]=fmincon(...)
[x,fval,exitflag]=fmincon(...)
[x,fval,exitflag,output]=fmincon(...)
[x,fval,exitflag,output,lambda]=fmincon(...)
[x,fval,exitflag,output,lambda,grad]=fmincon(...)
[x,fval,exitflag,output,lambda,grad,hessian]=fmincon(...)
Descriptionfminconattemptstofindaconstrainedminimumofascalarfunctionofseveralvariablesstartingataninitialestimate.Thisisgenerallyreferredtoasconstrainednonlinearoptimizationornonlinearprogramming.
Note:
PassingExtraParametersexplainshowtopassextraparameterstotheobjectivefunctionandnonlinearconstraintfunctions,ifnecessary.
x=fmincon(fun,x0,A,b)startsatx0andattemptstofindaminimizerxofthefunctiondescribedinfunsubjecttothelinear
inequalitiesA*x ≤ b.x0can
beascalar,vector,ormatrix.x=fmincon(fun,x0,A,b,Aeq,beq)minimizesfunsubject
tothelinearequalitiesAeq*x = beqandA*x ≤ b.Ifnoinequalities
exist,setA=[]andb=[].x=fmincon(fun,x0,A,b,Aeq,beq,lb,ub)defines
asetoflowerandupperboundsonthedesignvariablesinx,
sothatthesolutionisalwaysintherangelb ≤ x ≤ ub.
Ifnoequalitiesexist,setAeq=[]andbeq
=[].Ifx(i)isunboundedbelow,setlb(i)
=-Inf,andifx(i)isunboundedabove,
setub(i)=Inf.Note:
Ifthespecifiedinputboundsforaproblemareinconsistent,
theoutputxisx0andtheoutputfvalis[].Componentsofx0thatviolatetheboundslb ≤ x ≤ ubareresettotheinterioroftheboxdefined
bythebounds.Componentsthatrespecttheboundsarenotchanged.SeeIterationsCanViolateConstraints.x=fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)subjects
theminimizationtothenonlinearinequalitiesc(x)or
equalitiesceq(x)definedinnonlcon.fminconoptimizes
suchthatc(x) ≤ 0andceq(x) = 0.Ifnoboundsexist,setlb
=[]and/orub=[].x=fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)minimizes
withtheoptimizationoptionsspecifiedinoptions.
Useoptimoptionstosetthese
options.Iftherearenononlinearinequalityorequalityconstraints,
setnonlcon=[].x=fmincon(problem)findstheminimum
forproblem,whereproblemis
astructuredescribedinInputArguments.CreatetheproblemstructurebyexportingaproblemfromOptimizationapp,asdescribedinExportingYourWork.
[x,fval]=fmincon(...)returnsthevalueoftheobjectivefunctionfunatthesolutionx.
[x,fval,exitflag]=fmincon(...)returnsavalueexitflagthatdescribestheexitconditionoffmincon.
[x,fval,exitflag,output]=fmincon(...)returnsastructureoutputwithinformationabouttheoptimization.
[x,fval,exitflag,output,lambda]=fmincon(...)returnsastructurelambdawhosefieldscontaintheLagrangemultipliersatthesolutionx.
[x,fval,exitflag,output,lambda,grad]=fmincon(...)returnsthevalueofthegradientoffunatthesolutionx.
[x,fval,exitflag,output,lambda,grad,hessian]=fmincon(...)returnsthevalueoftheHessianatthesolutionx.SeefminconHessian.
InputArguments
FunctionArgumentsdescribestheargumentspassedtofmincon.Optionsprovidesthefunction-specificdetailsfortheoptionsvalues.Thissectionprovidesfunction-specificdetailsforfun,nonlcon,andproblem.
Fun
Thefunctiontobeminimized.funisafunctionthatacceptsavectorxandreturnsascalarf,theobjectivefunctionevaluatedatx.funcanbespecifiedasafunctionhandleforafile:
x=fmincon(@myfun,x0,A,b)wheremyfunisaMATLAB®functionsuchas
functionf=myfun(x)
f=...%Computefunctionvalueatx
funcanalsobeafunctionhandleforananonymous
function:
x=fmincon(@(x)norm(x)^2,x0,A,b);
IfthegradientoffuncanalsobecomputedandtheGradObjoptionis'on',assetby
options=optimoptions('fmincon','GradObj','on')
thenfunmustreturnthegradientvectorg(x)inthesecondoutputargument.IftheHessianmatrixcanalsobecomputedandtheHessianoptionis'on'viaoptions = optimoptions('fmincon','Hessian','user-supplied')andtheAlgorithmoptionistrust-region-reflective,funmustreturntheHessianvalueH(x),asymmetricmatrix,inathirdoutputargument.funcangiveasparseHessian.SeeWritingObjectiveFunctionsfordetails.
IftheHessianmatrixcanbecomputedandtheAlgorithmoptionisinterior-point,thereareseveralwaystopasstheHessiantofmincon.Formoreinformation,seeHessian.
A,b,Aeq,beq
LinearconstraintmatricesAandAeq,andtheircorrespondingvectorsbandbeq,canbesparseordense.Thetrust-region-reflectiveandinterior-pointalgorithmsusesparselinearalgebra.IfAorAeqislarge,withrelativelyfewnonzeroentries,saverunningtimeandmemoryinthetrust-region-reflectiveorinterior-pointalgorithmsbyusingsparsematrices.
Nonlcon
Thefunctionthatcomputesthenonlinearinequalityconstraintsc(x)≤0andthenonlinearequalityconstraintsceq(x)=0.nonlconacceptsavectorxandreturnsthetwovectorscandceq.cisavectorthatcontainsthenonlinearinequalitiesevaluatedatx,andceqisavectorthatcontainsthenonlinearequalitiesevaluatedatx.nonlconshouldbespecifiedasafunctionhandletoafileortoananonymousfunction,suchasmycon:
x=fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@mycon)
wheremyconisaMATLABfunctionsuchas
function[c,ceq]=mycon(x)
c=...%Computenonlinearinequalitiesatx.
ceq=...%Computenonlinearequalitiesatx.
IfthegradientsoftheconstraintscanalsobecomputedandtheGradConstroptionis'on',assetbyoptions=optimoptions('fmincon','GradConstr','on')thennonlconmustalsoreturn,inthethirdandfourthoutputarguments,GC,thegradientofc(x),andGCeq,thegradientofceq(x).GCandGCeqcanbesparseordense.IfGCorGCeqislarge,withrelativelyfewnonzeroentries,saverunningtimeandmemoryintheinterior-pointalgorithmbyrepresentingthemassparsematrices.Formoreinformation,seeNonlinearConstraints.
Note BecauseOptimizationToolbox™functionsonlyacceptinputsoftypedouble,user-suppliedobjectiveandnonlinearconstraintfunctionsmustreturnoutputsoftypedouble.
Problem
objectiveObjectivefunction
x0Initialpointforx
AineqMatrixforlinearinequalityconstraints
bineqVectorforlinearinequalityconstraints
AeqMatrixforlinearequalityconstraints
beqVectorforlinearequalityconstraints
lbVectoroflowerbounds
ubVectorofupperbounds
nonlconNonlinearconstraintfunction
solver'fmincon'
optionsOptionscreatedwithoptimoptions
OutputArgumentsFunctionArgumentsdescribesargumentsreturnedbyfmincon.Thissectionprovidesfunction-specificdetailsforexitflag,lambda,andoutput:
Exitflag
Integeridentifyingthereasonthealgorithmterminated.Thefollowingliststhevaluesofexitflagandthecorrespondingreasonsthealgorithmterminated.
AllAlgorithms:
1First-orderoptimalitymeasurewaslessthanoptions.TolFun,andmaximumconstraintviolationwaslessthanoptions.TolCon.
0Numberofiterationsexceededoptions.MaxIterornumberoffunctionevaluationsexceededoptions.MaxFunEvals.
-1Stoppedbyanoutputfunctionorplotfunction.
-2Nofeasiblepointwasfound.
trust-region-reflective,interior-point,andsqpalgorithms:
2Changeinxwaslessthanoptions.TolXandmaximumconstraintviolationwaslessthanoptions.TolCon.
trust-region-reflectivealgorithmonly:
3Changeintheobjectivefunctionvaluewaslessthanoptions.TolFunandmaximumconstraintviolationwaslessthanoptions.TolCon.
active-setalgorithmonly:
4Magnitudeofthesearchdirectionwaslessthan2*options.TolXandmaximumconstraintviolationwaslessthanoptions.TolCon.
5Magnitudeofdirectionalderivativeinsearchdirectionwaslessthan2*options.TolFunandmaximumconstraintviolationwaslessthanoptions.TolCon.
interior-pointandsqpalgorithms:
-3Objectivefunctionatcurrentiterationwentbelowoptions.ObjectiveLimitandmaximumconstraintviolationwaslessthanoptions.TolCon.
GradGradientatx
HessianHessianatx
LambdaStructurecontainingtheLagrangemultipliersatthesolutionx(separatedbyconstrainttype).Thefieldsofthestructureare:
LowerLowerboundslb
UpperUpperboundsub
IneqlinLinearinequalities
eqlinLinearequalities
ineqnonlinNonlinearinequalities
eqnonlinNonlinearequalities
outputStructurecontaininginformationabouttheoptimization.Thefieldsofthestructureare:
iterationsNumberofiterationstaken
funcCountNumberoffunctionevaluations
lssteplengthSizeoflinesearchsteprelativetosearchdirection(active-setalgorithmonly)
constrviolationMaximumofconstraintfunctions
stepsizeLengthoflastdisplacementinx(active-setandinterior-pointalgorithms)
algorithmOptimizationalgorithmused
cgiterationsTotalnumberofPCGiterations(trust-region-reflectiveandinterior-pointalgorithms)
firstorderoptMeasureoffirst-orderoptimality
messageExitmessage
Hessian
fminconusesaHessianasanoptionalinput.ThisHessianisthesecondderivativesoftheLagrangian(seeEquation 3-1),namely,
ThevariousfminconalgorithmshandleinputHessiansdifferently:
Theactive-setandsqpalgorithmsdonotacceptauser-suppliedHessian.Theycomputeaquasi-Newtonap