%-----------------------------------
% BEGIN: script chemicalProcessMain.m
%-----------------------------------
% This m-file is the main file for the following optimal control
% problem:
% minimize
% J = int(x^4+0.5z^2+0.5u^2)dt
% subject to
% dx/dt = xz
% dz/dt = 10(-z+u)
% [x(0), z(0)] = [1/sqrt(2) 0]
% [x(tf), z(tf)] = [ 0.5 0]
%------------------------------------------------------------------
% This example is taken from the following reference:
% Kokotovic, Petar; Khalil, Hassan; and O'Reilly, John, Singular
% Perturbation Methods in Control, Analysis and Design, SIAM, 1999.
% This problem was coded by Christopher L. Darby (cdarby@ufl.edu)
%------------------------------------------------------------------
gpopsInitialize;
x0 = 1/sqrt(2);
xf = 0.5;
z0 = 0;
zf = 0;
xmin = -3;
xmax = 3;
zmin = -3;
zmax = 3;
umin = -3;
umax = 3;
t0min = 0;
t0max = 0;
tfmin = 1;
tfmax = 1;
% Phase 1 Information
iphase = 1;
limits{imin}{iphase,itime} = [t0min tfmin];
limits{imax}{iphase,itime} = [t0max tfmax];
limits{imin}{iphase,istate}(1,:) = [x0 xmin xf];
limits{imax}{iphase,istate}(1,:) = [x0 xmax xf];
limits{imin}{iphase,istate}(2,:) = [z0 zmin zf];
limits{imax}{iphase,istate}(2,:) = [z0 zmax zf];
limits{imin}{iphase,icontrol} = umin;
limits{imax}{iphase,icontrol} = umax;
limits{imin}{iphase,iparameter} = [];
limits{imax}{iphase,iparameter} = [];
limits{imin}{iphase,ipath} = [];
limits{imax}{iphase,ipath} = [];
limits{imin}{iphase,ievent} = [];
limits{imax}{iphase,ievent} = [];
limits{imin}{iphase,iduration} = [];
limits{imax}{iphase,iduration} = [];
solinit{iphase,itime} = [t0min; tfmax];
solinit{iphase,istate}(:,1) = [x0; x0];
solinit{iphase,istate}(:,2) = [z0; z0];
solinit{iphase,3} = [umax; umax];
solinit{iphase,4} = []; % No parameters in Phase 1
connections = [];
setup.name = 'Chemical Process Problem';
setup.funcs{1} = 'chemicalProcessCost';
setup.funcs{2} = 'chemicalProcessDae';
setup.funcs{3} = 'chemicalProcessEvent';
setup.funcs{4} = 'chemicalProcessConnect';
setup.nodes = 50;
setup.limits = limits;
setup.solinit = solinit;
setup.connections = connections;
setup.derivatives = 'complex';
setup.direction = 'increasing';
setup.autoscale = 'off';
output = gpops(setup);
solution = output.solution;