%% Maximize Impedance Bandwidth of Triangular Patch Antenna
% This example shows how to optimize a triangular microstrip patch antenna to maximize its bandwidth such that its gain remains constant.
%
%% Define Antenna Geometry
% Define the initial geometry of the antenna.

% Define Antenna
ant = patchMicrostripTriangular;

% Center frequency and frequency range 
fc = 10e9;
% For BW ~20%
minFreq = fc * 0.8;
maxFreq = fc * 1.2;
freqRange = linspace(minFreq, maxFreq, 35);

% Initial point
ant = design(ant, fc)

% Use show function to view the antenna.

show(ant) 

%
%% Initial Analysis
% Analyze the gain of the antenna. Record the maximum gain of the antenna before optimization.

pattern(ant, fc);

% Analyze the impedance bandwidth of the antenna using S-parameters.

rfplot(sparameters(ant, freqRange));

%
%% Set Up Optimization Problem
% You will need the following inputs for the optimization problem:
% 1. Objective Function:  Choose 'maximizeBandwidth' as the objective function. The main goal of this example is maximizing the bandwidth of the antenna.
% 2. Design variables: Select side length, height of the triangular patch antenna, and feed offset as control variables for the objective function. The side length of the triangular patch will affect the front lobe gain. The height and the feed location will enhance the impedance bandwidth of the antenna and help improve its matching.
% 3. Constraints: The constraint function is gain more than 8.5 dBi.

% Design Variables
% Property Names
propNames = {'Side', 'Height', 'FeedOffset'};

% Bounds for the properties {lower bounds; upper bounds}          
bounds = {0.017 0.001 [0 0.0017]; ... % Specify the FeedOffset vector to move the feed along [x, y]
    0.023 0.004 [0.0002 0.0034]};

%
%% Optimization Function
% To optimize the antenna, use the optimize function.


figure; % Figure to display the convergence trend
% Optimize
optAnt = optimize(ant, fc, 'maximizeBandwidth', ...
    propNames, bounds, ...
    'Constraints', {'Gain > 8.5'}, ...
    'FrequencyRange', freqRange, ...
    'UseParallel', true);

% Set 'EnableLog' as true, in order to print the iteration number and best value of convergence on the command line.
%
%
%% Optimization Result
% Compare the analysis parameters of the antenna from before and after the optimization.

optAnt
show(optAnt) 
pattern(optAnt, fc)
rfplot(sparameters(optAnt, freqRange))

% Observe the bandwidth of the antenna after optimization.
%
% Copyright 2020 The MathWorks, Inc.