lnsrch1
PURPOSE
Copyright (C) 2001 Michel Juillard
SYNOPSIS
function [x,f,fvec,check]=lnsrch(xold,fold,g,p,stpmax,func,j1,j2,varargin)
DESCRIPTION
CROSS-REFERENCE INFORMATION
This function calls:
- check Copyright (C) 2001 Michel Juillard
This function is called by:
- solve1 Copyright (C) 2001 Michel Juillard
SOURCE CODE
0001
0002
0003 function [x,f,fvec,check]=lnsrch(xold,fold,g,p,stpmax,func,j1,j2,varargin)
0004
0005 alf = 1e-4 ;
0006 tolx = 3.7e-11 ;
0007 alam = 1;
0008
0009 x = xold;
0010 nn = length(j2);
0011 summ = sqrt(sum(p.*p)) ;
0012 if ~isfinite(summ)
0013 error(['Some element of Newton direction isn''t finite. Jacobian maybe' ...
0014 ' singular or there is a problem with initial values'])
0015 end
0016
0017 if summ > stpmax
0018 p=p.*stpmax/summ ;
0019 end
0020
0021 slope = g'*p ;
0022
0023 test = max(abs(p)'./max([abs(xold(j2))';ones(1,nn)])) ;
0024 alamin = tolx/test ;
0025
0026 if alamin > 0.1
0027 alamin = 0.1;
0028 end
0029
0030 while 1
0031 if alam < alamin
0032 check = 1 ;
0033 return
0034 end
0035
0036 x(j2) = xold(j2) + (alam*p) ;
0037 fvec = feval(func,x,varargin{:}) ;
0038 fvec = fvec(j1);
0039 f = 0.5*fvec'*fvec ;
0040
0041 if any(isnan(fvec))
0042 alam = alam/2 ;
0043 alam2 = alam ;
0044 f2 = f ;
0045 fold2 = fold ;
0046 else
0047
0048 if f <= fold+alf*alam*slope
0049 check = 0;
0050 break ;
0051 else
0052 if alam == 1
0053 tmplam = -slope/(2*(f-fold-slope)) ;
0054 else
0055 rhs1 = f-fold-alam*slope ;
0056 rhs2 = f2-fold2-alam2*slope ;
0057 a = (rhs1/(alam^2)-rhs2/(alam2^2))/(alam-alam2) ;
0058 b = (-alam2*rhs1/(alam^2)+alam*rhs2/(alam2^2))/(alam-alam2) ;
0059 if a == 0
0060 tmplam = -slope/(2*b) ;
0061 else
0062 disc = (b^2)-3*a*slope ;
0063
0064 if disc < 0
0065 error ('Roundoff problem in nlsearch') ;
0066 else
0067 tmplam = (-b+sqrt(disc))/(3*a) ;
0068 end
0069
0070 end
0071
0072 if tmplam > 0.5*alam
0073 tmplam = 0.5*alam;
0074 end
0075
0076 end
0077
0078 alam2 = alam ;
0079 f2 = f ;
0080 fold2 = fold ;
0081 alam = max([tmplam;(0.1*alam)]) ;
0082 end
0083 end
0084 end
0085
0086
0087
0088
Generated on Fri 16-Jun-2006 09:09:06 by m2html © 2003