www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@conswitch/SnapToGrid.m

    function m = SnapToGrid(m,Grid)
%SNAPTOGRID change operating points to match grid
%
% m = SnapToGrid(m,Grid);

%  Copyright 2007-2008 The MathWorks, Inc. and Ford Global Technologies, Inc.

OpPoints = m.OpPoints;
[N,ng] = size(OpPoints);
% tolerance for comparison
tol = getAbsoluteTolerance(m);

% make grid points
if ng>1
    % make grid points
    [XopGrid{1:ng}] = ndgrid(Grid{:});
else
    XopGrid = Grid;
end
Neval = numel(XopGrid{1});
Xgrid = zeros(Neval,ng);
for i=1:ng
    Xgrid(:,i) = XopGrid{i}(:);
end

pos = zeros(1,N);
for i = 1:N
    % find current operating point
    CurrentOp= true(1,Neval);
    for j=1:ng
        CurrentOp(CurrentOp)= abs( Xgrid(CurrentOp,j)-OpPoints(i,j)) < tol(j);
    end
    if any(CurrentOp)
        pos(i) = find(CurrentOp,1,'first');
        if any( pos(i)== pos(1:i-1) )
            % don't take duplicate points
            pos(i) = 0;
        end
    end
end

if ~all(pos)
    % remove duplicates or points not on grid
    m.ConList = m.ConList(pos~=0);
end
% change operating point to grid
m.OpPoints = Xgrid(pos(pos~=0),:);