Main Content

최적화 솔버 플롯 함수

플롯 함수란?

options 구조체의 PlotFcns 필드는 각 반복에서 최적화 함수가 다양한 진행률 측정값을 플로팅하도록 호출하는 하나 이상의 함수를 지정합니다. 함수 핸들 또는 함수 핸들로 구성된 셀형 배열을 전달합니다.

PlotFcns 옵션은 다음과 같은 MATLAB 최적화 함수에 사용할 수 있습니다.

  • fminbnd

  • fminsearch

  • fzero

이러한 최적화 함수를 위해 미리 정의된 플롯 함수는 다음과 같습니다.

  • @optimplotx는 현재 점을 플로팅합니다.

  • @optimplotfval은 함수 값을 플로팅합니다.

  • @optimplotfunccount는 함수 실행 횟수를 플로팅합니다(fzero에는 사용할 수 없음).

예제: 내장 플롯 함수 사용하기

fminsearch를 사용한 최소화 진행 상황을 플롯 함수 @optimplotfval을 사용하여 확인할 수 있습니다.

목적 함수 onehump이 예제의 끝에 나와 있습니다.

@optimplotfval 플롯 함수를 사용하도록 options를 설정합니다.

options = optimset('PlotFcns',@optimplotfval);

x0 = [2,1]에서부터 fminsearch를 호출합니다.

x0 = [2 1];
[x fval] = fminsearch(@onehump,x0,options)

Figure Optimization Plot Function contains an axes object. The axes object with title Current Function Value: -0.405237, xlabel Iteration, ylabel Function value contains an object of type scatter.

x = 1×2

   -0.6691    0.0000

fval = -0.4052

예제: 사용자 지정 플롯 함수 사용하기

출력 함수와 동일한 구문을 사용하여 사용자 지정 플롯 함수를 작성할 수 있습니다. 이 구조에 대한 자세한 내용은 최적화 솔버 출력 함수 항목을 참조하십시오.

반복 횟수로 레이블이 지정된 반복 점을 표시하는 2차원 플롯 함수를 만듭니다. 코드는 이 예제의 끝에 나와 있는 myplot 헬퍼 함수를 참조하십시오. 플롯 함수가 원래의 @optimplotfval 플롯 함수와 myplot 플롯 함수를 모두 호출하도록 합니다.

options.PlotFcns = {@myplot @optimplotfval};
[x fval] = fminsearch(@onehump,x0,options)

Figure Optimization Plot Function contains 2 axes objects. Axes object 1 contains 96 objects of type line, text. One or more of the lines displays its values using only markers Axes object 2 with title Current Function Value: -0.405237, xlabel Iteration, ylabel Function value contains an object of type scatter.

x = 1×2

   -0.6691    0.0000

fval = -0.4052

이 사용자 지정 플롯 함수는 솔버가 최종점 [-0.6691 0.0000]에 수렴함에 따라 반복 횟수의 마지막 절반 정도를 같은 위치에 겹쳐서 플로팅합니다. 이로 인해 반복 횟수의 마지막 절반 정도는 읽기 어렵습니다. 그렇지만 이 플롯을 통해 fminsearch가 최소화 점을 향해 어떻게 반복되는지를 확인할 수 있습니다.

헬퍼 함수

다음 코드는 onehump 헬퍼 함수를 생성합니다.

function f = onehump(x)

r = x(1)^2 + x(2)^2;
s = exp(-r);
f = x(1)*s+r/20;
end

다음 코드는 myplot 헬퍼 함수를 생성합니다.

function stop = myplot(x,optimValues,state)
stop = false;
switch state
    case 'init'
          % Setup for plots or dialog boxes
          hold on
    case 'iter'
          % Make updates to plots or dialog boxes as needed
          plot(x(1),x(2),'rx');
          text(x(1)+.05,x(2),... 
                num2str(optimValues.iteration));
    case 'done'
          % Cleanup of plots, dialog boxes, or final plot
          hold off
end
end

관련 항목