function Y = uq_limit_state_involving_surrogate(X,P)
% Y = uq_limit_state_involving_surrogate(X,P)
%
%       Computes a limit state function of the form: Y = R - SurrModel(Xinput)
%           where R is a random variable and SurrModel(X) evaluates a
%           pre-constructed surrogate model depending on variables Xinput
% 
%       This function is used as a follow-up example of 'uq_Example_PCK_04_TrussDataSet'
%
%       INPUT:
%                 X:  the sample matrix with N lines and (M+1) columns
%                     The M first columns correspond to the input
%                     parameters of the surrogate model.
%                     The last column are the realisation of the variable R
%      OUTPUT:
%                 P:  the MODEL name of the surrogate model
% 


% Extract first ten-parameter sample (inputs of the surrogate model)
M = 10 ;            % in the truss application example
Xtruss = X(:,1:M);            

% Extract the additional parameter
R = X(:,(M+1));

% P allows to pass the name of the surrogate model
myPCK = P;

% The limit state function is defined as R - SurrModel(Xinput)
Y = R - abs(uq_evalModel(myPCK,Xtruss));
end

