最小二乘最小化fortran 77
试图用fortran77实现单参数最小二乘最小化。这是代码;它可以编译并且似乎可以工作,除了……它被困在h1=1.8E-2和3.5E-2之间的无限循环中。 现在来看一下,但很有可能,我不会有太多的运气使用我自己的问题。欢迎大家帮忙最小二乘最小化fortran 77,fortran,fortran77,least-squares,minimization,Fortran,Fortran77,Least Squares,Minimization,试图用fortran77实现单参数最小二乘最小化。这是代码;它可以编译并且似乎可以工作,除了……它被困在h1=1.8E-2和3.5E-2之间的无限循环中。 现在来看一下,但很有可能,我不会有太多的运气使用我自己的问题。欢迎大家帮忙 PROGRAM assignment ! A program designed to fit experiemental data, using the method ! of least squares to minimise
PROGRAM assignment
! A program designed to fit experiemental data, using the method
! of least squares to minimise the associated chi-squared and
! obtain the four control parameters A,B,h1 and h2.
!*****************************************************************
IMPLICIT NONE
INTEGER i
DOUBLE PRECISION t(17),Ct(17),eCt(17)
DOUBLE PRECISION h1loop1,h1loop2,deltah,Cs
DOUBLE PRECISION chisqa,chisqb,dchisq
OPEN(21, FILE='data.txt', FORM='FORMATTED', STATUS='OLD')
DO i=1,17
READ(21,*)t(i),Ct(i),eCt(i)
END DO
CLOSE(21)
!Read in data.txt as three one dimensional arrays.
!*****************************************************************
!OPEN(21, FILE='outtest.txt', FORM='FORMATTED', STATUS='NEW')
!DO i=1,17
! WRITE(21,*)t(i),Ct(i),eCt(i)
!END DO
!CLOSE(21)
!
!Just to check input file is being read correctly.
!*****************************************************************
!**********************Minimising Lamda1 (h1)*********************
deltah= 0.0001
h1loop2= 0.001
h1loop1= 0.0 !Use initial value of 0 to calculate start-point chisq
DO 10
chisqa= 0.0
DO 20 i= 1, 17
Cs= exp(-h1loop1*t(i))
chisqa= chisqa + ((Ct(i) - Cs)/eCt(i))**2
20 END DO
chisqb= 0.0
DO 30 i= 1, 17
h1loop2= h1loop2 + deltah
Cs= exp(-h1loop2*t(i))
chisqb= chisqb + ((Ct(i) - Cs)/eCt(i))**2
30 END DO
!Print the two calculated chisq values to screen.
WRITE(6,*) 'Chi-squared a=',chisqa,'for Lamda1=',h1loop1
WRITE(6,*) 'Chi-squared b=',chisqb,'for Lamda1=',h1loop2
dchisq= chisqa - chisqb
IF (dchisq.GT.0.0) THEN
h1loop1= h1loop2
ELSE
deltah= deltah - ((deltah*2)/100)
END IF
IF (chisqb.LE.6618.681) EXIT
10 END DO
WRITE(6,*) 'Chi-squared is', chisqb,' for Lamda1 = ', h1loop2
END PROGRAM assignment
编辑:再看一遍,我觉得我不知道是什么搞砸了。应该得到6618.681的卡方,但它只是停留在6921.866和6920.031之间。救命啊
do i=1
不是开始循环,对于循环,还需要指定上限:
do i=1,ub
这就是为什么您会收到关于doi没有类型的错误消息,在固定格式中,空格是无关紧要的
编辑:如果希望有一个无限循环,只需完全跳过“i=”声明。当达到某个条件时,可以使用exit语句离开循环:
do
if (min_reached) EXIT
end do
我不知道你为什么坚持F77固定格式。这是您的免费格式程序,其中包含一些位置修复,看起来很奇怪,但没有深入了解太多细节:
PROGRAM assignment
! A program designed to fit experiemental data, using the method
! of least squares to minimise the associated chi-squared and
! obtain the four control parameters A,B,h1 and h2.
!*****************************************************************
IMPLICIT NONE
integer, parameter :: rk = selected_real_kind(15)
integer, parameter :: nd = 17
integer :: i,t0
real(kind=rk) :: t(nd),t2(nd),Ct(nd),eCt(nd),Ctdiff(nd),c(nd)
real(kind=rk) :: Aa,Ab,Ba,Bb,h1a,h1b,h2a,h2b,chisqa,chisqb,dchisq
real(kind=rk) :: deltah,Cs(nd)
OPEN(21, FILE='data.txt', FORM='FORMATTED', STATUS='OLD')
DO i=1,nd
READ(21,*) t(i),Ct(i),eCt(i)
END DO
CLOSE(21)
!Read in data.txt as three one dimensional arrays.
!*****************************************************************
!OPEN(21, FILE='outtest.txt', FORM='FORMATTED', STATUS='NEW')
!DO i=1,17
! WRITE(21,*)t(i),Ct(i),eCt(i)
!END DO
!CLOSE(21)
!
!Just to check input file is being read correctly.
!*****************************************************************
!****************************Parameters***************************
Aa= 0
Ba= 0
h1a= 0
h2a= 0
!**********************Minimising Lamda1 (h1)*********************
deltah= 0.001_rk
h1b= deltah
minloop: DO
chisqa= 0
DO i= 1,nd
Cs(i)= exp(-h1a*t(i))!*Aa !+ Ba*exp(-h2a*t(i))
Ctdiff(i)= Ct(i) - Cs(i)
c(i)= Ctdiff(i)**2/eCt(i)**2
chisqa= chisqa + c(i)
h1a= h1a + deltah
END DO
! Use initial h1 value of 0 to calculate start-point chisq.
chisqb= 0
DO i= 1,nd
h1b= h1b + deltah
Cs(i)= exp(-h1b*t(i))!*Ab !+ Bb*exp(-h2b*t(i))
Ctdiff(i)= Ct(i) - Cs(i)
c(i)= Ctdiff(i)**2/eCt(i)**2
chisqb= chisqb + c(i)
END DO
! First-step h1 used to find competing chisq for comparison.
WRITE(6,*) 'Chi-squared a=', chisqa,'for Lamda1=',h1a
WRITE(6,*) 'Chi-squared b=', chisqb,'for Lamda1=',h1b
! Prints the two calculated chisq values to screen.
dchisq= chisqa - chisqb
IF (dchisq.GT.0) THEN
h1a= h1b
ELSE IF (dchisq.LE.0) THEN
deltah= (-deltah*2)/10
END IF
IF (chisqb.LE.6000) EXIT minloop
END DO minloop
WRITE(6,*) 'Chi-squared is', chisqb,'for Lamda1=',h1b
END PROGRAM assignment
我明白了。知道如何让它迭代到最小值吗?假设不知道需要多少次迭代,那太好了!谢谢你。就在我也需要它的时候,我已经准备好测试第一个循环了。祝我好运。再次感谢:)无限循环!我会编辑主要问题,然后好好看看,看看我是否能找出原因。我不太理解你在编辑中的问题,你的代码丢失了,如果dchisq.gt.0的代码在循环结束时结束,那么你指的是h1,你指的是哪个邻居?很抱歉,我错过了这个。你现在可能已经算出了,但为了清楚起见,h1是通过最小化找到的变量,对于方程Cs=Aexp(-h1*t)+Bexp(h2*t)。我在这里把它分为h1a和h1b,这样我可以在两个DO循环中使用它。至于邻域,我只是指最小化中h1的邻域值。比如-如果h1最小化到h1b的卡方比h1a大,但卡方仍然很大,那么程序需要返回到h1b的不同值,这样它就不会卡住。回过头来看,似乎不太清楚,但不知道该怎么说。希望新的代码上传比我更容易理解!在20循环中,无条件地将deltah设置为0.001。在第二个循环之后,它本质上会使你的条件设置为负值,这有点无用(它只在第一个i中处于活动状态)。此外,在循环中将h1b始终设置为0.001看起来也很奇怪。是否有任何原因,为什么你想在F77固定格式的代码?