%
% Chapter 7, Example 7.1, pages 317-318
% Optimal Reference Shaping for Dynamical Systems
% Theory and Applications
% -Tarunraj Singh
%
function example_71
x0 = [1 2 3 4];

x = fmincon(@cst,x0,[],[],[],[],[],[],@constr,[])

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function [f,g] = cst(x);

T1 = x(1);
T2 = x(2);
T3 = x(3);
T4 = x(4);

f = x(4);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function [g,geq] = constr(x);

T1 = x(1);
T2 = x(2);
T3 = x(3);
T4 = x(4);

s = j*sqrt(2);

geq(1) = -2*T1+2*T2-2*T3+T4;
geq(2) = real(1-2*exp(-s*T1)+2*exp(-s*T2)-2*exp(-s*T3)+exp(-s*T4));
geq(3) = imag(1-2*exp(-s*T1)+2*exp(-s*T2)-2*exp(-s*T3)+exp(-s*T4));
geq(4) = -2*T1^2+2*T2^2-2*T3^2+T4^2-4;

g(1) = -T1;
g(2) = -(T2-T1);
g(3) = -(T3-T2);
g(4) = -(T4-T3);