%% pwchixy.m is my MATLAB version of the fitexy program from Numerical Recipes
%% (see p663 Section 15.3). It is designed to be called by pwfitexy.m.
%%

function chixy=pwchixy(bang)

% bang = slope as angle.
% chixy = required chi value

%Define required constants
big=1e30;

%Declare required GLOBAL VARIABLES
global xx yy sx sy ww aa offs nop

b=tan(bang);
ww=(b*sx).^2+sy.^2;
wwc=ww<(1/big);
ww(wwc)=big;
ww(~wwc)=1./ww;
sumw=sum(ww);
avex=sum(ww.*xx)/sumw;
avey=sum(ww.*yy)/sumw;
aa=avey-b*avex;
chixy=-offs+sum(ww.*(yy-aa-b.*xx).^2);


