Implement a routine which makes a least-squares fit of a given data-set, {xi, yi, δyi | i=1...n}, with a linear combination of given functions, F(x) =∑ k=1..m ck fk(x).
The subroutine must calculate the vector of the coefficients, ck, and the covariance matrix. The fitting functions {fk(x)} can be programmed, e.g., as
a vector of functions (C++11,JavaScript,Python),
std::vector<std::function<double(double)>> fitfunctions =
{f1,...,fm};
a function of two parameters (C,Fortran,C++,Python),
double fitfunctions(int i, double x){return fi(x);}
For example:
#include<math.h> // NAN is here
double funs(int i, double x){
switch(i){
case 0: return 1.0; break;
case 1: return x; break;
case 2: return x*x; break;
default: {fprintf(stderr,"funs: wrong i:%d",i); return NAN;}
}
}
Hint: the variation of y with respect to variations of
a and b
as function of x is given as
δy = δa + δb x.
Therefore
δy2 = δa2 + δb2
x2 +2δa δb x.
For normally distributed a and b this translates into
Δy2(x) =
Δa2 +
Δb2x2 + 2<δa δb>x
where <δa δb> is the covariance.
Make up an interesting exercise here.