Home > classes > @ssm > modifparams.m

modifparams

PURPOSE ^

modifparams enables to options(i_options)y and substitute parameters

SYNOPSIS ^

function varargout = modifparams(varargin)

DESCRIPTION ^

 modifparams enables to options(i_options)y and substitute parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: modifparams enables to options(i_options)y and substitute parameters

 CALL: varargout = modfiparams(varargin)
 [sys_out] = modfiparams(sys, options)

 INPUTS:
 sys -      (array of) ssm objects
 options -   plist of options:
    'KEEP_PARAM' cell array of parameter to keep in the symbollic expression
    'KEEP_PARAM_ORDER' array of 1 / 0 depending whether parameter are kept
     or not
    'SET_PARAM_NAMES' Cell array of parameter names for numerical
     substitutions with user defined values
    'SET_PARAM_VALUES' Array of parameter means for numerical substitutions
    'SET_PARAM_SIGMAS' Array of parameter standard deviation for numerical
     substitutions
    'SET_PARAM_VALUES_ORDER' Array of 1 / 0 depending whether parameter
     means are effectively substituted or not
    'SET_PARAM_SIGMAS_ORDER' Array of 1 / 0 depending whether parameter
     means are effectively substituted or not
    'SYMBOLIC_OUTPUT_ALLOWED' if 0 checks output tensors are double and
     not symbollic arrays

 OUTPUTS:
           The output array are of size Nsys*Noptions
 sys_out -  (array of) ssm objects with substitutions / modifications done
           (case when more output than inputs)
 sys     -  (array of) ssm objects with substitutions / modifications done
           (case when the number of inputs and outputs is the same (numel
           options =1))

 M-FILE INFO: Get information about this methods by calling
              >> ssm.getInfo('modifparams')

              Get information about a specified set-plist by calling:
              >> ssm.getInfo('modifparams', 'Default')

 HISTORY:  17-04-2008 A Grynagier
 01-04-2008 A Grynagier
 26-02-2008 A Grynagier
 29-01-2008 A Grynagier
 Creation

 TODO : Modify for multiple outputs
 detect case when parameter is Set twice in a list
 inplement error message if keep is not empty and no symbollic output is
 allowed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % modifparams enables to options(i_options)y and substitute parameters
