# A Linear Problem with Bang Bang Control

Paper: Solving Tough Optimal Control Problems by Network Enabled Optimization Server (NEOS)

Jinsong Liang, YangQuan Chen, Max Q.-H. Meng, Rees Fullmer Utah State University and Chinese University of Hong Kong (Meng)

EXAMPLE-1: A TEXTBOOK BANG-BANG OPTIMAL CONTROL PROBLEM

## Problem description

Find u over t in [0; tF ] to minimize subject to:       % Copyright (c) 2007-2008 by Tomlab Optimization Inc.


## Problem setup

toms t
toms tf

p = tomPhase('p', t, 0, tf, 30);
setPhase(p);

tomStates x1 x2
tomControls u

% Initial guess
% Note: The guess for tf must appear in the list before expression involving t.
x0 = {tf == 20
icollocate({x1 == 300*t/tf; x2 == 0})
collocate(u==1-2*t/tf)};

% Box constraints
cbox = {10 <= tf <= 40
-2 <= collocate(u) <= 1};

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

% ODEs and path constraints
ceq = collocate({dot(x1) == x2; dot(x2) == u});

% Objective
objective = tf;


## Solve the problem

options = struct;
options.name = 'Bang-Bang Free Time';
options.prilev = 1;
solution = ezsolve(objective, {cbox, cbnd, ceq}, x0, options);
t  = subs(collocate(t),solution);
x1 = subs(collocate(x1),solution);
x2 = subs(collocate(x2),solution);
u  = subs(collocate(u),solution);

Problem type appears to be: lpcon
===== * * * =================================================================== * * *
TOMLAB - Tomlab Optimization Inc. Development license  999001. Valid to 2010-02-05
=====================================================================================
Problem: ---  1: Bang-Bang Free Time            f_k      30.019823270470464000
sum(|constr|)      0.000028856745654083
f(x_k) + sum(|constr|)     30.019852127216119000
f(x_0)     20.000000000000000000

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

FuncEv    1 ConstrEv  526 ConJacEv  526 Iter  136 MinorIter  191
CPU time: 0.625000 sec. Elapsed time: 0.656000 sec.


## Plot result

subplot(2,1,1)
plot(t,x1,'*-',t,x2,'*-');
legend('x1','x2');
title('Bang-Bang Free Time state variables');

subplot(2,1,2)
plot(t,u,'+-');
legend('u');
title('Bang-Bang Free Time control'); 