summaryrefslogtreecommitdiff
path: root/lib/polyfit.f90
diff options
context:
space:
mode:
authorhibby <d@vehibberd.com>2018-02-16 15:18:44 +0000
committerhibby <d@vehibberd.com>2018-02-16 15:18:44 +0000
commit368f5331a7641772aa7e0cd1feea57d09a394a36 (patch)
treeb421ac60cc6eb726896ea268ea24e47fc4ce7c88 /lib/polyfit.f90
parent71c717edc92fa771c857b3c2e5ed65f523f68f90 (diff)
parent37f727ac0746fd85504aa46c1f023c09b3eff33a (diff)
Update upstream source from tag 'upstream/1.8.0+repack'
Update to upstream version '1.8.0+repack' with Debian dir a83ef78cb70a32d1211871fdc08f3ffa295e45a8
Diffstat (limited to 'lib/polyfit.f90')
-rw-r--r--lib/polyfit.f9072
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/polyfit.f90 b/lib/polyfit.f90
new file mode 100644
index 00000000..63222304
--- /dev/null
+++ b/lib/polyfit.f90
@@ -0,0 +1,72 @@
+subroutine polyfit(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(10), 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 polyfit