summaryrefslogtreecommitdiff
path: root/map65/libm65/fchisq0.f90
blob: 7cb9cb8a1f434d79e226a194ec3fc7f4a456472c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module fchisq0_mod
  implicit none
contains

real function fchisq0(y, npts, a) result(r)
  implicit none

  !--------------------------------------------------------------------
  ! Arguments
  !--------------------------------------------------------------------
  integer, intent(in) :: npts
  real,    intent(in) :: y(npts)
  real,    intent(in) :: a(4)

  !--------------------------------------------------------------------
  ! Locals
  !--------------------------------------------------------------------
  integer :: i
  real    :: x, z, yfit, d
  real    :: chisq

  !--------------------------------------------------------------------
  ! Compute chi-square
  !--------------------------------------------------------------------
  chisq = 0.0
  do i = 1, npts
     x = real(i)
     z = (x - a(3)) / (0.5 * a(4))
     yfit = a(1)

     if (abs(z) < 3.0) then
        d = 1.0 + z*z
        yfit = a(1) + a(2) * (1.0/d - 0.1)
     end if

     chisq = chisq + (y(i) - yfit)**2
  end do

  r = chisq

end function fchisq0

end module fchisq0_mod