home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 4.ddi / OPTIM.DI$ / FSOLVE.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  2.4 KB  |  76 lines

  1. function [x,OPTIONS] = fsolve(FUN,x,OPTIONS,GRADFUN,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10)
  2. %FSOLVE Solves nonlinear equations by a least squares method.
  3. %
  4. %    FSOLVE solves equations of the form:
  5. %              
  6. %    F(X)=0    where F and X may be vectors or matrices.   
  7. %
  8. %    X=FSOLVE('FUN',X0) starts at the matrix X0 and tries to solve the 
  9. %    equations described in FUN. FUN is usually and  M-file which returns 
  10. %    an evaluation of the equations for a particular value of X: F=FUN(X).
  11. %
  12. %    X=FSOLVE('FUN',X0,OPTIONS) allows a vector of optional parameters to
  13. %    be defined. OPTIONS(2) is a measure of the precision required for the 
  14. %    values of X at the solution. OPTIONS(3) is a measure of the precision
  15. %    required of the objective function at the solution. See HELP FOPTIONS. 
  16. %
  17. %    X=FSOLVE('FUN',X0,OPTIONS,'GRADFUN') enables a function'GRADFUN'
  18. %    to be entered which returns the partial derivatives of the functions,
  19. %    dF/dX, (stored in columns) at the point X: gf = GRADFUN(X).
  20. %
  21. %    The default algorithm is the Gauss-Newton method with a 
  22. %    mixed quadratic and cubic line search procedure.  A Levenberg-Marquardt 
  23. %    method is selected by setting  OPTIONS(5)=1. 
  24.  
  25. %    Copyright (c) 1990 by the MathWorks, Inc.
  26. %    Andy Grace 7-9-90.
  27.  
  28. %    X=FSOLVE('FUN',X,OPTIONS,GRADFUN,P1,P2,..) allows
  29. %    coefficients, P1, P2, ... to be passed directly to FUN:
  30. %    F=FUN(X,P1,P2,...). Empty arguments ([]) are ignored.
  31.  
  32. % Handle undefined arguments
  33. if nargin<4
  34.     GRADFUN=[];
  35.     if nargin<3
  36.         OPTIONS=[];
  37.     end
  38. end
  39.  
  40. % Check if optimization toolbox is on path 
  41. if exist('leastsq') 
  42.     if length(GRADFUN)  & ~isstr(GRADFUN) 
  43.         disp('The user-supplied gradient (Jacobian) must be a string.');
  44.         error('The syntax to fsolve has been changed  - refer to the Optimization Toolbox guide');
  45.     end
  46.  
  47.     if length(OPTIONS)<5; 
  48.         OPTIONS(5)=0; 
  49.     end
  50. % Switch methods making Gauss Newton the default method.
  51.     if OPTIONS(5)==0; OPTIONS(5)=1; else OPTIONS(5)=0; end
  52.  
  53.     evalstr='leastsq(FUN,x,OPTIONS,GRADFUN';
  54.  
  55.     for i=1:nargin - 4
  56.         evalstr = [evalstr,',P',int2str(i)];
  57.     end
  58.     evalstr = [evalstr, ')'];
  59.  
  60.     [x, OPTIONS] = eval(evalstr);
  61.  
  62.     if OPTIONS(8)>10*OPTIONS(3) & OPTIONS(1)>0
  63.         disp('Optimizer is stuck at a minimum that is not a root')
  64.         disp('Try again with a new starting guess')
  65.     end
  66.  
  67. else
  68. % The old syntax:
  69.     evalstr = 'fsolve2(FUN,x,OPTIONS,GRADFUN';
  70.     for i=1:nargin - 4
  71.         evalstr = [evalstr,',P',int2str(i)];
  72.     end
  73.     evalstr = [evalstr, ')'];
  74.     [x, OPTIONS] = eval(evalstr);
  75. end
  76.