xref: /petsc/src/tao/leastsquares/tutorials/matlab/ProblemFinalize.m (revision c4762a1b19cd2af06abeed90e8f9d34fb975dd94)
1*c4762a1bSJed Brown% Sample calling syntax for testing taopounders and comparing to fminsearch
2*c4762a1bSJed Brown% ProblemFinalize is called after solving the instance
3*c4762a1bSJed Brown
4*c4762a1bSJed Brown% Pad the histroy if there are remaining evaluations or truncate if too many
5*c4762a1bSJed Brownif nfev < nfmax
6*c4762a1bSJed Brown  fvals = [fvals(1:nfev);ones(nfmax-nfev,1)*fvals(nfev)];
7*c4762a1bSJed Brownelse
8*c4762a1bSJed Brown  fvals = fvals(1:nfmax);
9*c4762a1bSJed Brown  fvecs = fvecs(1:nfmax,:);
10*c4762a1bSJed Brown  X_hist = X_hist(1:nfmax,:);
11*c4762a1bSJed Brownend
12*c4762a1bSJed Brown
13*c4762a1bSJed Brown% Store the history and results for taopounders
14*c4762a1bSJed BrownSolverNumber = 1;
15*c4762a1bSJed BrownResults{SolverNumber,np}.alg = 'TAO Pounders';
16*c4762a1bSJed BrownResults{SolverNumber,np}.problem = ['problem ' num2str(np) ' from More/Wild'];
17*c4762a1bSJed BrownResults{SolverNumber,np}.H = fvals;
18*c4762a1bSJed BrownResults{SolverNumber,np}.X = X_hist;
19*c4762a1bSJed BrownResults{SolverNumber,np}.fvecs = fvecs;
20*c4762a1bSJed Brown
21*c4762a1bSJed Brown% Initialize the function handle for evaluating the norm of the residuals
22*c4762a1bSJed Brownfunc = @(x)dfovec_wrap(m,n,x,nprob,0);
23*c4762a1bSJed Brown
24*c4762a1bSJed Brown% Initialize the algorithmic parameters for fminsearch
25*c4762a1bSJed Brownrand('seed',0);
26*c4762a1bSJed Brownoptions = optimset('MaxFunEvals',nfmax,'MaxIter',nfmax);
27*c4762a1bSJed Brown
28*c4762a1bSJed Brown% Reset the global history of the evaluations
29*c4762a1bSJed Brownnfev = 0;
30*c4762a1bSJed Brownfvals = zeros(nfmax,1);
31*c4762a1bSJed Brownfvecs = zeros(nfmax,m);
32*c4762a1bSJed BrownX_hist = zeros(nfmax,n);
33*c4762a1bSJed Brown
34*c4762a1bSJed Brown% Call fminsearch
35*c4762a1bSJed Brownfminsearch(func,X0,options);
36*c4762a1bSJed Brown
37*c4762a1bSJed Brown% Pad the histroy if there are remaining evaluations or truncate if too many
38*c4762a1bSJed Brownif nfev < nfmax
39*c4762a1bSJed Brown  fvals = [fvals(1:nfev);ones(nfmax-nfev,1)*fvals(nfev)];
40*c4762a1bSJed Brownelse
41*c4762a1bSJed Brown  fvals = fvals(1:nfmax);
42*c4762a1bSJed Brown  fvecs = fvecs(1:nfmax,:);
43*c4762a1bSJed Brown  X_hist = X_hist(1:nfmax,:);
44*c4762a1bSJed Brownend
45*c4762a1bSJed Brown
46*c4762a1bSJed Brown% Store the history and results for taopounders
47*c4762a1bSJed BrownSolverNumber = 2;
48*c4762a1bSJed BrownResults{SolverNumber,np}.alg = 'fminsearch';
49*c4762a1bSJed BrownResults{SolverNumber,np}.problem = ['problem ' num2str(np) ' from More/Wild'];
50*c4762a1bSJed BrownResults{SolverNumber,np}.H = fvals;
51*c4762a1bSJed BrownResults{SolverNumber,np}.X = X_hist;
52*c4762a1bSJed BrownResults{SolverNumber,np}.fvecs = fvecs;
53*c4762a1bSJed Brown
54