%% Varying Tuning Goal
% Create a tuning goal that specifies variable gain and phase margins across a grid of design points.
% Suppose you use the following 5-by-5 grid of design points to tune your controller.

[alpha,V] = ndgrid(linspace(0,20,5),linspace(700,1300,5));

%
% Suppose further that you have 5-by-5 arrays of target gain margins and target phase margins corresponding to each of the design points, such as the following.

[GM,PM] = ndgrid(linspace(7,20,5),linspace(45,70,5));

%
% To enforce the specified margins at each design point, first create a template for the margins goal. The template is a function that takes gain and phase margin values and returns a TuningGoal.Margins object with those margins.

FH = @(gm,pm) TuningGoal.Margins('u',gm,pm);

%
% Use the template and the margin arrays to create the varying goal.

VG = varyingGoal(FH,GM,PM);

%
% To make it easier to trace which goal applies to which design point, use the SamplingGrid property to attach the design-point information to VG.

VG.SamplingGrid = struct('alpha',alpha,'V',V);

% Use VG with systune as you would use any other tuning goal. Use viewGoal to visualize the tuning goal and identify design points that fail to meet the target margins.
% Copyright 2012 The MathWorks, Inc.