%     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('nleq1.m.out','w');
  fiddat = fopen('nleq1.m.dat','w');
  fprintf(1,'%s\n',' monitor: nleq1.m.out , data: nleq1.m.dat ') ;
  nmaxp = 9 ;
  ast(1:66) = '*' ;
  for n=2:9
  
    rtol = 1.0e-5 ;
%     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 ;
%       Jacobian storage mode: 0=full matrix, 1=band matrix
    iopt.mstor = 0 ;
%       For a band matrix Jacobian: lower bandwidth
    iopt.ml = 0 ;
%       For a band matrix Jacobian: upper bandwidth
    iopt.mu = 0 ;
%     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 ;
    wk = [] ;
%       Override maximum allowed number of iterations:
    wk.nitmax = 10 ;
%       Override starting damping factor:
%   wk.fcstrt = 1.0e0 ;
%       Override minimal allowed damping factor:
%   wk.fcmin = 1.0e-2 ;
%       Override rank1-decision parameter SIGMA:
%   wk.sigma = 3.0e0 ;
%       Override 'take corrector instead of small predictor'-
%       decision parameter SIGMA2:
%   wk.sigma2 = 3.0e0 ;
    p.n = n ;
%       Starting vector for the iteration
    x = feval('problem',0,'init',p) ;
    xscal = zeros(size(x)) ;
    info.ierr = -1 ;
    i = 0 ;
    stime = cputime ;
    while info.ierr == -1 
      [x,info,wk] = nleq1('problem',x,xscal,rtol,iopt,p,wk) ;
      i = i+1 ;
	  fprintf(fidout,'Returned from call %i of NLEQ1\n',i) ;
    end
    etime = cputime ;
    cptime = etime-stime ;
    if cptime ~= 0
	  fprintf(fidout,'Time used = %9.3f Sec\n\n',cptime) ;
    end
    fprintf(fidout,'%s\n',ast) ;
  end
