## 108  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

### 108.1  Problem Description

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

Does not say u^2 in text

5a:

J =
 0.78 0
(x12+x22+0.1*u2)/2 dt + x1(tF)2 + x2(tF)2

5b:

J =
 0.78 0
(x12+x22+0.1*u2)/2 dt

5c:

J =
 0.78 0
(x12+x22)/2 dt

subject to:

 a1 = x1 + 0.25
 a2 = x2 + 0.5
 a3 = x1 + 2.0
a4 = a2*exp(25.0*
 x1 a3
 dx1 dt
= −2.0*a1 + a4 − a1*u
 dx2 dt
= 0.5 − x2 − a4
 x(0) = [0.05  0]

5b, 5c - x(t_F) = [0 0];

5c - u <= 1

Reference: [16]

### 108.2  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});
```

### 108.3  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
Starting numeric solver
===== * * * =================================================================== * * *
TOMLAB - Tomlab Optimization Inc. Development license  999001. Valid to 2011-02-05
=====================================================================================
Problem: ---  1: Stirred Tank 5a                f_k       0.014213969120012267
sum(|constr|)      0.000000005238899986
f(x_k) + sum(|constr|)      0.014213974358912253
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  113
CPU time: 0.171875 sec. Elapsed time: 0.172000 sec.
```
```Problem type appears to be: qpcon
Starting numeric solver
===== * * * =================================================================== * * *
TOMLAB - Tomlab Optimization Inc. Development license  999001. Valid to 2011-02-05
=====================================================================================
Problem: ---  1: Stirred Tank 5b                f_k       0.016702811155814266
sum(|constr|)      0.000000899223593776
f(x_k) + sum(|constr|)      0.016703710379408040
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  118
CPU time: 0.125000 sec. Elapsed time: 0.125000 sec.
```
```Problem type appears to be: qpcon
Starting numeric solver
===== * * * =================================================================== * * *
TOMLAB - Tomlab Optimization Inc. Development license  999001. Valid to 2011-02-05
=====================================================================================
Problem: ---  1: Stirred Tank 5c                f_k       0.000989922252663805
sum(|constr|)      0.000000035597664481
f(x_k) + sum(|constr|)      0.000989957850328286
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  139
CPU time: 0.078125 sec. Elapsed time: 0.078000 sec.
```
```end
```

### 108.4  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');
```