Main Content

이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.

플롯 함수

실행 중 최적화 플로팅하기

솔버를 실행하는 동안 다양한 진행률 측정값을 플로팅할 수 있습니다. optimoptionsPlotFcn 이름-값 쌍을 설정하고, 매 반복 시 호출할 솔버에 대해 하나 이상의 플롯 함수를 지정합니다. 함수 핸들 또는 함수 핸들로 구성된 셀형 배열을 전달합니다.

미리 정의된 다양한 플롯 함수를 사용할 수 있습니다. 솔버 함수 도움말 페이지의 PlotFcn 옵션 설명을 참조하십시오.

맞춤형으로 작성한 플롯 함수를 사용할 수도 있습니다. 동일한 구조체를 출력 함수로 사용하여 함수 파일을 작성합니다. 이 구조체에 대한 자세한 내용은 Output Function and Plot Function Syntax 항목을 참조하십시오.

플롯 함수 사용하기

이 예제에서는 플롯 함수를 사용하여 fmincon 'interior-point' 알고리즘의 진행률을 표시하는 방법을 보여줍니다. 이 문제는 최적화 라이브 편집기 작업 또는 솔버를 사용한, 제약 조건이 있는 비선형 문제에서 가져왔습니다.

비선형 목적 함수와 제약 조건 함수를 해당 기울기와 함께 작성합니다. 목적 함수는 로젠브록 함수입니다.

type rosenbrockwithgrad
function [f,g] = rosenbrockwithgrad(x)
% Calculate objective f
f = 100*(x(2) - x(1)^2)^2 + (1-x(1))^2;

if nargout > 1 % gradient required
    g = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1));
        200*(x(2)-x(1)^2)];
end

이 파일을 rosenbrockwithgrad.m으로 저장합니다.

제약 조건 함수는 해가 norm(x)^2 <= 1을 충족하는 것입니다.

type unitdisk2
function [c,ceq,gc,gceq] = unitdisk2(x)
c = x(1)^2 + x(2)^2 - 1;
ceq = [ ];

if nargout > 2
    gc = [2*x(1);2*x(2)];
    gceq = [];
end

이 파일을 unitdisk2.m으로 저장합니다.

세 개의 플롯 함수 호출을 포함하는 솔버의 옵션을 만듭니다.

options = optimoptions(@fmincon,'Algorithm','interior-point',...
 'SpecifyObjectiveGradient',true,'SpecifyConstraintGradient',true,...
 'PlotFcn',{@optimplotx,@optimplotfval,@optimplotfirstorderopt});

초기점 x0 = [0,0]을 만들고 나머지 입력값을 빈 값([])으로 설정합니다.

x0 = [0,0];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];

options를 포함하여 fmincon을 호출합니다.

fun = @rosenbrockwithgrad;
nonlcon = @unitdisk2;
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)

{"String":"Figure Optimization Plot Function contains 3 axes objects. Axes object 1 with title Current Point contains an object of type bar. Axes object 2 with title Current Function Value: 0.0456748 contains an object of type line. Axes object 3 with title First-order Optimality: 2.16246e-08 contains an object of type line.","Tex":[],"LaTex":[]}

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
x = 1×2

    0.7864    0.6177

관련 항목