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

    function [c,ok,sp,bp,sfopts] = fit(c,action,opts,Xdata,sp,bp)
%FIT fit con object
%
% [con,ok,sp,bp,status,params] = fit(con,action,opts,X,sp,bp)
%  Inputs
%    con:      con object
%    action:   'All', 'SpecialPoints', 'BoundaryPoints', or% 'Constraint'
%    opts:     fitoptions optimmgr
%    X:        data to be fitted {Xglobal, Xlocal}
%    sp:       special points
%    bp:       boundary points
%
% Outputs:
%    con
%    ok:      fit status flag
%    sp       special points
%    bp       boundary points
%    status:  status structure
%    params:  local parameters matrix

%  Copyright 2006-2011 The MathWorks, Inc. 

% 
if isempty(c.ConList)
   error(message('mbc:conswitch:InvalidState'))
end
con = c.ConList{1};

% work out what sort of fit is needed and what fit options are needed
doSpecialPoints  = false;
doBoundaryPoints = false;
doFitConstraint  = false;

if builtin('isempty',opts) 
    opts = getFitOptions(con,'all');
end

switch lower(action)
    case 'all'
        sopts = get(opts,'SpecialPointOptions');
        bpopts = get(opts,'BoundaryPointOptions');
        fopts = get(opts,'ConstraintFitOptions');

        doSpecialPoints  = true;
        doBoundaryPoints = true;
        doFitConstraint  = true;
        
    case 'specialpoints'
        doSpecialPoints = true;
        sopts = opts;
        bpopts = [];
        fopts = [];
        
    case 'boundarypoints'
        doBoundaryPoints = true;
        
        sopts = [];
        bpopts = opts;
        fopts = [];
        
    case 'constraint'
        doFitConstraint = true;
        sopts = [];
        bpopts = [];
        fopts = opts;
        
    otherwise
        error(message('mbc:xregbdrydev:InvalidArgument'));
end

doSpecialPoints  = doSpecialPoints  && ~isempty( sopts );
doBoundaryPoints = doBoundaryPoints && ~isempty( bpopts );
doFitConstraint  = doFitConstraint  && ~isempty( fopts );


% data {Xglobal, Xlocal}
Xlocal  = Xdata{2};
Xglobal = Xdata{1};

NumTests = size(Xlocal,3);
clist = cell(NumTests,1);
OK = true(1,NumTests);
% constraint type

cinp = getInputFactors(con);
if nargin<5 || isempty(sp)
    sp = cell(1,NumTests);
end
if nargin<6 || isempty(bp)
    bp = cell(1,NumTests);
end
params = NaN(NumTests,1);
TestIndex = 1;

for TestNo = 1:NumTests
    X = Xlocal{TestNo};

    % remove nonfinite data 
    DataOK = all(isfinite(X),2);
    X = X(DataOK,:);
    
    % use data range
    cinp = DataRange(cinp,X);
    con = setInputFactors(con,cinp);
    
    % fit  local constraint
    
    % do special points if there are any
    if OK(TestNo) && doSpecialPoints
        [con, sp{TestNo}, OK(TestNo)] = findSpecialPoints( con, sopts, X );
    end
    % find boundary points
    if OK(TestNo) && doBoundaryPoints
        [con, bp{TestNo}, OK(TestNo)] = findBoundaryPoints( con, bpopts, X );
        X = X(bp{TestNo},:);
    end
    
    % fit constraint if required (convex hull doesn't require fitting)
    if OK(TestNo) && doFitConstraint
        [con,OK(TestNo)] = fitConstraint(con,fopts,X);
    end

    if ~isempty(bp{TestNo})
        % correct boundary point indices
        DataOK = find(DataOK);
        bp{TestNo} = DataOK(bp{TestNo});
    end
    
    % collect results
    if OK(TestNo)
        % for convenience the operating point is stored to allow easy
        % reconstruction
        params(TestNo,:) = TestIndex;
        TestIndex = TestIndex+1;
    end
    clist{TestNo} = con;
end
ok = any(OK);

if ok
    c.ConList = clist(OK);
    c.OpPoints = double(Xglobal(OK,:));
end


% store status of fit in struct for use in setting bdrydev
sfopts.doSpecialPoints = doSpecialPoints;
sfopts.doBoundaryPoints = doBoundaryPoints;
sfopts.doFitConstraint = doFitConstraint;
sfopts.LocalParameters = params;