www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgpointconstraint/staticRHSEvaluate.m

    function [out, grad] = staticRHSEvaluate(obj, InputValues, ExprVals, GradVals, GradVars)
%STATICRHSEVALUATE Evaluate the item at a set of inputs and expression values.
%
%   OUT = STATICRHSEVALUATE(OBJ, INPUTVALUES, EXPRVALS) evaluates the right
%   hand side of the constraint item at a set of input values given the set
%   of expression output values EXPRVALS and returns outputs for each input
%   set. INPUTVALUES must be a cell array with the same number of elements
%   as the item has inputs.  Each cell contains a matrix of data for that
%   input.  EXPRVALS must be a cell array with the same number of elements
%   as there are expressions in the expression/function list provided by
%   the GETSTATICEXPRESSIONS method.  The output is a matrix of evaluation
%   values.
%
%   [OUT, GRAD] = STATICRHSEVALUATE(OBJ, INPUTS, EXPRVALS, GRADVALS, GRADVARS)
%   also calculates the gradient of the value, given the gradients of
%   the expression values.  GRAVALS is a cell array the same size as
%   EXPRVALS that contains a 3D matrix for each 2D matrix of expression
%   evaluations in EXPRVALS.  The 3rd dimension contains the gradients with
%   respect to each free variable value.  GRADVARS is a vector of input
%   indices that indicates which inputs the expression gradients have been
%   calculated for.  GRAD should be a 3D matrix with the first two
%   dimensions the same size as OUT, and the 3rd dimension the same size as
%   the 3rd dimension of the expression gradients, i.e. a gradient should
%   be produced for each variable value GRADVARS specifies.

%   Copyright 2005 The MathWorks, Inc.


if nargout>1 && nargin<4
    error(message('mbc:cgpointconstraint:InvalidArgument2'));
end

Sr = cellfun('size', InputValues, 1);
Sc = cellfun('size', InputValues, 2);
if isempty(ExprVals)
    out = zeros(max(Sr), 0);
    if nargin>3
        grad = zeros(max(Sr), 0, sum(Sc(GradVars)));
    else
        grad = [];
    end
else
    out = ExprVals{1};
    % Check whether expression needs repmatting for each output 
    if size(out, 2)~=max(Sc)
        out = repmat(out, 1, max(Sc));
    end

    if nargin>3
        grad = GradVals{1};
        % Check whether expression needs repmatting for each output
        if size(grad, 2)~=max(Sc)
            grad = repmat(grad, 1, max(Sc));
        end
    else
        grad = [];
    end
end