summaryrefslogtreecommitdiff
path: root/lib/polfit.f90
diff options
context:
space:
mode:
authorIain R. Learmonth <irl@debian.org>2015-10-18 18:51:00 +0100
committerIain R. Learmonth <irl@debian.org>2015-10-18 18:51:00 +0100
commitd63dd8d081a6fcd0715cd13f26df59fb831769d2 (patch)
treeb9c1f6245147a310aac3762630bad389bbb57335 /lib/polfit.f90
parentc720a3d55c5ec832c29cb7ce50b6fb4b70c55b11 (diff)
Imported Upstream version 1.5.0upstream/1.5.0
Diffstat (limited to 'lib/polfit.f90')
-rw-r--r--lib/polfit.f9072
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/polfit.f90 b/lib/polfit.f90
new file mode 100644
index 00000000..ea7ffc9f
--- /dev/null
+++ b/lib/polfit.f90
@@ -0,0 +1,72 @@
+subroutine polfit(x,y,sigmay,npts,nterms,mode,a,chisqr)
+ implicit real*8 (a-h,o-z)
+ real*8 x(npts), y(npts), sigmay(npts), a(nterms)
+ real*8 sumx(19), sumy(10), array(10,10)
+
+! Accumulate weighted sums
+ nmax = 2*nterms-1
+ sumx=0.
+ sumy=0.
+ chisq=0.
+ do i=1,npts
+ xi=x(i)
+ yi=y(i)
+ if(mode.lt.0) then
+ weight=1./abs(yi)
+ else if(mode.eq.0) then
+ weight=1
+ else
+ weight=1./sigmay(i)**2
+ end if
+ xterm=weight
+ do n=1,nmax
+ sumx(n)=sumx(n)+xterm
+ xterm=xterm*xi
+ enddo
+ yterm=weight*yi
+ do n=1,nterms
+ sumy(n)=sumy(n)+yterm
+ yterm=yterm*xi
+ enddo
+ chisq=chisq+weight*yi**2
+ enddo
+
+! Construct matrices and calculate coefficients
+ do j=1,nterms
+ do k=1,nterms
+ n=j+k-1
+ array(j,k)=sumx(n)
+ enddo
+ enddo
+
+ delta=determ(array,nterms)
+ if(delta.eq.0) then
+ chisqr=0.
+ a=0.
+ else
+ do l=1,nterms
+ do j=1,nterms
+ do k=1,nterms
+ n=j+k-1
+ array(j,k)=sumx(n)
+ enddo
+ array(j,l)=sumy(j)
+ enddo
+ a(l)=determ(array,nterms)/delta
+ enddo
+
+! Calculate chi square
+
+ do j=1,nterms
+ chisq=chisq-2*a(j)*sumy(j)
+ do k=1,nterms
+ n=j+k-1
+ chisq=chisq+a(j)*a(k)*sumx(n)
+ enddo
+ enddo
+ free=npts-nterms
+ chisqr=chisq/free
+ end if
+
+ return
+end subroutine polfit