%% Loop Shaping with ncfsyn
% Use ncfsyn to design a controller for the following plant.

G = tf([1 5],[1 2 10]);

% Use weighting functions that yield a shaped plant W1*G*W2 with high gain for disturbance attenuation below 0.1 rad/s, and low gain for good robust stability above about 5 rad/s. For this G, a pre-compensator weight alone is sufficient.

W1 = tf(1,[1 0]);
bodemag(W1*G)
grid

%
% Compute the controller.

[K,CL,gamma,info] = ncfsyn(G,W1);

%
% The optimal cost gamma is related to the normalized coprime stability margin of the system by 1/gamma = ncfmargin(Gs,-Ks). (The minus sign is needed because ncfmargin assumes a negative-feedback loop, while ncfsyn computes a positive-feedback controller.)

b = ncfmargin(info.Gs,-info.Ks);
[gamma 1/b]

%
% Compare the achieved and target loop shapes.

sigma(G*K,G*W1)
legend('achieved','target')

%
% Copyright 2020 The MathWorks, Inc.