% ModelErrorGraph.m % % Plots graphs of parameter optimization for simple two-parameter % functions. Shows results for three parameter search algorithms: % SimpleHillClimb, fminsearch, and fmincon. % % Programmed by John Kruschke, 1/28/03. Modified 1/26/2005. % Initial parameter values parInit = [ 1.6 -1.6 ] ; % Increment (a.k.a. step) size on each parameter parInc = [ 0.1 0.1 ] ; % Lowest and highest parameter values allowed parLow = [ -4 -4 ] ; parHigh = [ 4 4 ] ; % The function that returns the model error is "modelError". % Compute optimization from SimpleHillClimb. disp('======================= SimpleHillClimb ===========================') traj = SimpleHillClimb(@ModelError,parInit,parLow,parHigh,parInc) bestFitSimpleHillClimb = traj(size(traj,1),:) % Compute optimization from another built-in MATLAB routine. disp('======================= fminsearch ================================') searchOptions = optimset('Display','iter'); [X,FVAL,exitflag,output] = fminsearch(@ModelError,parInit,searchOptions) bestFitFMINSEARCH = [ X FVAL ] % Compute optimization from a built-in MATLAB routine. disp('======================= fmincon ===================================') searchOptions = optimset('Display','iter'); [X,FVAL,exitflag,output] = fmincon(@ModelError,parInit,[],[],[],[], ... parLow,parHigh,[],searchOptions) bestFitFMINCON = [ X FVAL ] % Graph the results % GridEvaluation evaluates the model error over an entire grid of points. % This can be done for the simplistic two-parameter "model" used here, % but in general is not feasible. [ X Y Z ] = GridEvaluation(@ModelError,parLow,parHigh,parInc) ; figure(1) subplot(1,2,1,'replace') ; surfc(X,Y,Z) ; hold on; plot3(traj(:,1),traj(:,2),traj(:,3),'-m','LineWidth',3); plot3(bestFitSimpleHillClimb(1), bestFitSimpleHillClimb(2), ... bestFitSimpleHillClimb(3),'om', 'MarkerFaceColor','m','MarkerSize',10); plot3(bestFitFMINSEARCH(1),bestFitFMINSEARCH(2),bestFitFMINSEARCH(3), ... 'sb', 'MarkerFaceColor','b','MarkerSize',10); plot3(bestFitFMINCON(1),bestFitFMINCON(2),bestFitFMINCON(3),'dg', ... 'MarkerFaceColor','g','MarkerSize',10); hold off; axis([ parLow(1) parHigh(1) parLow(2) parHigh(2) 0 16 ]) ; axis square ; title('Magenta = SimpleHillClimb, Blue = fminsearch, Green = fmincon') ; xlabel('Param_1') ; ylabel('Param_2') ; zlabel('Error') ; subplot(1,2,2,'replace') ; hold on; contour(X,Y,Z,20) ; plot(traj(:,1),traj(:,2),'-m','LineWidth',3); plot(bestFitSimpleHillClimb(1),bestFitSimpleHillClimb(2),'om', ... 'MarkerFaceColor','m','MarkerSize',10); plot(bestFitFMINSEARCH(1),bestFitFMINSEARCH(2),'sb', ... 'MarkerFaceColor','b','MarkerSize',10); plot(bestFitFMINCON(1),bestFitFMINCON(2),'dg', ... 'MarkerFaceColor','g','MarkerSize',10); hold off; axis([ parLow(1) parHigh(1) parLow(2) parHigh(2) ]) ; axis square ; grid on; xlabel('Param_1') ; ylabel('Param_2') ; %%%%%%%%%%%%%%% end of program %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%