%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LSSCNL subroutine
% Constrained nonlinear problem
% 
%   x_i:   nx1     matrix, initial guess
%   W:     qxq     matrix, the weight function
%   q:     dimension of function phi
%   n:     number of design variables
%   ell:   number of constraints
%   tolx:  scalar, tolerance for dx
%   tolh:  scalar, tolerance for h
%   Max_i: scalar, maximum number of iterations
% 
% Jean-François Gauthier : 12-15-06
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function[x_optimal,f_min] = LSSCNL(x_i,W,q,n,ell,tolx,tolh,Max_i,ODA)

x       = x_i;
i       = 0;
norm_dx = 1;
norm_h  = 1;

% Initialisation of phi, PHI, h and J
phi = phi_vector(x,q,n);          % FUNPHI
PHI = Phi_matrix(x,q,n);          % DPHIDX
h   = h_vector(x,q,n,ell);        % FUNH
J   = J_matrix(x,q,n,ell);        % DHDX

mphi = (-1)*phi;
mh   = (-1)*h;

while(norm_dx >= tolx && norm_h >= tolh && i <= Max_i)
    
    dx = LSSCLS(mphi,PHI,W,mh,J,q,n,ell,ODA); 
    
    x = x + dx;
    
    phi = phi_vector(x,q,n);      % FUNPHI
    PHI = Phi_matrix(x,q,n);      % DPHIDX
    h   = h_vector(x,q,n,ell);    % FUNH
    J   = J_matrix(x,q,n,ell);    % DHDX
    
    mphi = (-1)*phi;
    mh   = (-1)*h;
    
    norm_dx = norm(dx);
    norm_h  = norm(h);

    i = i+1;
end

x_optimal = x;
f_min = (1/q)*(phi')*W*phi;