0002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0003 %
0004 % DESCRIPTION: modifparams enables to options(i_options)y and substitute parameters
0005 %
0006 % CALL: varargout = modfiparams(varargin)
0007 % [sys_out] = modfiparams(sys, options)
0008 %
0009 % INPUTS:
0010 % sys -      (array of) ssm objects
0011 % options -   plist of options:
0012 %    'KEEP_PARAM' cell array of parameter to keep in the symbollic expression
0013 %    'KEEP_PARAM_ORDER' array of 1 / 0 depending whether parameter are kept
0014 %     or not
0015 %    'SET_PARAM_NAMES' Cell array of parameter names for numerical
0016 %     substitutions with user defined values
0017 %    'SET_PARAM_VALUES' Array of parameter means for numerical substitutions
0018 %    'SET_PARAM_SIGMAS' Array of parameter standard deviation for numerical
0019 %     substitutions
0020 %    'SET_PARAM_VALUES_ORDER' Array of 1 / 0 depending whether parameter
0021 %     means are effectively substituted or not
0022 %    'SET_PARAM_SIGMAS_ORDER' Array of 1 / 0 depending whether parameter
0023 %     means are effectively substituted or not
0024 %    'SYMBOLIC_OUTPUT_ALLOWED' if 0 checks output tensors are double and
0025 %     not symbollic arrays
0026 %
0027 % OUTPUTS:
0028 %           The output array are of size Nsys*Noptions
0029 % sys_out -  (array of) ssm objects with substitutions / modifications done
0030 %           (case when more output than inputs)
0031 % sys     -  (array of) ssm objects with substitutions / modifications done
0032 %           (case when the number of inputs and outputs is the same (numel
0033 %           options =1))
0034 %
0035 % M-FILE INFO: Get information about this methods by calling
0036 %              >> ssm.getInfo('modifparams')
0037 %
0038 %              Get information about a specified set-plist by calling:
0039 %              >> ssm.getInfo('modifparams', 'Default')
0040 %
0041 % HISTORY:  17-04-2008 A Grynagier
0042 % 01-04-2008 A Grynagier
0043 % 26-02-2008 A Grynagier
0044 % 29-01-2008 A Grynagier
0045 % Creation
0046 %
0047 % TODO : Modify for multiple outputs
0048 % detect case when parameter is Set twice in a list
0049 % inplement error message if keep is not empty and no symbollic output is
0050 % allowed
0051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0052 
0053 function varargout = modifparams(varargin)
0054 
0055   %% starting initial checks
0056   utils.helper.msg(utils.const.msg.MNAME, ['running ', mfilename]);
0057 
0058   % Check if this is a call for parameters
0059   if utils.helper.isinfocall(varargin{:})
0060     varargout{1} = getInfo(varargin{3});
0061     return
0062   end
0063 
0064   % Collect input variable names
0065   in_names = cell(size(varargin));
0066   for ii = 1:nargin,in_names{ii} = inputname(ii);end
0067 
0068   % Collect all SSMs and plists
0069   [sys, ssm_invars] = utils.helper.collect_objects(varargin(:), 'ssm', in_names);
0070   pl                = utils.helper.collect_objects(varargin(:), 'plist', in_names);
0071 
0072   % Get defaults:
0073   options = combine(pl, getDefaultPlist('Default'));
0074 
0075   Nsys     = numel(sys);
0076   Noptions = numel(options);
0077   if Nsys ~= 1
0078     error('### Please input (only) one SSM model');
0079   end
0080 
0081   % Decide on a deep copy or a modify, depending on the output
0082   sys = copy(sys, nargout);
0083 
0084   %% begin function body
0085 
0086   for i_sys = 1:Nsys
0087     for i_options = 1 :Noptions
0088       options(i_options) = combine(options(i_options), getDefaultPlist('Default'));
0089       %% Retriveing option fields for operations
0090       KeepParamNamesIni = find(options(i_options),'KEEP_PARAM_NAMES') ;
0091       KeepParamOrder = find(options(i_options),'KEEP_PARAM_ORDER') ;
0092       SetParamNamesIni = find(options(i_options),'SET_PARAM_NAMES') ;
0093       SetParamValuesIni = find(options(i_options),'SET_PARAM_VALUES') ;
0094       SetParamSigmasIni = find(options(i_options),'SET_PARAM_SIGMAS') ;
0095       SetParamValuesOrder = find(options(i_options),'SET_PARAM_VALUES_ORDER') ;
0096       SetParamSigmasOrder = find(options(i_options),'SET_PARAM_SIGMAS_ORDER') ;
0097       SymbolicOutputAllowed = find(options(i_options),'SYMBOLIC_OUTPUT_ALLOWED') ;
0098       %% Retrieving system data
0099       if length(i_options)>1
0100         sys(i_sys, i_options) = sys(i_sys);
0101       end
0102 
0103       %% building updated list for KeepParam...
0104       if isequal(KeepParamOrder,'NONE') || SymbolicOutputAllowed==0
0105         KeepParamNames = {};
0106       elseif isequal(KeepParamOrder,'LIST')
0107         KeepParamNames = KeepParamNamesIni;
0108       elseif isequal(KeepParamOrder,'ALL')
0109         KeepParamNames = sys(i_sys).paramnames;
0110       else
0111         KeepParamNames = {};
0112         j=1;
0113         for i=1:length(KeepParamNamesIni)
0114           if KeepParamOrder(i)
0115             KeepParamNames{j} = KeepParamNamesIni{i}; %#ok<AGROW>
0116             j = j+1;
0117           end
0118         end
0119       end
0120       nKeep = length(KeepParamNames);
0121 
0122       %% building updated list for SetParamValues
0123       if (isequal(SetParamValuesOrder,'LIST'))
0124         SetParamValuesNames = SetParamNamesIni;
0125         SetParamValuesOrder = ones(length(SetParamNamesIni),1);
0126         SetParamValues = SetParamValuesIni;
0127       elseif (isequal(SetParamValuesOrder,'NONE'))
0128         SetParamValuesNames = {};
0129         SetParamValuesOrder = [];
0130         SetParamValues = [];
0131       else
0132         SetParamValuesNames = SetParamNamesIni;
0133         SetParamValues = SetParamValuesIni;
0134       end
0135       nValues = length(SetParamValues);
0136 
0137       %% building updated list for SetParamSigmas
0138       if (isequal(SetParamSigmasOrder,'LIST'))
0139         SetParamSigmasNames = SetParamNamesIni;
0140         SetParamSigmasOrder = ones(length(SetParamNamesIni),1);
0141         SetParamSigmas = SetParamSigmasIni;
0142       elseif (isequal(SetParamSigmasOrder,'NONE'))
0143         SetParamSigmasNames = {};
0144         SetParamSigmasOrder = [];
0145         SetParamSigmas = [];
0146       else
0147         SetParamSigmasNames = SetParamNamesIni;
0148         SetParamSigmas = SetParamSigmasIni;
0149       end
0150       nSigmas = length(SetParamSigmas);
0151 
0152       %% building param position tables
0153       posValues = zeros(nValues,1);
0154       posKeep = zeros(nKeep,1);
0155       posSigmas = zeros(nSigmas,1);
0156       for i=1:sys(i_sys).Nparams
0157         for j=1:nKeep
0158           if isequal(sys(i_sys).paramnames{i},KeepParamNames{j})
0159             posKeep(j) = i;
0160           end
0161         end
0162         for j=1:nValues
0163           if isequal(sys(i_sys).paramnames{i},SetParamValuesNames{j})
0164             posValues(j) = i;
0165           end
0166         end
0167         for j=1:nSigmas
0168           if isequal(sys(i_sys).paramnames{i},SetParamSigmasNames{j})
0169             posSigmas(j) = i;
0170           end
0171         end
0172       end
0173 
0174       %% looking for non exisiting but modified parameter names
0175       if not(nKeep==sum(posKeep>0))
0176         msg = 'error with keep parameter list because one does not exist in system.';
0177         error(msg);
0178       elseif not(nValues==sum(posValues>0))
0179         msg = 'error with SetValues parameter list because one does not exist in system.';
0180         error(msg);
0181       elseif not(nSigmas==sum(posSigmas>0))
0182         msg = 'error with SetSigma list because one does not exist in system.';
0183         error(msg);
0184       end
0185 
0186       %% modifying parameter tables in sys(i_sys) (Setparam list)
0187       for i=1:nValues
0188         if SetParamValuesOrder(i)
0189           sys(i_sys).paramvalues(posValues(i)) = SetParamValues(i);
0190         end
0191       end
0192       for i=1:nSigmas
0193         if SetParamSigmasOrder(i)
0194           sys(i_sys).paramsigmas(posSigmas(i)) = SetParamSigmas(i);
0195         end
0196       end
0197 
0198       %% Numerical value for symbolic variables
0199       for i=1:sys(i_sys).Nparams
0200         if sum(ismember(posKeep, i))==0
0201           cmd1 = ['sym(''', sys(i_sys).paramnames{i}, ''');'];
0202           cmd2 = [sys(i_sys).paramnames{i}, '= sys(i_sys).paramvalues(', num2str(i), ');'];
0203           eval(cmd1);
0204           eval(cmd2);
0205         else
0206           cmd1 = [sys(i_sys).paramnames{i}, '=sym(''', sys(i_sys).paramnames{i}, ''');'];
0207           eval(cmd1);
0208         end
0209       end
0210 
0211       %% keeping only params in 'keep param names'
0212       sys(i_sys,i_options).paramnames  = sys(i_sys,i_options).paramnames(posKeep);
0213       sys(i_sys,i_options).paramvalues = sys(i_sys,i_options).paramvalues(posKeep);
0214       sys(i_sys,i_options).paramsigmas = sys(i_sys,i_options).paramsigmas(posKeep);
0215 
0216       %% evaluation of A, B, C, D matrices
0217       for i_ss=1:sys(i_sys,i_options).Nss
0218         for j_ss =1:sys(i_sys,i_options).Nss
0219           %Mass Matrix
0220           if ~isempty(sys(i_sys,i_options).mmats{i_ss, j_ss})
0221             if ~isnumeric(sys(i_sys,i_options).mmats{i_ss, j_ss})
0222               sys(i_sys,i_options).mmats{i_ss, j_ss} = ...
0223                 eval(sym(sys(i_sys,i_options).mmats{i_ss, j_ss}));
0224             end
0225           end
0226           %A Matrix
0227           if ~isempty(sys(i_sys,i_options).amats{i_ss, j_ss})
0228             if ~isnumeric(sys(i_sys,i_options).amats{i_ss, j_ss})
0229               sys(i_sys,i_options).amats{i_ss, j_ss} = ...
0230                 eval(sym(sys(i_sys,i_options).amats{i_ss, j_ss}));
0231             end
0232           end
0233         end
0234       end
0235       for i_ss=1:sys(i_sys,i_options).Nss
0236         for j_in =1:sys(i_sys,i_options).Ninputs
0237           %B Matrix
0238           if ~isempty(sys(i_sys,i_options).bmats{i_ss, j_in})
0239             if ~isnumeric(sys(i_sys,i_options).bmats{i_ss, j_in})
0240               sys(i_sys,i_options).bmats{i_ss, j_in} = ...
0241                 eval(sym(sys(i_sys,i_options).bmats{i_ss, j_in}));
0242             end
0243           end
0244         end
0245       end
0246       for i_out=1:sys(i_sys,i_options).Noutputs
0247         for j_ss =1:sys(i_sys,i_options).Nss
0248           %C Matrix
0249           if ~isempty(sys(i_sys,i_options).cmats{i_out, j_ss})
0250             if ~isnumeric(sys(i_sys,i_options).cmats{i_out, j_ss})
0251               sys(i_sys,i_options).cmats{i_out, j_ss} = ...
0252                 eval(sym(sys(i_sys,i_options).cmats{i_out, j_ss}));
0253             end
0254           end
0255         end
0256       end
0257       for i_out=1:sys(i_sys,i_options).Noutputs
0258         for j_in =1:sys(i_sys,i_options).Ninputs
0259           %D Matrix
0260           if ~isempty(sys(i_sys,i_options).dmats{i_out, j_in})
0261             if ~isnumeric(sys(i_sys,i_options).dmats{i_out, j_in})
0262               sys(i_sys,i_options).dmats{i_out, j_in} = ...
0263                 eval(sym(sys(i_sys,i_options).dmats{i_out, j_in}));
0264             end
0265           end
0266         end
0267       end
0268       validate(sys(i_sys,i_options));
0269       sys(i_sys,i_options).addHistory(ssm.getInfo(mfilename), options(i_options) , {''}, sys(i_sys,i_options).hist );
0270     end
0271   end
0272   if nargout > 0
0273     varargout{1} = sys;
0274   end
0275 end
0276 
0277 
0278 %--------------------------------------------------------------------------
0279 % Get Info Object
0280 %--------------------------------------------------------------------------
0281 function ii = getInfo(varargin)
0282   if nargin == 1 && strcmpi(varargin{1}, 'None')
0283     sets = {};
0284     pls   = [];
0285   elseif nargin == 1 && ~isempty(varargin{1}) && ischar(varargin{1})
0286     sets{1} = varargin{1};
0287     pls = getDefaultPlist(sets{1});
0288   else
0289     sets = {'Default'};
0290     pls = [];
0291     for kk=1:numel(sets)
0292       pls = [pls getDefaultPlist(sets{kk})];
0293     end
0294   end
0295   % Build info object
0296   ii = minfo(mfilename, 'ssm', '', utils.const.categories.statespace, '$Id: resp.m,v 1.17 2008/07/22 10:22:38 ingo Exp $', sets, pls);
0297 end
0298 
0299 %--------------------------------------------------------------------------
0300 % Get Default Plist
0301 %--------------------------------------------------------------------------
0302 function plo = getDefaultPlist(set)
0303   switch set
0304     case 'Default'
0305       plo = plist( 'KEEP_PARAM_NAMES', {}, 'KEEP_PARAM_ORDER', 'LIST',...
0306         'SET_PARAM_NAMES', {}, 'SET_PARAM_VALUES', [], 'SET_PARAM_SIGMAS', [],...
0307         'SET_PARAM_VALUES_ORDER', 'LIST', 'SET_PARAM_SIGMAS_ORDER', 'NONE', 'SYMBOLIC_OUTPUT_ALLOWED', 1);
0308     otherwise
0309       error('### Unknown parameter set [%s].', set);
0310   end
0311 end

Generated on Mon 08-Sep-2008 13:18:47 by m2html © 2003