PI Controller Tuning : Cost function given by Dr. Steve Brunton​
​Authors: Housam Binous and Ahmed Bellagi

MATLAB code given below uses genetic algorithms for PI controller tuning and a Cost function given by Dr. Steve Brunton. This cost function is designed to minimize the ISE while penalizing high values of the manipulated variable or u. Excellent agreement is found between ga command of Matlab and NMinimize of Mathematica.
​
Reference:
https://www.youtube.com/watch?v=S5C_z1nVaSg
MATHEMATICA code below uses NMINIMIZE to fetch best PI controller parameters
In[]:=
Gc[KP_?NumericQ,TAUI_?NumericQ]:=KP+TAUI/s;​​GT=1/(s+1)/(s+2)/(s+3)
Out[]=
1
(1+s)(2+s)(3+s)
In[]:=
G[KP_?NumericQ,TAUI_?NumericQ]:=GTGc[KP,TAUI]/(1+GTGc[KP,TAUI])
In[]:=
Gu[KP_?NumericQ,TAUI_?NumericQ]:=Gc[KP,TAUI]/(1+GTGc[KP,TAUI])
In[]:=
y[KP_?NumericQ,TAUI_?NumericQ]:=InverseLaplaceTransform[1/sG[KP,TAUI],s,t]//Chop;
In[]:=
u[KP_?NumericQ,TAUI_?NumericQ]:=InverseLaplaceTransform[1/sGu[KP,TAUI],s,t]//Chop;
CRITERIA by Dr. Steve Brunton
In[]:=
f1[KP_?NumericQ,TAUI_?NumericQ]:=Re[Quiet@NIntegrate[(1-y[KP,TAUI])^2+0.001(u[KP,TAUI])^2,{t,0,30}]//Chop];
In[]:=
NMinimize[f1[KP,TAUI],{KP,TAUI},MaxIterations->10]
Out[]=
{1.93174,{KP14.4733,TAUI4.14302}}
Comparison with OUTPUTS of MATLAB coding (red curves) and MATHEMATICA calculations (dashed blue curves)
In[]:=
p1=Plot[u[14.473272,4.14302],{t,0,30},AspectRatio->1,GridLines->Automatic,Frame->True,PlotStyle->{Dashed,Thick},PlotRange->{{0,30},{2,16}},FrameLabel->{Style["time",14],Style["manipulated variable",14]}]
Out[]=
In[]:=
img=
;
In[]:=
Show[p1,ImageSize400{1,1},Prolog{Raster[ImageData[img,DataReversedTrue],{Scaled[{0.0,0.00}],Scaled[{1,1}]}]}]
Out[]=
In[]:=
p2=Plot[y[14.473272,4.14302],{t,0,30},AspectRatio->1,GridLines->Automatic,Frame->True,PlotStyle->{Dashed,Thick},PlotRange->{{0,30},{0,1.2}},FrameLabel->{Style["time",14],Style["controlled variable",14]}]
Out[]=
In[]:=
img2=
;
In[]:=
Show[p2,ImageSize400{1,1},Prolog{Raster[ImageData[img2,DataReversedTrue],{Scaled[{0.005,0.00}],Scaled[{1,1}]}]}]
Out[]=