# Stirred Tank

Users Guide for dyn.Opt, Example 5a, 5b, 5c

Stirred-Tank Chemical Reactor - Kirk, D. E., Optimal control theory: An introduction, Prentice-Hall, 1970.

5a - unconstrained with terminal penalty 5b - unconstrained 5c - control constraint

## Problem Description

Find u over t in [0; 0.78 ] to minimize

Does not say u^2 in text

5a: 5b: 5c: subject to:       5b, 5c - x(tF) = [0 0];

5c - u <= 1

% Copyright (c) 2007-2008 by Tomlab Optimization Inc.


## Problem setup

toms t

for i=1:3

    p = tomPhase('p', t, 0, 0.78, 40);
setPhase(p);

tomStates x1 x2
tomControls u

% Initial guess
x0 = {icollocate({x1 == 0.05; x2 == 0})
collocate(u == 0)};

% Box constraints
cbox = {-1.99 <= icollocate(x1) <= 100
-100  <= icollocate(x2) <= 100
-1000 <= collocate(u) <= 1000};
% x1 cannot be equal to -2, setting to greater
% to avoid singularity in a2*exp(25.0*x1/a3)

% Boundary constraints
cbnd = initial({x1 == 0.05; x2 == 0});

% ODEs and path constraints
a1 = x1 + 0.25; a2 = x2 + 0.5;
a3 = x1 + 2.0;  a4 = a2.*exp(25.0*x1./a3);

ceq = collocate({
dot(x1) == -2.0*a1 + a4 - a1.*u
dot(x2) == 0.5 - x2 - a4});


## Solve the problem

    options = struct;
if i==1
objective = final(x1)^2+final(x2)^2+...
integrate((x1.^2+x2.^2+0.1*u.^2)/2);
options.name = 'Stirred Tank 5a';
solution1 = ezsolve(objective, {cbox, cbnd, ceq}, x0, options);
t1  = subs(collocate(t),solution1);
x11 = subs(collocate(x1),solution1);
x21 = subs(collocate(x2),solution1);
u1  = subs(collocate(u),solution1);
elseif i == 2
cbnd = {cbnd; final({x1 == 0; x2 == 0})};
objective = integrate((x1.^2+x2.^2+0.1*u.^2)/2);
options.name = 'Stirred Tank 5b';
solution2 = ezsolve(objective, {cbox, cbnd, ceq}, x0, options);
else
cbnd = {cbnd; final({x1 == 0; x2 == 0})};
cbox = {-1.99 <= icollocate(x1) <= 100
-100  <= icollocate(x2) <= 100
-1    <= collocate(u)   <= 1};
objective = integrate((x1.^2+x2.^2)/2);
options.name = 'Stirred Tank 5c';
solution3 = ezsolve(objective, {cbox, cbnd, ceq}, x0, options);
end

Problem type appears to be: qpcon
===== * * * =================================================================== * * *
TOMLAB - Tomlab Optimization Inc. Development license  999001. Valid to 2010-02-05
=====================================================================================
Problem: ---  1: Stirred Tank 5a                f_k       0.014213969120012286
sum(|constr|)      0.000000005238894780
f(x_k) + sum(|constr|)      0.014213974358907066
f(x_0)      0.003474999999999964

Solver: snopt.  EXIT=0.  INFORM=1.
SNOPT 7.2-5 NLP code
Optimality conditions satisfied

FuncEv    1 ConstrEv   30 ConJacEv   30 Iter   27 MinorIter  103
CPU time: 0.140625 sec. Elapsed time: 0.141000 sec.

Problem type appears to be: qpcon
===== * * * =================================================================== * * *
TOMLAB - Tomlab Optimization Inc. Development license  999001. Valid to 2010-02-05
=====================================================================================
Problem: ---  1: Stirred Tank 5b                f_k       0.016702811171074760
sum(|constr|)      0.000000902063254886
f(x_k) + sum(|constr|)      0.016703713234329644
f(x_0)      0.000974999999999999

Solver: snopt.  EXIT=0.  INFORM=1.
SNOPT 7.2-5 NLP code
Optimality conditions satisfied

FuncEv    1 ConstrEv   18 ConJacEv   18 Iter   16 MinorIter  133
CPU time: 0.109375 sec. Elapsed time: 0.109000 sec.

Problem type appears to be: qpcon
===== * * * =================================================================== * * *
TOMLAB - Tomlab Optimization Inc. Development license  999001. Valid to 2010-02-05
=====================================================================================
Problem: ---  1: Stirred Tank 5c                f_k       0.000989922252663431
sum(|constr|)      0.000000035597740903
f(x_k) + sum(|constr|)      0.000989957850404334
f(x_0)      0.000974999999999999

Solver: snopt.  EXIT=0.  INFORM=1.
SNOPT 7.2-5 NLP code
Optimality conditions satisfied

FuncEv    1 ConstrEv   14 ConJacEv   13 Iter   10 MinorIter  157
CPU time: 0.078125 sec. Elapsed time: 0.079000 sec.

end


## Plot result

subplot(2,1,1)
plot(t1,x11,'*-',t1,x21,'*-');
legend('x1','x2');
title('Stirred Tank state variables');

subplot(2,1,2)
plot(t1,u1,'+-');
legend('u');
title('Stirred Tank control'); 