function [nodes,w,geff,momerr,dbox] = dNORD(deg,X,gefftol,maxit,...
    LHDM_options,verbose)
%--------------------------------------------------------------------------
% Object:
% This routine computes a Near Optimal Regression Design with a given
% G-efficiency on a discrete d-dim set X, by a basic multiplicative
% algorithm and Caratheodory-Tchakaloff discrete measure compression
%--------------------------------------------------------------------------
% Input:
% deg: total degree of the algebraic regression polynomial; 
% X: d-column array of set points coordinates;
% * gefftol: G-efficiency threshold;
% * maxit: maximum number of iterations of the multiplicative algorithm;
% * LHDM_options: struct containing the values of optimization parameters.
%
% Note: the variables with an asterisk "*" are not mandatory and can be 
% also set as empty matrix.
%--------------------------------------------------------------------------
% Output:
% nodes,w: compressed near optimal design points and weights; "cpts" is a
%     subset of "X";
% geff: G-efficiency of the output design;
% momerr: moment reconstruction error by the compressed measure.
% dbox: variable that defines the smallest hyperectangle with sides
%     parallel to the axis, containing the domain (or the pointset "X", in
%     the discrete case).
%--------------------------------------------------------------------------
% Dates:
% Written by M. Dessole, F. Marcuzzi, M. Vianello - July 2020
%
% Modified by M. Dessole, A. Sommariva, M. Vianello - November 2020
%--------------------------------------------------------------------------

% .........................  Function Body ................................

% ..... troubleshooting .....

if nargin < 6, verbose=[]; end
if nargin < 5, LHDM_options=[]; end
if nargin < 4, maxit=[]; end
if nargin < 3, gefftol=[]; end

if isempty(verbose), verbose=0; end
if isempty(maxit), maxit=10000; end
if isempty(gefftol), gefftol=0.95; end



% ..... main code below .....


% initializing the probability weights
M=length(X(:,1));
w=ones(M,1)/M;

nit=0; go=1;
tic;
% Titterington multiplicative iteration up to the given G-efficiency 
while go==1
    nit=nit+1;
    if nit==1 
        [U,jvec]=dORTHVAND(deg,X,w,[],[],[]);
        % dimension of the polynomial space on X 
        N=length(jvec);
    else
        U=dORTHVAND(deg,X,w,jvec,[],[]);
    end
    % updating the design 
    K=sum((U.*U)');
    % computing G-efficiency of the design  
    geff=N/max(K);
    % updating the cicle exit condition
    go=(geff<gefftol & nit<maxit);
    if go==1
        % updating the design
        w=w.*K'/N;
    end
end
elapsed = toc;
if verbose
    fprintf('Titterington number of iterations = %d\n',nit);
    fprintf('Titterington elapted time = %f s\n',elapsed);
end

% Caratheodory-Tchakaloff design compression 
[nodes,w,momerr,dbox]=dCATCH(2*deg,X,w,LHDM_options,verbose);




