#include <stdio.h> #include <math.h> #define NPT 100 #define SPREAD 0.1 #define NTERM 4 void funcs(double x, double func[]) { func[0]=cos(x); func[1]=sin(x); func[2]=exp(x); func[3]=1.0; } int main(void) { int i,j; array int ia[NTERM]; array double a[NTERM],x[NPT],y[NPT],sig[NPT],covar[NTERM][NTERM],u[NPT]; double chisq; linspace(x, 0, 10); urand(u); y = 4*cos(x) + 3*sin(x) +2*exp(x) +(array double [NPT])1.0 + SPREAD*u; sig=(array double[NPT])(SPREAD); ia[0] = 0; ia[1] = 1; ia[2] = 0; ia[3] = 1; a[0] = 4; a[1] = 3; a[2] = 2; a[3] = 1; curvefit(a,x,y,funcs,sig,ia,covar,&chisq); printf("\n%11s %21s\n","parameter","uncertainty"); for (i=0;i<NTERM;i++) printf(" a[%1d] = %8.6f %12.6f\n",i,a[i],sqrt(covar[i][i])); printf("chi-square = %12f\n",chisq); printf("full covariance matrix\n"); printf("%12f",covar); }
```
parameter           uncertainty
a[0] = 4.000000     0.000000
a[1] = 2.994009     0.015044
a[2] = 2.000000     0.000000
a[3] = 1.052622     0.010357
chi-square =     7.537645
full covariance matrix
0.000000     0.000000     0.000000     0.000000
0.000000     0.000226     0.000000    -0.000041
0.000000     0.000000     0.000000     0.000000
0.000000    -0.000041     0.000000     0.000107
```