function RegCtrlExample6p1a
% Figs. A.1a, A.1b, A.2a, A.2b Book Illustration 
%     for Regular Control & Bang Control Examples:
%     Bang Control is Also part of Exercise 3
% Linear State Dynamics, Power Utility.
% 
clc % clear variables, but must come before globals, 
    %    else clears globals too.
clf % clear figures
fprintf('\nExample 6.1: Regular & Bang Control OutPuts:');
%
global gamma mu0 x0 tf 
%
gammav = [0.5,2.0]; % i.e., [Regular,Bang] examples
ng = 2;
mu0 = 0.10;
x0 = 10;
t0 = 0; tf = 1.0;
dt = 0.01; 
t = t0:dt:tf;
umin = 0; umax = 10; du = (umax-umin)/100;
uv = umin:du:umax;
fprintf('\n(t0,tf)=(%f,%f); x0=%f; mu0=%f;\n(umin,umax)=(%f,%f); du=%f;'...
    ,t0,tf,x0,mu0,umin,umax,du);
nfig = 0;
scrsize = get(0,'ScreenSize');
ss = [3.0,2.8,2.6,2.4,2.2,2.0];
%
for i = 1:ng
    gamma = gammav(i);
    if i == 1
        K00 = 10; K0 = K00;
        K01 = 1.0;
        K02 = 20;
fprintf('\n GK(%f)=%e; GK(%f)=%e;',K01,GK(K01),K02,GK(K02));
        [Z,Gval,exitflag] = fzero(@GK,[K01,K02]);
fprintf('\ngamma=%f; Z=%2e;  Gval=%e; exitflag=%e;'...
    ,gamma,Z,Gval,exitflag); 
        if isnan(Z) == 0
            K0 = Z; 
        end
        ureg = K0; % us = ureg;
        xreg = X(t,ureg);
        xf = X(tf,ureg);
        lamf = xf^(gamma-1);
        lamreg = LAM(t,ureg,lamf);
        Sf = S(xf,tf);
fprintf('\ngamma=%f; Ureg=%f; xf =%f; lamf=%f; Sf=%f;' ...
    ,gamma,K0,xf,lamf,Sf); 
        nfig = nfig + 1;
        fprintf('\n\nFigure(%i):  Regular Control Example\n',nfig)
        figure(nfig)
        plot(t,xreg,'k-',t,lamreg,'k--','LineWidth',4);
        title('Regular Control Maximum Example (a)'...
            ,'Fontsize',44,'FontWeight','Bold');
        ylabel('X^*(t) and \lambda^*(t), Optimal States'...
            ,'Fontsize',44,'FontWeight','Bold');
        xlabel('t, time'...
            ,'Fontsize',44,'FontWeight','Bold');
        hlegend=legend('X^*(t) Optimal Wealth State' ...
        ,'\lambda^*(t) Optimal Co-State','Location','Best');
set(hlegend,'Fontsize',36,'FontName','Helvetica','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;
    K = (x0*exp((mu0-ureg)*tf))^gamma;
    hv = Hc(uv,K);
fprintf('\n\nFigure(%i): Hamiltonian Regular Example\n',nfig)
figure(nfig)
plot(uv,hv,'k-','LineWidth',4);
title('Hamiltonian Regular Example'...
    ,'Fontsize',44,'FontWeight','Bold');
ylabel('Hc(u), Hamiltonian'...
    ,'Fontsize',44,'FontWeight','Bold');
xlabel('u, Control'...
    ,'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
%%%%%%%%%%%%%%%
    if i == 2
        K = (x0*exp((mu0-umin)*tf))^gamma;
        Hmin = Hc(umin,K);
        K = (x0*exp((mu0-umax)*tf))^gamma;
        Hmax = Hc(umax,K);
        ubang = umin; % Hbang = umax;
        if Hmax > Hmin, ubang = umax; end
        xbang = X(t,ubang); % us = ubang;
        xf = X(tf,ubang);
        lamf = xf^(gamma-1);
        lambang = LAM(t,ubang,lamf);
fprintf('\ngamma=%f; ubang=%f; xf=%e; lamf=%e;'...
    ,gamma,ubang,xf,lamf);
fprintf('\ngamma=%f; ubang=%f; Hmin=%e; Hmax=%e;'...
    ,gamma,ubang,Hmin,Hmax);
        nfig = nfig + 1;
        fprintf('\n\nFigure(%i): Bang Control Example\n',nfig)
        figure(nfig)
        plot(t,xbang,'k-',t,lambang,'k--','LineWidth',4);
        title('Bang Control Maximum Example (b)'...
            ,'Fontsize',44,'FontWeight','Bold');
        ylabel('X^{bang}(t) and \lambda^{bang}(t), Bang States'...
            ,'Fontsize',44,'FontWeight','Bold');
        xlabel('t, time'...
            ,'Fontsize',44,'FontWeight','Bold');
        hlegend=legend('X^{bang}(t) Bang Wealth State' ...
            ,'\lambda^{bang}(t) Bang Co-State','Location','Best');
set(hlegend,'Fontsize',36,'FontName','Helvetica','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;
    K = (x0*exp((mu0-ubang)*tf))^gamma;
    hv = Hc(uv,K);
fprintf('\n\nFigure(%i): Hamiltonian Bang Example\n',nfig)
figure(nfig)
plot(uv,hv,'k-','LineWidth',4);
title('Hamiltonian Bang Example'...
    ,'Fontsize',44,'FontWeight','Bold');
ylabel('Hc(u), Hamiltonian'...
    ,'Fontsize',44,'FontWeight','Bold');
xlabel('u, Control'...
    ,'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
%
end
%
% End Optimal Example Code
%
function cv = C(u)
global gamma 
cv = u.^gamma/gamma;
%
function sv = S(x,t) % general form, ignore t not used warning
global gamma 
sv = x.^gamma/gamma;
%
function gv = GK(K0)
global gamma mu0 x0 tf
gv = K0^(gamma-1)-(x0*exp((mu0-K0)*tf))^gamma;
%
function xv = X(t,K0)
global mu0 x0
xv = x0*exp((mu0-K0)*t);
%
function lamv = LAM(t,K0,lamf)
global mu0 tf
lamv = lamf*exp(-(mu0-K0)*(t-tf));
%
function hu = Hc(u,K) % Hamiltonian for fixed or bang control u.
global mu0
hu = C(u) + K*(mu0-u);
%
% End RegCtrlExample6p1a