% Levenberg-Marquardt Demo using lsqnonlin.

%************ OPTIONS ***************

% Choose the Gauss-Newton option:
options = optimset('LargeScale', 'off');
options = optimset(options, 'LevenbergMarquardt', 'on');

% Allow for a user-defined Jacobian:
options = optimset(options, 'Jacobian', 'on');

% Set the line search algorithm for the step size calculation:
options = optimset(options, 'LineSearchType', 'quadcubic');

% Set the max number of function evaluations.
% Note: This includes function evaluations used
% for finite-differencing and line search steps.
options = optimset(options, 'MaxFunEvals', 90);

% Turn on the display of iterative output:
options = optimset(options, 'Display', 'iter'); 

% Turn on the display of diagnostic information:
options = optimset(options, 'Diagnostics', 'on'); 

%********* END OPTIONS **********

% Plot the objective (Rosenbrock's function):
rbplot
title('Function Evaluations: Major & Minor Iterations')

% Set the initial value and run the optimization:
x0 = [-1.9, 2];
global iterations
iterations = x0;
[x, resnorm, residual, exitflag, output, lambda, jacobian] = ...
    lsqnonlin(@rbfun2, x0, [ ], [ ], options)

% Plot the iterations.
% Note: This includes function evaluations used
% for finite-differencing and line search steps.
plot(iterations(:,1), iterations(:,2), ...
      'ko', 'MarkerSize', 3, 'MarkerFaceColor', 'r')
for i = 1:length(iterations)
plot3(iterations(i,1), iterations(i,2), ...
        rbfun([iterations(i,1), iterations(i,2)]) + 2000, ...
        'ko', 'MarkerSize', 3, 'MarkerFaceColor', 'w')
end

clear i rblim iterations