Commit 6180bc22 authored by Houtan Bastani's avatar Houtan Bastani

Add TZcode submodule to repository

parent 9576255e
......@@ -4,3 +4,6 @@
[submodule "contrib/ms-sbvar/switch_dw"]
path = contrib/ms-sbvar/switch_dw
url = http://www.dynare.org/git/frbatlanta/switch_dw.git
[submodule "contrib/ms-sbvar/TZcode"]
path = contrib/ms-sbvar/TZcode
url = http://www.dynare.org/git/frbatlanta/TZcode.git
TZcode @ 1673c6f7
Subproject commit 1673c6f7170ac61ad3d3cb832eb3f038c8e4b3ef
......@@ -152,13 +152,83 @@ Copyright: 1996 Christopher Sims
2003 Karibzhanov, Waggoner and Zha
License: GPL-3+
Files: matlab/ms-sbvar/cstz/*
Copyright: 1993-2011 Tao Zha
Files: contrib/ms-sbvar/TZcode/*
Copyright: 1997-2012 Tao Zha
License: GPL-3+
Files: matlab/ms-sbvar/cstz/bfgsi.m matlab/ms-sbvar/cstz/csminit.m
matlab/ms-sbvar/cstz/csminwel.m
Copyright: 1993-2011 Tao Zha and Christopher Sims
Files: contrib/ms-sbvar/TZcode/MatlabFiles/szbvar.m
contrib/ms-sbvar/TZcode/MatlabFiles/phg233.m
contrib/ms-sbvar/TZcode/MatlabFiles/phg235.m
contrib/ms-sbvar/TZcode/MatlabFiles/pwf234.m
contrib/ms-sbvar/TZcode/MatlabFiles/pmddf235.m
contrib/ms-sbvar/TZcode/MatlabFiles/pmddf234.m
contrib/ms-sbvar/TZcode/MatlabFiles/pmddf236.m
contrib/ms-sbvar/TZcode/MatlabFiles/csminwel.m
contrib/ms-sbvar/TZcode/MatlabFiles/phg234.m
contrib/ms-sbvar/TZcode/MatlabFiles/pwf235.m
contrib/ms-sbvar/TZcode/MatlabFiles/mnpdf.m
contrib/ms-sbvar/TZcode/MatlabFiles/szasbvar.m
Copyright: 1997-2012 Christopher A. Sims and Tao Zha
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/pmddf233.m
contrib/ms-sbvar/TZcode/MatlabFiles/csminit.m
contrib/ms-sbvar/TZcode/MatlabFiles/gensysoldversion.m
contrib/ms-sbvar/TZcode/MatlabFiles/gensys_z2new.m
contrib/ms-sbvar/TZcode/MatlabFiles/gensysct.m
contrib/ms-sbvar/TZcode/MatlabFiles/csminitworksuntiil0205.m
contrib/ms-sbvar/TZcode/MatlabFiles/gensys_z2.m
contrib/ms-sbvar/TZcode/MatlabFiles/qzdiv.m
contrib/ms-sbvar/TZcode/MatlabFiles/pwf233.m
Copyright: 1997-2012 Christopher A. Sims
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/srestrictrwzalg.m
Copyright: 1997-2012 Juan Rubio-Ramirez, Daniel Waggoner and Tao Zha
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/clmonq.m
contrib/ms-sbvar/TZcode/MatlabFiles/clgls.m
Copyright: 1997-2012 Eric Leeper and Tao Zha
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/a0lhfun.m
Copyright: 1997-2012 Eric Leeper
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/suptitle.m
Copyright: 1997-2012 Drea Thomas
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/fn_gibbsrvar.m
contrib/ms-sbvar/TZcode/MatlabFiles/gibbsvar.m
contrib/ms-sbvar/TZcode/MatlabFiles/simtanzphi.m
contrib/ms-sbvar/TZcode/MatlabFiles/fn_gibbsglb.m
contrib/ms-sbvar/TZcode/MatlabFiles/fn_gibbsrvar_setup.m
contrib/ms-sbvar/TZcode/MatlabFiles/gibbsglb.m
contrib/ms-sbvar/TZcode/MatlabFiles/fn_gibbsrvaroldworks.m
Copyright: 1997-2012 Daniel Waggoner and Tao Zha
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/gensys.m
Copyright: 1996-2012 Christopher A. Sims
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/xydata.m
Copyright: 1997-2012 Lutz Kilian and Tao Zha
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/bfgsi.m
Copyright: 1996 Christopher A. Sims
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/qplot2.m
contrib/ms-sbvar/TZcode/MatlabFiles/ellipse.m
Copyright: 1997-2012 Clark A. Burdick
License: GPL-3+
Files: contrib/ms-sbvar/TZcode/MatlabFiles/chol2.m
Copyright: 1996-2012 Tao Zha
License: GPL-3+
License: GFDL-NIV-1.3+
......
......@@ -51,8 +51,8 @@ addpath([dynareroot '/kalman/likelihood'])
addpath([dynareroot '/AIM/'])
addpath([dynareroot '/partial_information/'])
addpath([dynareroot '/ms-sbvar/'])
addpath([dynareroot '/ms-sbvar/cstz/'])
addpath([dynareroot '/ms-sbvar/identification/'])
addpath([dynareroot '../contrib/ms-sbvar/TZcode/MatlabFiles/'])
addpath([dynareroot '/parallel/'])
addpath([dynareroot '/particle/'])
addpath([dynareroot '/gsa/'])
......
function H = bfgsi(H0,dg,dx)
% H = bfgsi(H0,dg,dx)
% dg is previous change in gradient; dx is previous change in x;
% 6/8/93 version that updates inverse hessian instead of hessian
% itself.
% Copyright by Christopher Sims 1996. This material may be freely
% reproduced and modified.
dispIndx = 0; % 1: turn on all the diplays on the screen; 0: turn off (Added by T. Zha)
% Copyright (C) 1996-2011 Tao Zha and Christopher Sims
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if size(dg,2)>1
dg=dg';
end
if size(dx,2)>1
dx=dx';
end
Hdg = H0*dg;
dgdx = dg'*dx;
if (abs(dgdx) >1e-12)
H = H0 + (1+(dg'*Hdg)/dgdx)*(dx*dx')/dgdx - (dx*Hdg'+Hdg*dx')/dgdx;
else
if dispIndx
disp('bfgs update failed.')
disp(['|dg| = ' num2str(sqrt(dg'*dg)) '|dx| = ' num2str(sqrt(dx'*dx))]);
disp(['dg''*dx = ' num2str(dgdx)])
disp(['|H*dg| = ' num2str(Hdg'*Hdg)])
end
H=H0;
end
save H.dat H
function [fhat,xhat,fcount,retcode] = csminit(fcn,x0,f0,g0,badg,H0,varargin)
% [fhat,xhat,fcount,retcode] = csminit(fcn,x0,f0,g0,badg,H0,...
% P1,P2,P3,P4,P5,P6,P7,P8)
% retcodes: 0, normal step. 5, largest step still improves too fast.
% 4,2 back and forth adjustment of stepsize didn't finish. 3, smallest
% stepsize still improves too slow. 6, no improvement found. 1, zero
% gradient.
%---------------------
% Modified 7/22/96 to omit variable-length P list, for efficiency and compilation.
% Places where the number of P's need to be altered or the code could be returned to
% its old form are marked with ARGLIST comments.
%
% Fixed 7/17/93 to use inverse-hessian instead of hessian itself in bfgs
% update.
%
% Fixed 7/19/93 to flip eigenvalues of H to get better performance when
% it's not psd.
%
% Fixed 02/19/05 to correct for low angle problems.
%
%tailstr = ')';
%for i=nargin-6:-1:1
% tailstr=[ ',P' num2str(i) tailstr];
%end
% Copyright (C) 1993-2011 Tao Zha and Christopher Sims
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
dispIndx = 0; % 1: turn on all the diplays on the screen; 0: turn off (Added by T. Zha)
%ANGLE = .03; % when output of this variable becomes negative, we have wrong analytical graident
ANGLE = .005; % works for identified VARs and OLS
%THETA = .03;
THETA = .3; %(0<THETA<.5) THETA near .5 makes long line searches, possibly fewer iterations.
FCHANGE = 1000;
MINLAMB = 1e-9;
% fixed 7/15/94
% MINDX = .0001;
% MINDX = 1e-6;
MINDFAC = .01;
fcount=0;
lambda=1;
xhat=x0;
f=f0;
fhat=f0;
g = g0;
gnorm = norm(g);
%
if (gnorm < 1.e-12) && ~badg % put ~badg 8/4/94
retcode =1;
dxnorm=0;
% gradient convergence
else
% with badg true, we don't try to match rate of improvement to directional
% derivative. We're satisfied just to get some improvement in f.
%
%if(badg)
% dx = -g*FCHANGE/(gnorm*gnorm);
% dxnorm = norm(dx);
% if dxnorm > 1e12
% disp('Bad, small gradient problem.')
% dx = dx*FCHANGE/dxnorm;
% end
%else
% Gauss-Newton step;
%---------- Start of 7/19/93 mod ---------------
%[v d] = eig(H0);
%toc
%d=max(1e-10,abs(diag(d)));
%d=abs(diag(d));
%dx = -(v.*(ones(size(v,1),1)*d'))*(v'*g);
% toc
dx = -H0*g;
% toc
dxnorm = norm(dx);
if dxnorm > 1e12
if dispIndx, disp('Near-singular H problem.'), end
dx = dx*FCHANGE/dxnorm;
end
dfhat = dx'*g0;
%end
%
%
if ~badg
% test for alignment of dx with gradient and fix if necessary
a = -dfhat/(gnorm*dxnorm);
if a<ANGLE
dx = dx - (ANGLE*dxnorm/gnorm+dfhat/(gnorm*gnorm))*g;
% suggested alternate code: ---------------------
dx = dx*dxnorm/norm(dx); % Added 02/19/05 by CAS. This keeps scale invariant to the angle correction
% ------------------------------------------------
dfhat = dx'*g;
% dxnorm = norm(dx); % Removed 02/19/05 by CAS. This line unnecessary with modification that keeps scale invariant
if dispIndx, disp(sprintf('Correct for low angle: %g',a)), end
end
end
if dispIndx, disp(sprintf('Predicted improvement: %18.9f',-dfhat/2)), end
%
% Have OK dx, now adjust length of step (lambda) until min and
% max improvement rate criteria are met.
done=0;
factor=3;
shrink=1;
lambdaMin=0;
lambdaMax=inf;
lambdaPeak=0;
fPeak=f0;
lambdahat=0;
while ~done
if size(x0,2)>1
dxtest=x0+dx'*lambda;
else
dxtest=x0+dx*lambda;
end
% home
f = feval(fcn,dxtest,varargin{:});
%ARGLIST
%f = feval(fcn,dxtest,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13);
% f = feval(fcn,x0+dx*lambda,P1,P2,P3,P4,P5,P6,P7,P8);
if dispIndx, disp(sprintf('lambda = %10.5g; f = %20.7f',lambda,f )), end
%debug
%disp(sprintf('Improvement too great? f0-f: %g, criterion: %g',f0-f,-(1-THETA)*dfhat*lambda))
if f<fhat
fhat=f;
xhat=dxtest;
lambdahat = lambda;
end
fcount=fcount+1;
shrinkSignal = (~badg & (f0-f < max([-THETA*dfhat*lambda 0]))) | (badg & (f0-f) < 0) ;
growSignal = ~badg & ( (lambda > 0) & (f0-f > -(1-THETA)*dfhat*lambda) );
if shrinkSignal && ( (lambda>lambdaPeak) || (lambda<0) )
if (lambda>0) && ((~shrink) || (lambda/factor <= lambdaPeak))
shrink=1;
factor=factor^.6;
while lambda/factor <= lambdaPeak
factor=factor^.6;
end
%if (abs(lambda)*(factor-1)*dxnorm < MINDX) || (abs(lambda)*(factor-1) < MINLAMB)
if abs(factor-1)<MINDFAC
if abs(lambda)<4
retcode=2;
else
retcode=7;
end
done=1;
end
end
if (lambda<lambdaMax) && (lambda>lambdaPeak)
lambdaMax=lambda;
end
lambda=lambda/factor;
if abs(lambda) < MINLAMB
if (lambda > 0) && (f0 <= fhat)
% try going against gradient, which may be inaccurate
if dispIndx, lambda = -lambda*factor^6, end
else
if lambda < 0
retcode = 6;
else
retcode = 3;
end
done = 1;
end
end
elseif (growSignal && lambda>0) || (shrinkSignal && ((lambda <= lambdaPeak) && (lambda>0)))
if shrink
shrink=0;
factor = factor^.6;
%if ( abs(lambda)*(factor-1)*dxnorm< MINDX ) || ( abs(lambda)*(factor-1)< MINLAMB)
if abs(factor-1)<MINDFAC
if abs(lambda)<4
retcode=4;
else
retcode=7;
end
done=1;
end
end
if ( f<fPeak ) && (lambda>0)
fPeak=f;
lambdaPeak=lambda;
if lambdaMax<=lambdaPeak
lambdaMax=lambdaPeak*factor*factor;
end
end
lambda=lambda*factor;
if abs(lambda) > 1e20;
retcode = 5;
done =1;
end
else
done=1;
if factor < 1.2
retcode=7;
else
retcode=0;
end
end
end
end
if dispIndx, disp(sprintf('Norm of dx %10.5g', dxnorm)), end
function [fh,xh,gh,H,itct,fcount,retcodeh] = csminwel(fcn,x0,H0,grad,crit,nit,varargin)
%[fhat,xhat,ghat,Hhat,itct,fcount,retcodehat] = csminwel(fcn,x0,H0,grad,crit,nit,varargin)
% fcn: string naming the objective function to be minimized
% x0: initial value of the parameter vector
% H0: initial value for the inverse Hessian. Must be positive definite.
% grad: Either a string naming a function that calculates the gradient, or the null matrix.
% If it's null, the program calculates a numerical gradient. In this case fcn must
% be written so that it can take a matrix argument and produce a row vector of values.
% crit: Convergence criterion. Iteration will cease when it proves impossible to improve the
% function value by more than crit.
% nit: Maximum number of iterations.
% varargin: A list of optional length of additional parameters that get handed off to fcn each
% time it is called.
% Note that if the program ends abnormally, it is possible to retrieve the current x,
% f, and H from the files g1.mat and H.mat that are written at each iteration and at each
% hessian update, respectively. (When the routine hits certain kinds of difficulty, it
% write g2.mat and g3.mat as well. If all were written at about the same time, any of them
% may be a decent starting point. One can also start from the one with best function value.)
% NOTE: The display on screen can be turned off by seeting dispIndx=0 in this
% function. This option is used for the loop operation. T. Zha, 2 May 2000
% NOTE: You may want to change stps to 1.0e-02 or 1.0e-03 to get a better convergence. August, 2006
% Copyright (C) 1993-2011 Tao Zha and Christopher Sims
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
Verbose = 0; % 1: turn on all the diplays on the screen; 0: turn off (Added by T. Zha)
dispIndx = 0; % 1: turn on all the diplays on the screen; 0: turn off (Added by T. Zha)
[nx,no]=size(x0);
nx=max(nx,no);
NumGrad= ( ~ischar(grad) | length(grad)==0);
done=0;
itct=0;
fcount=0;
snit=100;
%tailstr = ')';
%stailstr = [];
% Lines below make the number of Pi's optional. This is inefficient, though, and precludes
% use of the matlab compiler. Without them, we use feval and the number of Pi's must be
% changed with the editor for each application. Places where this is required are marked
% with ARGLIST comments
%for i=nargin-6:-1:1
% tailstr=[ ',P' num2str(i) tailstr];
% stailstr=[' P' num2str(i) stailstr];
%end
f0 = eval([fcn '(x0,varargin{:})']);
%ARGLIST
%f0 = feval(fcn,x0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13);
% disp('first fcn in csminwel.m ----------------') % Jinill on 9/5/95
if f0 > 1e50, disp('Bad initial parameter.'), return, end
if NumGrad
if length(grad)==0
[g badg] = numgradcd(fcn,x0, varargin{:});
%ARGLIST
%[g badg] = numgradcd(fcn,x0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13);
else
badg=any(find(grad==0));
g=grad;
end
%numgradcd(fcn,x0,P1,P2,P3,P4);
else
[g badg] = eval([grad '(x0,varargin{:})']);
%ARGLIST
%[g badg] = feval(grad,x0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13);
end
retcode3=101;
x=x0;
f=f0;
H=H0;
cliff=0;
while ~done
g1=[]; g2=[]; g3=[];
%addition fj. 7/6/94 for control
if dispIndx
disp('-----------------')
disp('-----------------')
%disp('f and x at the beginning of new iteration')
disp(sprintf('f at the beginning of new iteration, %20.10f',f))
%-----------Comment out this line if the x vector is long----------------
disp([sprintf('x = ') sprintf('%15.8g%15.8g%15.8g%15.8g%15.8g\n',x)]);
end
%-------------------------
itct=itct+1;
[f1 x1 fc retcode1] = csminit(fcn,x,f,g,badg,H,varargin{:});
%ARGLIST
%[f1 x1 fc retcode1] = csminit(fcn,x,f,g,badg,H,P1,P2,P3,P4,P5,P6,P7,...
% P8,P9,P10,P11,P12,P13);
% itct=itct+1;
fcount = fcount+fc;
% erased on 8/4/94
% if (retcode == 1) || (abs(f1-f) < crit)
% done=1;
% end
% if itct > nit
% done = 1;
% retcode = -retcode;
% end
if retcode1 ~= 1
if retcode1==2 || retcode1==4
wall1=1; badg1=1;
else
if NumGrad
[g1 badg1] = numgradcd(fcn, x1,varargin{:});
%ARGLIST
%[g1 badg1] = numgradcd(fcn, x1,P1,P2,P3,P4,P5,P6,P7,P8,P9,...
% P10,P11,P12,P13);
else
[g1 badg1] = eval([grad '(x1,varargin{:})']);
%ARGLIST
%[g1 badg1] = feval(grad, x1,P1,P2,P3,P4,P5,P6,P7,P8,P9,...
% P10,P11,P12,P13);
end
wall1=badg1;
% g1
save g1.mat g1 x1 f1 varargin;
%ARGLIST
%save g1 g1 x1 f1 P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13;
end
if wall1 % && (~done) by Jinill
% Bad gradient or back and forth on step length. Possibly at
% cliff edge. Try perturbing search direction.
%
%fcliff=fh;xcliff=xh;
if dispIndx
disp(' ')
disp('************************* Random search. *****************************************')
disp('************************* Random search. *****************************************')
disp(' ')
pause(1.0)
end
Hcliff=H+diag(diag(H).*rand(nx,1));
if dispIndx, disp('Cliff. Perturbing search direction.'), end
[f2 x2 fc retcode2] = csminit(fcn,x,f,g,badg,Hcliff,varargin{:});
%ARGLIST
%[f2 x2 fc retcode2] = csminit(fcn,x,f,g,badg,Hcliff,P1,P2,P3,P4,...
% P5,P6,P7,P8,P9,P10,P11,P12,P13);
fcount = fcount+fc; % put by Jinill
if f2 < f
if retcode2==2 || retcode2==4
wall2=1; badg2=1;
else
if NumGrad
[g2 badg2] = numgradcd(fcn, x2,varargin{:});
%ARGLIST
%[g2 badg2] = numgradcd(fcn, x2,P1,P2,P3,P4,P5,P6,P7,P8,...
% P9,P10,P11,P12,P13);
else
[g2 badg2] = eval([grad '(x2,varargin{:})']);
%ARGLIST
%[g2 badg2] = feval(grad,x2,P1,P2,P3,P4,P5,P6,P7,P8,...
% P9,P10,P11,P12,P13);
end
wall2=badg2;
% g2
if dispIndx, badg2, end
save g2.mat g2 x2 f2 varargin
%ARGLIST
%save g2 g2 x2 f2 P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13;
end
if wall2
if dispIndx, disp('Cliff again. Try traversing'), end
if norm(x2-x1) < 1e-13
f3=f; x3=x; badg3=1;retcode3=101;
else
gcliff=((f2-f1)/((norm(x2-x1))^2))*(x2-x1);
[f3 x3 fc retcode3] = csminit(fcn,x,f,gcliff,0,eye(nx),varargin{:});
%ARGLIST
%[f3 x3 fc retcode3] = csminit(fcn,x,f,gcliff,0,eye(nx),P1,P2,P3,...
% P4,P5,P6,P7,P8,...
% P9,P10,P11,P12,P13);
fcount = fcount+fc; % put by Jinill
if retcode3==2 || retcode3==4
wall3=1; badg3=1;
else
if NumGrad
[g3 badg3] = numgradcd(fcn, x3,varargin{:});
%ARGLIST
%[g3 badg3] = numgradcd(fcn, x3,P1,P2,P3,P4,P5,P6,P7,P8,...
% P9,P10,P11,P12,P13);
else
[g3 badg3] = eval([grad '(x3,varargin{:})']);
%ARGLIST
%[g3 badg3] = feval(grad,x3,P1,P2,P3,P4,P5,P6,P7,P8,...
% P9,P10,P11,P12,P13);
end
wall3=badg3;
% g3
if dispIndx, badg3, end
save g3.mat g3 x3 f3 varargin;
%ARGLIST
%save g3 g3 x3 f3 P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13;
end
end
else
f3=f; x3=x; badg3=1; retcode3=101;
end
else
f3=f; x3=x; badg3=1;retcode3=101;
end
else
% normal iteration, no walls, or else we're finished here.
f2=f; f3=f; badg2=1; badg3=1; retcode2=101; retcode3=101;
end
else
f1=f; f2=f; f3=f; retcode2=retcode1; retcode3=retcode1;
end
%how to pick gh and xh
if f3<f && badg3==0
if dispIndx, ih=3, end
fh=f3;xh=x3;gh=g3;badgh=badg3;retcodeh=retcode3;
elseif f2<f && badg2==0
if dispIndx, ih=2, end
fh=f2;xh=x2;gh=g2;badgh=badg2;retcodeh=retcode2;
elseif f1<f && badg1==0
if dispIndx, ih=1, end
fh=f1;xh=x1;gh=g1;badgh=badg1;retcodeh=retcode1;
else
[fh,ih] = min([f1,f2,f3]);
if dispIndx, disp(sprintf('ih = %d',ih)), end
%eval(['xh=x' num2str(ih) ';'])
switch ih
case 1
xh=x1;
case 2
xh=x2;
case 3
xh=x3;
end %case
%eval(['gh=g' num2str(ih) ';'])
%eval(['retcodeh=retcode' num2str(ih) ';'])
retcodei=[retcode1,retcode2,retcode3];
retcodeh=retcodei(ih);
if exist('gh')
nogh=isempty(gh);
else
nogh=1;
end
if nogh
if NumGrad
[gh badgh] = feval('numgrad',fcn,xh,varargin{:});
else
[gh badgh] = feval('grad', xh,varargin{:});
end
end
badgh=1;