function SimpleOptExample
% Figs. A.3a, A.3b Book Illustration for Simple Opt Principle Example: 
% Static, Quadratic Control (3/2007).
% 
clc % clear variables, but must come before globals, 
    %    else clears globals too.
clf % clear figures
fprintf('\nExample:  Static, Quadratic Control OutPut:');
%
dx = 0.1; 
x = -3:dx:+3;
us = ustar(x);
cs = C(x,us);
nfig = 0;
scrsize = get(0,'ScreenSize');
ss = [3.0,2.8,2.6,2.4,2.2,2.0];
%%%%%%%%%%%%%%%
nfig = nfig + 1;
fprintf('\n\nFigure(%i):  Static Optimal Example Control\n',nfig)
figure(nfig)
plot(x,us,'k-','LineWidth',4);
axis([-3 3 -2 2]);
title('Static Optimal Example Control'...
    ,'Fontsize',44,'FontWeight','Bold');
ylabel('u^*(x), Optimal Control'...
    ,'Fontsize',44,'FontWeight','Bold');
xlabel('x, State'...
    ,'Fontsize',44,'FontWeight','Bold');
set(gca,'Fontsize',36,'FontWeight','Bold','linewidth',3);
set(gcf,'Color','White','Position' ...
    ,[scrsize(3)/ss(nfig) 70 scrsize(3)*0.60 scrsize(4)*0.80]);
%
%%%%%%%%%%%%%%%
nfig = nfig + 1;
fprintf('\n\nFigure(%i):  Static Optimal Example Cost\n',nfig)
figure(nfig)
plot(x,cs,'k-','LineWidth',4);
title('Static Optimal Example Cost'...
    ,'Fontsize',44,'FontWeight','Bold');
ylabel('C^*(x), Optimal Cost'...
    ,'Fontsize',44,'FontWeight','Bold');
xlabel('x, State'...
    ,'Fontsize',44,'FontWeight','Bold');
set(gca,'Fontsize',36,'FontWeight','Bold','linewidth',3);
set(gcf,'Color','White','Position' ...
    ,[scrsize(3)/ss(nfig) 70 scrsize(3)*0.60 scrsize(4)*0.80]);
%
% End Optimal Example Code
function us = ustar(x)
us = max(-1,min(+1,x));
%
function cv = C(x,u)
cv = 2 + x + 0.5*(u-x).^2;
%
% End SimpleOptExample