# 从指定方向图合成阵列：优化工作流程

## 示例1：仅优化权重

1. 确定期望方向图
2. 开发一个代价函数，尽可能缩小起始方向图和期望方向图之间的距离
3. 运行优化
4. 使用生成的权重查看方向图，并将其与期望方向图进行比较

```%% Set up optimization

objfun = @(w)norm(w'*stvmat-Beam_d);     % Define objective function used in fmincon
% Goal is to minimize the norms between
% the desired pattern and
% resulting pattern

weights_i = ones(N,1);                        % Initial setting for array amplitudes
% Serves as starting point to
% optimization

weights_o = fmincon(objfun,weights_i,[],[],[],[],zeros(N,1),ones(N,1));

% fmincon takes in the objfun,
% the initial weights, and
% upper and lower bounds of the weights
% In this example,
% 0 <= weights_o <= 1
% weights_o holds the weights
% which can be used to create
% a beam that matches our
% desired pattern

```

## 示例2：优化权重和阵元位置

1. 识别方向图属性
2. 定义约束，确保系统可以实现
3. -开发一个客观（成本）函数，得到所需“方向”的方向图属性
4. 运行优化
5. 将优化的方向图与期望方向图进行比较

```function [Val] = Position_Objective_optim_cost(Position_Taper)

% Define constants

lmbda = physconst('lightspeed')/1.1e9;
azimuth = -180:2:180;
elevation = -90:2:90;

% Creating a cosine Antenna Element
handle_Ant =phased.CosineAntennaElement('FrequencyRange',[1.0e9 1.2e9],...
'CosinePower',[1.5 2.5]);

% Extracting taper avalues from the input vector
Taper_value = Position_Taper(51:75) + 1i.*Position_Taper(76:100);

% Creating a Conformal Array with cosine elements.
% Conformal array will be limited to a single plane
handle_Conf_Array = phased.ConformalArray('Element',handle_Ant,...
'ElementPosition',[zeros(1,25);Position_Taper(1,1:25);Position_Taper(1,26:50)],...
'Taper',Taper_value);
```

```% Extracting Azimuth and Elevation Patterns
[fieldval_az] = patternAzimuth(handle_Conf_Array,1.1e9,0,'Azimuth',azimuth,'Type','Power');
[fieldval_el] = patternElevation(handle_Conf_Array,1.1e9,0,'Elevation'>,elevation,'Type'>,'Power');

% Replacing any inf and zeros with a very low value
fieldval_az(isinf(fieldval_az)) = -400;
fieldval_az((fieldval_az == 0)) = -400;

% Genarating a polar plot with the extracted data
% the polar plot method "FindLobes" is used to fetch the required

handle_polar_az = polarpattern(azimuth,mag2db(fieldval_az));
lobe_info = findLobes(handle_polar_az);

% Fetching the Sidelobe Magnitude
% Magnitude of the azimuth beam pattern
SL_Mag_az = lobe_info.sideLobes.magnitude;
```

## 建立目标函数

```% Extracting Azimuth and Elevation Patterns
[fieldval_az] = patternAzimuth(handle_Conf_Array,1.1e9,0,'Azimuth',azimuth,'Type','Power');
[fieldval_el] = patternElevation(handle_Conf_Array,1.1e9,0,'Elevation'>,elevation,'Type'>,'Power');

% Replacing any inf and zeros with a very low value
fieldval_az(isinf(fieldval_az)) = -400;
fieldval_az((fieldval_az == 0)) = -400;

% Genarating a polar plot with the extracted data
% the polar plot method "FindLobes" is used to fetch the required

handle_polar_az = polarpattern(azimuth,mag2db(fieldval_az));
lobe_info = findLobes(handle_polar_az);

% Fetching the Sidelobe Magnitude
% Magnitude of the azimuth beam pattern
SL_Mag_az = lobe_info.sideLobes.magnitude;

%%
% Summing the azimuth and elevation values
% Adding term to minimize the difference between the Az and El patterns

SL_Mag = SL_Mag_az + SL_Mag_el + abs(SL_Mag_az-SL_Mag_el);

% Computing the function value from the SL_Mag
Val = SL_Mag;
```