Implement a function which makes a least-squares fit of a given data-set, {xi, yi, δyi}i=1...n , with a linear combination F(x)≐∑ k=1..m ck fk(x) of given functions fk(x)|k=1..m .
The parameters to the function should be the data to fit and the set of functions the linear combination of which should fit the data. The function must calculate the vector of the coefficients, ck .
Fit the following data,
with the linear combination of functions
x = 0.100 0.145 0.211 0.307 0.447 0.649 0.944 1.372 1.995 2.900
y = 12.644 9.235 7.377 6.460 5.555 5.896 5.673 6.964 8.896 11.355
dy = 0.858 0.359 0.505 0.403 0.683 0.605 0.856 0.351 1.083 1.002
{1/x, 1, x}.
The fitting functions {fk(x)} can be implemented, e.g., as
a vector of functions (C++11),
std::vector<std::function<double(double)>> fitfunctions = {f1,...,fm};
a list of functions (Python),
fitfunctions = [f1,...,fm]
a function of two parameters (C,Fortran,C++,Python),
For example:
double fitfunctions(int i, double x){return fi(x);}
#include<math.h> // NAN is here
double funs(int i, double x){
switch(i){
case 0: return 1.0/x; break;
case 1: return 1.0; break;
case 2: return x; break;
default: {fprintf(stderr,"funs: wrong i:%d",i); return NAN;}
}
}
(3 points) Uncertainties of the fitting coefficients
Modify you least-squares fitting function such that it also calculates the covariance matrix and the uncertainties of the fitting coefficients.
Make up some interesting fits and check that the reported uncertainties of the fitting coefficients are reasonable. For example,
(1 points) Ordinary least-squares solution by thin singular-value decomposition
(0 points) Extra
Make up an interesting exercise here.