*chi2=; Calculate χ2 . *q=; if (mwt == 0) { for (i=1;i | 666 Chapter 15. Modeling ofData chi2 Calculate 2. q if mwt 0 for i 1 i ndata i chi2 SQR y i - a - b x i sigdat sqrt chi2 ndata-2 For unweighted data evaluate typ- siga sigdat ical sig using chi2 and ad- sigb sigdat just the standarddeviations. else for i 1 i ndata i chi2 SQR y i - a - b x i sig i if ndata 2 q gammq ndata-2 chi2 Equation . CITED REFERENCES AND FURTHER READING Bevington . 1969 Data Reduction and Error Analysis for the Physical Sciences New York McGraw-Hill Chapter 6. Straight-Line Data with Errors in Both Coordinates If experimental data are subject to measurement error not only in the yi s but also in the xi s then the task of fitting a straight-line model y x a bx is considerably harder. It is straightforward to write down the y2 merit function for this case 2 yi - a - bxi 2 X b -2 I b2a2 i i yi xi where ax i and ay i are respectively the x and y standard deviations for the ith point. The weighted sum of variances in the denominator of equation can be understood both as the variance in the direction of the smallest y2 between each data point and the line with slope b and also as the variance of the linear combination yi - a - bxi of two random variables xi and yi Var yi a bxi Var yi b2Var xi ay i b2a i i 1 wi The sum of the square of N random variables each normalized by its variance is thus X2-distributed. We want to minimize equation with respect to a and b. Unfortunately the occurrence of b in the denominator of equation makes the resulting equation for the slope dy2 db 0 nonlinear. However the corresponding condition for the intercept dy2 da 0 is still linear and yields a - i z bxi Wi Sample page from NUMERICAL RECIPES IN C THE ART OF SCIENTIFIC COMPUTING ISBN 0-521-43108-5 where the wi s are defined by equation . A reasonable strategy now is to use the machinery of Chapter 10 . the routine brent for minimizing a general one-dimensional function to .