The script generates xdata from 100 independent
samples from an exponential distribution with mean 2. It generates ydata from Equation 10-25 using a = [1;3;2], perturbed by adding normal
deviates with standard deviations [0.1;0.5;0.5].

The goal is to find parameters a^i, i = 1, 2, 3,
for the model that best fit the data.

In order to fit the parameters to the data using lsqcurvefit,
you need to define a fitting function. Define the fitting function predicted as
an anonymous function:

predicted = @(a,xdata) a(1)*ones(100,1)+a(2)*exp(-a(3)*xdata);

To fit the model to the data, lsqcurvefit needs
an initial estimate a0 of the parameters. Enter

a0 = [2;2;2];

Run the solver lsqcurvefit as follows:

[ahat,resnorm,residual,exitflag,output,lambda,jacobian] =...
lsqcurvefit(predicted,a0,xdata,ydata);
Local minimum possible.
lsqcurvefit stopped because the final change in the
sum of squares relative to its initial value is
less than the default value of the function tolerance.

To see the resulting least-squares estimate of a^, enter:

ahat
ahat =
1.0169
3.1444
2.1596

The fitted values ahat are within 8% of a = [1;3;2].

If you have Statistics and Machine Learning Toolbox™ software, use the nlparci function
to generate confidence intervals for the ahat estimate.

Was this topic helpful?

Select Your Country

Choose your country to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .