Pseudocode for first-order least-squares calculation. Assumptions: the data are in the arrays x and y; length(x) returns the nunber of points; x(i) is the ith element of array x, where i is integer; x^2 is the square of x, etc. The for loops (for i=1:n....end) increment i in unit steps. n=length(x) sumx=0 sumy=0 sumxy=0 sumx2=0 for i=1:n sumx=sumx+x(i) sumy=sumy+y(i) sumxy=sumxy+x(i)*y(i) sumx2=sumx2+x(i)*x(i) end meanx=sumx/n meany=sumy/n slope=(n*sumxy-sumx*sumy)/(n*sumx2-sumx*sumx) intercept=meany-(slope*meanx) ssy=0 ssr=0; for i=1:n ssy=ssy+(y(i)-meany)^2 ssr=ssr+(y(i)-intercept-slope*x(i))^2 end R2 = 1-(ssr/ssy)