www.gusucode.com > mbcmodels 工具箱 matlab 源码程序 > mbcmodels/@xregcubic/getScript.m

    function Y = getScript(m)
%getScript Creates the script to evaluate a polynomial
% Generates a script for polynomial evaluation without using recursion.
%
% s = getScript(M) returns a script as a char array that
% evaluates a polynomial model M without recursion.

%  Copyright 2013 The MathWorks, Inc. and Ford Global Technologies, Inc.
coeffs = double(m);
orderM = order(m);
reorderM = reorder(m);
maxInteractions = get(m, 'maxinteract');

header = {
    'function Y = fcn(X, coeffs)'
    '%#codegen'
    ''
    makeHelp(m)
    sprintf('%% Autogenerated by the Model-Based Calibration Toolbox on %s.', datestr(now));
    };
h = sprintf('%s\n', header{:});

if maxInteractions<0
    maxInteractions = 0;
end
 
if max(orderM) == 1 % 1-D case
    Y = horners1DCase(orderM);
    Y = sprintf('%s\n\nY = %s;', h, Y);
else % multi-D case
    % calculate terms upto interaction limit
    currpos = 1;
    Y = sprintf('Y = coeffs(1);');
    [Y,currpos] = recursiveEval(Y, 1, 1, orderM, maxInteractions, currpos, reorderM, coeffs);
    
    % add power terms outside of interactions
    numInteractions = length(orderM);
    numFactors = get(m, 'nfactors');
    if maxInteractions<numInteractions
        higherOrder = higherOrderEval('', numInteractions, numFactors, orderM, maxInteractions, currpos, reorderM);
        if ~isempty(higherOrder)
            Y = sprintf('%s\nY = Y + %s;', Y, higherOrder(4:end));
        end
    end
    
    Y = sprintf('%s\n%s', h, Y);
end


function helpText = makeHelp(m)
helpText = sprintf('%% This function specifies a polynomial with %d inputs: \n', get(m,'nfactors'));
symbol = get(m, 'symbols');
order = get(m, 'order');
for i = 1:get(m,'nfactors')
    helpText = sprintf('%s%% \t%s\t = order %d\n', helpText, symbol{i}, order(i));    
end
helpText = sprintf('%s%% Maximum interaction order = %d', helpText, get(m,'maxInteract'));

textEq = char(m,1,false);
commentedTextEq = sprintf('%%\n%% The model definition is:');
for i=1:size(textEq, 1)
    commentedTextEq = sprintf('%s\n%% %s', commentedTextEq, textEq(i,:));
end

helpText = sprintf('%s\n%s\n', helpText, commentedTextEq);


function Y = horners1DCase(order)
Y = [];
currpos = length(order)+1;
for i=length(order):-1:1
    if order(i) == 1 
        if isempty(Y)
            Y = sprintf('X(1)*coeffs(%d)', currpos);
        else
            Y = sprintf('X(1)*(coeffs(%d) + %s)', currpos, Y);
        end
    else
        if ~isempty(Y)
            Y = sprintf('X(1)*%s', Y);
        end
    end
    currpos = currpos-1;
end
Y = sprintf('coeffs(%d) + %s', currpos, Y);


function [evalIn,currpos] = recursiveEval(evalIn, currOrder, startIndex, order, maxInteractions, currpos, reorder, coeffs)
for i = startIndex:order(currOrder)
     % this switch takes care of interaction=0 case 
     if (maxInteractions > 0) 
        currpos = currpos+1;
        v = sprintf('coeffs(%d)', currpos);
        if (currOrder < maxInteractions)
            % increase poly order (level) 
            [y,currpos] = recursiveEval(v, currOrder+1, i, order, maxInteractions, currpos, reorder, coeffs);
            y = sprintf('(%s)', y);
        else
            y = v;
        end
        if (currOrder == 1)
            evalIn = sprintf('%s\nY = Y + X(%d)*%s;', evalIn, reorder(i), y);
        else
            evalIn = sprintf('%s + X(%d)*%s', evalIn, reorder(i), y);
        end
    end
end

function Y = higherOrderEval(Y, maxOrder, numFactors, order, maxInteractions, currpos, reorder)
for pow=1:maxOrder
    for i=1:numFactors
        if pow>maxInteractions && i<=order(pow)
            currpos = currpos+1;
            XPows = sprintf('X(%d)^%d', reorder(i), pow);
            Y = sprintf('%s + coeffs(%d)*%s', Y, currpos, XPows);
        end
    end
end