%     Testexample for Nlscon: Least Squares Approximation of
%     measurement points with an Exponential of Negative Square
%     curve.
%
%*  Written by        L. Weimann 
%*  Purpose           Testexample for code NLSCON
%*  Version           2.3
%*  Revision          June 2004
%*  Latest Change     June 2004
%*  Library           CodeLib
%*  Code              Matlab 6.0
%*  Environment       Standard Matlab 6.0 environment on PC's,
%                     workstations and hosts.
%
%     ____________________________________________________________
  fidout = fopen('nlscon.m.out','w');
  fiddat = fopen('nlscon.m.dat','w');
  fprintf(1,'%s\n',' monitor: nlscon.m.out , data: nlscon.m.dat') ;
  
  rtol = 1.0e-4 ;
%     Execution mode: 0=Standard Mode, 1=Stepwise mode
  iopt.mode = 1 ;
%       Jacobian: 0=(same as value 3)
%                 1=supplied by user routine JAC
%                 2=computed by numerical differentation (no feedback) 
%                 3=computed by numerical differentation (with feedback)
  iopt.jacgen = 1 ;
%     A posteriori statistical analysis: 0 = no, 1 = yes
  iopt.mprsta = 1 ;
%     Problem classification:
%     1 = linear , 2 = mildly nonlinear  3 = highly nonlinear
  iopt.nonlin = 3 ;
%     Broyden updates: 0 = inhibit, 1=allow
  iopt.qrank1 = 0 ;
%     Set output level on various units
  iopt.mprerr = 3 ;
  iopt.mprmon = 3 ;
  iopt.mprsol = 2 ;
  iopt.mprtim = 1 ;
%     Set output units
  iopt.luerr = fidout ;
  iopt.lumon = fidout ;
  iopt.lusol = fiddat ;
  iopt.lutim = fidout ;
%     Automatic row scaling of linear system (constraints)
%     and user scaling (measurements):
%     0 = allowed , 1 = inhibited
  iopt.norowscal = 0 ;
  
  p = [] ;
  [x,fobs] = feval('problem',0,'init',p) ;
  fprintf(fidout,' Simulated  experimential  data  :%s\n',' ') ;
  fprintf(fidout,' %10.7f\n',fobs) ;
  fprintf(fidout,'%s\n\n\n',' ') ;
  xscal = zeros(size(x)) ;
  fscal = zeros(size(fobs)) ;
  f = feval('problem',x,'',p) ;
  m = length(f) ;
  info.ierr = -1 ;
  wk = [] ;
  i = 0 ;
  stime = cputime ;
  while info.ierr == -1 
	[x,info,wk] = nlscon(m,'problem',x,xscal,fobs,fscal,rtol,iopt,p,wk);
	i = i+1 ;
	fprintf(fidout,'Returned from call %i of NLSCON\n',i) ;
  end
  etime = cputime ;
  cptime = etime-stime ;
  if cptime ~= 0
	fprintf(fidout,'Time used = %9.3f Sec\n\n',cptime) ;
  end
