www.gusucode.com > mbcmodels 工具箱 matlab 源码程序 > mbcmodels/@xregusermod/checkin.m
function [U0,OK]= checkin(U,fname,x) %CHECKIN Test and check a user-defined model into MBC Toolbox % % [U,OK]= CHECKIN(U,FNAME,X) checks the user-defined model into the MBC Toolbox. % Inputs % U is a xregusermod object. Use the constructor 'xregusermod' to specify a % default object. % FNAME is the name of the function file that contains the required model % definition information. % X is a matrix of test input data that is used to check if the model % file can be executed. % % Example usage: % [u,ok] = checkin(xregusermod,'weibul',[0.1:0.01:0.2 ]'); % % See also xregusermod/weibul, xregtransient/fuelPuddle % Copyright 2000-2010 The MathWorks, Inc. and Ford Global Technologies, Inc. if nargin < 3 % default inputs nf = feval(fname,U,'nfactors'); if isa(U,'xregtransient') % transient models need time as the first variable x = [0:0.1:10;ones(nf-1,101)]'; else x = ones(101,nf); end end % initialise function U=funcinit(U,fname); % turn on error U = ErrorStatus(U,true); nf= nfactors(U); % check data is the right size if size(x,2) ~= nf error(message('mbc:xregusermod:InvalidModel3')); end np= numParams(U); yf =eval(U,x); if length(yf)~=size(x,1) || all(isnan(yf)) error(message('mbc:xregusermod:InvalidModel4')); end % calculate initial estimates of model parameters x0 = initial(U,x,yf); % evaluate constraints on model parameters evalConstraints(U,x0,x,yf); U0=U; U0.parameters=x0; try Jf = feval(U.funcName,U,'Jacobian',x); catch Jf = []; end if ~isempty(Jf) if any( size(Jf)~=[size(x,1),np] ) error(message('mbc:xregusermod:InvalidModel5')); end % use the derivative check in fmincon to check the Jacobian % calculation fopts= optimset(optimset('fmincon'),... 'DerivativeCheck','on',... 'largescale','off',... 'Algorithm','active-set',... 'GradObj','on',... 'MaxIter',0); fmincon('lsqcon',x0,ones(size(x0(:)))',sum(x0),[],[],[],[],'',fopts,U,x,yf); end % fit the user-defined model Uf = leastsq(U0,x,yf); figure; hold on; if size(x,2) > 1 %% plot all input factors plot(x(:,1),x(:,2:end)); end plot(x(:,1),eval(U,x),x(:,1),eval(Uf,x)); % display objects at the command-line U U0 Uf % display info text(0.05,0.25,char(Uf,1),'Units','norm'); lab= labels(U); if length(lab)~=np error(message('mbc:xregusermod:InvalidModel6')); end text(0.05,0.15,sprintf('%s ',lab{:}),'Units','norm'); text(0.05,0.05,str_func(Uf),'Units','norm'); % make a localusermod model Ulr= localusermod(Uf); % local regression checks [rf,dG]= rfvals(Uf); if ~isempty(rf) if ~any(size(rf)==1) error(message('mbc:xregusermod:InvalidModel7')) end if size(dG,1)~=length(rf) || size(dG,2)~=np error(message('mbc:xregusermod:InvalidModel8', length( rf ), np)) end % test delG/delb b= double(Uf); ui= U; dGn=zeros(size(dG)); for i=1:np bi=b; di= max(1e-8,abs(b(i))*1e-8); bi(i)= b(i)+ di; ui.parameters= bi; rfi= rfvals(ui); dGn(:,i)= (rfi(:)-rf(:))/di; end err= norm(dGn-dG)/max(norm(dG),1); if err>1e-3 warning(message('mbc:xregusermod:InvalidModel9')) end % make sure we can construct a list of features features(Ulr); % add all the user defined response features to localmod Ulr= AddFeat(Ulr,zeros(length(rf),1),np+1:np+length(rf)); % evaluate features evalfeatures(Ulr); % what combinations can you use for reconstruction selrf= SelectRF(Ulr); if size(selrf,1)==1 error(message('mbc:xregusermod:InvalidModel10')); end end % turn off error U0 = ErrorStatus(U0,false); % add user-defined model to mbc preferences modelcfg(U0,'add'); disp('Model successfully registered for Model-Based Calibration Toolbox software') OK=1;