c2--567--1---------2---------3---------4---------5---------6---------712 Subroutine linear_fit(x, y, a, b, n) c c -- Linear_fit/Linear fit of a set of points/N. Nath/May '01 c c -- Function: Given x(i), y(i) for i = 1 through n, this routine c finds the best fit for y(i) to a*x(i) + b. c implicit none c c -- Input & Output Variables: c real x(n), y(n) !abscissa & ordinate (input) real a, b !slope & intercept (output) integer n !number of data points (input) c c -- Local Variables: c real c11, c12, c22, d1, d2, det integer i c c11 = 0.0 c12 = 0.0 do i = 1,n c11 = c11 + x(i)**2 c12 = c12 + x(i) end do c22 = n d1 = 0.0 d2 = 0.0 do i = 1,n d1 = d1 + x(i)*y(i) d2 = d2 + y(i) end do det = c11*c22 - c12**2 a = (c22*d1 - c12*d2)/det b = (-c12*d1 + c11*d2)/det c return end