Time for action - using leasqr
1. Let us generate data with normally distributed random noise:
octave:23> x=linspace(0, 5); y = 1./(1 + 1.2*x.^1.8) + \ > randn(1,100)*0.03;
2. Then we specify the model using the following function definition:
octave:24> function y = ffun(x, p) > y = 1./(1+p(1)*x.^p(2)); > endfunction
3. Give an initial guess of the parameters α and β:
octave:25> p = [0.5 0.0];
4. We can now fit the model to data:
octave:26> [yfit pfit cvg iter] = leasqr(x, y, p, "ffun");
Easy!
5. We can check whether the fitting algorithm converged or not, and how many iterations it used:
octave:27> cvg, iter cvg = 1 iter = 6
6. The values of the fitted parameters are of course important:
octave:28> pfit p = 1.1962 1.7955
This is very close to the values that we would expect from Command 24. The fit is plotted together with the data in the figure below.
What just happened?
In Command 23, we instantiated the free variable x
, and set it to be a vector with element values...