%% Examine the Solution Process
% Examine the solution process both as it occurs (by setting the Display option to 'iter') and afterward (by examining the output structure).
% Suppose that you have observation time data xdata and observed response data ydata, and you want to find parameters x(1) and x(2) to fit a model of the form
%  \hbox{ydata} = x(1)\exp\left(x(2)\hbox{xdata}\right).
%
% Input the observation times and responses.

xdata = ...
 [0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
ydata = ...
 [455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];

%
% Create a simple exponential decay model. The model computes a vector of differences between predicted values and observed values.

fun = @(x)x(1)*exp(x(2)*xdata)-ydata;

%
% Fit the model using the starting point x0 = [100,-1]. Examine the solution process by setting the Display option to 'iter'. Obtain an output structure to obtain more information about the solution process.

x0 = [100,-1];
options = optimoptions('lsqnonlin','Display','iter');
[x,resnorm,residual,exitflag,output] = lsqnonlin(fun,x0,[],[],options);

%
% Examine the output structure to obtain more information about the solution process.

output

%
% For comparison, set the Algorithm option to 'levenberg-marquardt'.

options.Algorithm = 'levenberg-marquardt';
[x,resnorm,residual,exitflag,output] = lsqnonlin(fun,x0,[],[],options);

%
% The 'levenberg-marquardt' converged with fewer iterations, but almost as many function evaluations:

output

% Copyright 2015 The MathWorks, Inc.