% a minimal test of least squares fitting in octave
SNR=100/3; % 3% noise
a=1.2; % "true" values
b=1.8;
x=linspace(0, 5);
y = 1./(1 + a*x.^b) + randn(1,100)*0.05;
pkg load optim
% fit using leasqr (x, y, pin, F), where
% x - Vector (or matrix) of independent variables.
% y - Vector or matrix of observed values.
% pin - Vector of initial parameters to be adjusted by leasqr.
% F - Name of function or function handle.
% The function must be of the form y = f(x, p), with y, x, p of the form y, x, pin.
% Outputs: [f, p, cvg, iter, corp, covp, covr, stdresid, Z, r2] = leasqr (…)
% f - Column vector of values computed: f = F(x,p).
% p - Column vector trial or final parameters, i.e, the solution.
% cvg - Scalar: = 1 if convergence, = 0 otherwise.
% iter - Scalar number of iterations used.
% corp - Correlation matrix for parameters.
% covp - Covariance matrix of the parameters.
% ...
% A simple-minded interpretation of the results of the fit:
% From http://originlab.com/www/helponline/Origin/en/UserGuide/The_Fit_Results.html
% "The square root of a main diagonal value of the covariance matrix is the
% Standard Error of the corresponding parameter"
function y = f(x, p)
y = 1./(1+p(1)*x.^p(2));
end
p_in = [0.5;1];
[yfit p_out cvg iter corp covp] = leasqr(x, y, p_in, "f");
if (cvg == 1)
disp(sprintf("convergence achieved in %d iterations\n initial final +/-",iter))
er=sqrt(diag(covp));
disp([p_in p_out er])
end