function [p,status] = fpoint(A,G,d,p0)
%FPOINT Compute a feasible point for mtsvdcstr and tikhcstr.
%
% [p,status] = fpoint(A,G,d,p0);
%
% Finds a feasible point p such that
%    A p = 0   and   G p >= d ,
% using p0 as an initial guess.
%
% If status = 'feasible', then p is a feasible point.

% Ann-Charlotte Berglund, IMM and UNI-C, June 28, 1999.

% Initialization.
status = 'feasible';
[nEqCstr,nVar] = size(A);
nCstr = nEqCstr + size(G,1);
if (nargin < 4 | isempty(p0)), p0 = zeros(nVar,1); end

% Start by checking whether initial guess p0 is feasible.
% It is assumed that p0 lies in the null space of A such that A*p0 = 0.
minCstr = min(G*p0 - d);
if  minCstr > -eps
  p = p0;
  return
end

% If p0 is not feasible then try to find a feasible point p.
G = [[A;G;zeros(1,nVar)],[zeros(nEqCstr,1);ones(nCstr+1-nEqCstr,1)]];
d = [zeros(nEqCstr,1);d];
pTmp = tcsub([],[zeros(nVar,1);1],G,[d;-1e-5], ...
	        [p0;-minCstr],nEqCstr,'fpoint',size(G,1),nVar+1);
         
% Element no. nVar+1 of pTmp measures the residual. ???
if pTmp(nVar+1) > eps
  % No feasible solution found.
  status = 'infeasible';
  p = NaN*ones(size(p0));
else
  % First elements constitute a feasible point.
  p = pTmp(1:nVar);
end