Arrays 函数数组和分段错误-内存引用无效

Arrays 函数数组和分段错误-内存引用无效,arrays,function,fortran,dimension,geany,Arrays,Function,Fortran,Dimension,Geany,我试图将函数f设置为数组,但出现以下错误: Program received signal SIGSEGV: Segmentation fault - invalid memory reference. Backtrace for this error: #0 0x6f8b36e3 #1 0x6f8a2722 #2 0x402752 #3 0x747bd411 我必须解开普勒方程:f=psi-e*sin(psi)-M对于M的每个值。因此,如果我有一个维度为8的数组M,我的程序将计算

我试图将函数
f
设置为数组,但出现以下错误:

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:

#0  0x6f8b36e3
#1  0x6f8a2722
#2  0x402752
#3  0x747bd411
我必须解开普勒方程:
f=psi-e*sin(psi)-M
对于
M
的每个值。因此,如果我有一个维度为8的数组
M
,我的程序将计算8个零。问题是,如果我写
f=psi-e*sin(psi)-M(1)
我将计算第一个零,如果我写
f=psi-e*sin(psi)-M(8)
我将计算最后一个零。但是,我的问题是,如果我想一次计算所有零,我必须写
f=psi-e*sin(psi)-M(1:8)
,我的程序应该键入所有零,但是,这没有发生,我得到了我前面提到的错误。下面是代码:

子程序(我在外部使用):此子程序是二分法(用于获取零):

主程序:

       include 'bisecc.f' 
   implicit real*8 (a-h,o-z)            
   external f     
   real*8 f    
   ! I  WRITE  THE INTERVAL OF MY 8 ZEROS(left and right point)
   b=0.1D0


   xl1=-0.5D0                
   xr1=0.D0                 
   xl2=xr1+b
   xr2=1.D0
   xl3=xr2+b
   xr3=2.D0
   xl4=xr3+b
   xr4=3.D0
   xl5=xr4+b
   xr5=4.D0
   xl6=xr5+b
   xr6=5.D0
   xl7=xr6+b
   xr7=6.D0
   xl8=xr7+b
   xr8=7.D0        
   kmax=100                            
   tol=0.0001D0                               
   call bisecc(f,xl1,xr1,kmax,tol,k,xm1)         
   call bisecc(f,xl2,xr2,kmax,tol,k,xm2)          
   call bisecc(f,xl3,xr3,kmax,tol,k,xm3)         
   call bisecc(f,xl4,xr4,kmax,tol,k,xm4)         
   call bisecc(f,xl5,xr5,kmax,tol,k,xm5)         
   call bisecc(f,xl6,xr6,kmax,tol,k,xm6)         
   call bisecc(f,xl7,xr7,kmax,tol,k,xm7)          
   call bisecc(f,xl8,xr8,kmax,tol,k,xm8)                     
   write(*,*) 'Program ended'
   stop
   end program

例如: 这里我想计算第一个值M的psi值,
M(1)=pi/4

在中可以看到
psi=0.95303344726562489
。我刚刚计算了第一个零。但是,您也可以7次看到此消息。这意味着程序只能向我显示零(对于
M(1)
),其他7个零不计算,因为我写了
f=psi-e*sin(psi)-M(1)

我应该写些什么才能得到全零而不是1的结果,就像在这个例子中一样?

因为函数
f()
在对分例程
bisecc()
中使用,我认为通过DO循环将每个输入传递给
bisecc()
要比生成
f()简单得多
返回数组的函数(因为后者也需要修改
bisecc()
)。我们可以通过各种方式将
M
的值传递给
f()
(这几乎是常见问题,我相信有很多Q/a页面)。一种简单的方法是在主程序中包含
f()
,并对
M
使用主机关联。因此,简化的代码可能看起来像

program main
    implicit none
    integer  kmax, kiter, i
    real*8   xl( 8 ), xr( 8 ), xans( 8 ), tol, M( 8 ), b, pi

    pi   = acos(-1.0D0)                 
    kmax = 100
    tol  = 1.0d-8

    M = [ pi/4.D0,      pi/2.D0,      3.D0/4.D0*pi, pi, &
          5.D0/4.D0*pi, 3.D0*pi/2.D0, 7.D0/4.D0*pi, 2.D0*pi ]
    ! or M = [( i, i=1,8 )] * pi/4.0D0

    ! Use a fixed interval for simplicity.
    xl   =  0.0d0
    xr   = 10.0d0
    xans =  0.0d0

    do i = 1, 8
        call bisecc( f, xl( i ), xr( i ), kmax, tol, kiter, xans( i ) )
        ! print *, "check: f(xans(i)) = ", f( xans( i ) )
    enddo

contains

function f( psi ) result( res )
    implicit none
    real*8  psi, e, res
    e = 0.2056D0
    res = psi - e * sin( psi ) - M( i )   !<-- this "M(i)" refers to that defined above
end function 

end program

 bisection converged: k=          31 xm=  0.95299366395920515     
 bisection converged: k=          31 xm=   1.7722388869151473     
 bisection converged: k=          30 xm=   2.4821592587977648     
 bisection converged: k=          30 xm=   3.1415926571935415     
 bisection converged: k=          29 xm=   3.8010260276496410     
 bisection converged: k=          29 xm=   4.5109464414417744     
 bisection converged: k=          29 xm=   5.3301916457712650     
 bisection converged: k=          29 xm=   6.2831853143870831
答案似乎与e=0.2056的曲线图一致(因此
bisecc()
可能是可以的)


上面的代码仍然有很多需要改进的地方。特别是,通常更方便的方法是将类似
f()
的函数包含到模块中(甚至将所有例程包含到模块中)。我们也可以通过将其设置为模块变量来传递
M
,并从
f()
(而不是使用
common
语句)或通过主机关联来使用
M
,因此如果有兴趣,请尝试一下。

我的解决方案:我将在练习中添加一个更通用的解决方案,以避免上述错误。这是一个更通用的解决方案,对于M的N值,而不是8:

   include 'bisecc.f'  
   implicit real*8 (a-h,o-z)             
   external f     
   parameter (Mlong=100)              !Number of elemnts of M(from 0 to 2pi)
   real*8 f ,M                               
   common M,e                         !to not copy them twice 

   kmax=100                           !max number of iterations
   tol=0.0001D0                       !Tolerance of 0.01%
   e=0.2056D0                         !Mercury excentricity
   pi=acos(-1.0D0)                     
   c=sqrt((1.0D0-e)/(1.0D0+e))     

   open(10,file='153b.dat',status='unknown') !data will apear in a .dat file
   write(*,*)'            i        M                 Theta(rad)'
   write(10,*)'           i        M                 Theta(rad)'

  do i=1,Mlong
     xl=-1.D0                     !LEFT STARTING POINT
     xr=7.D0                      !RIGHT POINT(psi wont be more than 2*pi)  
     M=2.D0*pi*i/Mlong                         !GENERIC M(0 TO 2PI 100STEPS) 
     call bisecc(f,xl,xr,kmax,tol,k,xm)        !CALLING THE SUBROUTINE
     write(10,*) i,M,theta             ! I WILL PLOT THETA IN FUNCTION OF M
     write(*,*) i,M,theta
   end do 

   close(10)
   write(*,*)
   write(*,*) 'Program ENDED'
   stop
   end program
*我的外部功能



您必须显示进行编写的代码。请使用
-g-fbacktrace-fcheck=all-Wall
或等效工具编译代码,然后重试并报告输出。我没有收到编译错误或生成错误,错误是我在执行程序时收到的,它显示在cmd中。如果要检查代码,你可以给我你的电子邮件地址,我会把这两个文件都发送给你。f所以你可以在自己的电脑上运行它,前提是你想提交一个新问题,如果你知道答案,它可能是解决这个问题的一个很好的替代方法:“可以将f()更改为返回数组的函数,但如果它用于对分,保持它只是一个标量函数,并为输入数组的每个元素调用对分例程不是更好吗?(上面的“答案”实际上是一个解决办法,因为它不回答OP代码中出现分段错误的原因,也不回答如何从函数返回数组。)感谢您的帮助,我知道你们对代码有点严格,因为这可能对任何人都有用,但我编写代码的方式,是我们老师教我们的方式,所以现在我必须像他一样工作,使用他向我们展示的东西,将来我将学习正确的编码方式,我的物理老师说他不是一个程序员,他是一个物理学家,只是用电脑来解决问题,所以我们对这个问题不是很严格code@stepdead0X没问题,“common”有点老了,但仍然是传递变量的有效方法。当变量的数量变大时,考虑在那个时候将公共模块切换到模块可能是有用的(以避免公共模块的各种陷阱)。这可能是一个很好的开始,我们一个月前开始学习计算物理,仍然需要学习更多的东西,比如模块等等。
subroutine bisecc( f, xl, xr, kmax, tol, k, xm )
    implicit none
    real*8  f, xl, xr, tol, xm
    external f
    integer kmax, k
    real*8  fl, fr, fm, dif

    fl = f( xl )
    fr = f( xr )
    if( fl * fr > 0.0D0 ) then
        write(*,*) "bad input data (xl,xr)"
        return
    endif

    do k = 1, kmax
        xm = (xr + xl) / 2.0D0              
        fm = f( xm )

        dif = abs( (xr-xl) / xm )
        if ( dif < tol ) then
            write(*,*) "bisection converged: k=", k, "xm=", xm
            return
        endif

        if ( fm * fr <= 0.0D0 ) then
            xl = xm
            fl = fm
        else
            xr = xm
            fr = fm
        end if
    end do  !! iteration

    write(*,*) "bisection did not converge: k=", k, "xm=", xm
end
 bisection converged: k=          31 xm=  0.95299366395920515     
 bisection converged: k=          31 xm=   1.7722388869151473     
 bisection converged: k=          30 xm=   2.4821592587977648     
 bisection converged: k=          30 xm=   3.1415926571935415     
 bisection converged: k=          29 xm=   3.8010260276496410     
 bisection converged: k=          29 xm=   4.5109464414417744     
 bisection converged: k=          29 xm=   5.3301916457712650     
 bisection converged: k=          29 xm=   6.2831853143870831
   include 'bisecc.f'  
   implicit real*8 (a-h,o-z)             
   external f     
   parameter (Mlong=100)              !Number of elemnts of M(from 0 to 2pi)
   real*8 f ,M                               
   common M,e                         !to not copy them twice 

   kmax=100                           !max number of iterations
   tol=0.0001D0                       !Tolerance of 0.01%
   e=0.2056D0                         !Mercury excentricity
   pi=acos(-1.0D0)                     
   c=sqrt((1.0D0-e)/(1.0D0+e))     

   open(10,file='153b.dat',status='unknown') !data will apear in a .dat file
   write(*,*)'            i        M                 Theta(rad)'
   write(10,*)'           i        M                 Theta(rad)'

  do i=1,Mlong
     xl=-1.D0                     !LEFT STARTING POINT
     xr=7.D0                      !RIGHT POINT(psi wont be more than 2*pi)  
     M=2.D0*pi*i/Mlong                         !GENERIC M(0 TO 2PI 100STEPS) 
     call bisecc(f,xl,xr,kmax,tol,k,xm)        !CALLING THE SUBROUTINE
     write(10,*) i,M,theta             ! I WILL PLOT THETA IN FUNCTION OF M
     write(*,*) i,M,theta
   end do 

   close(10)
   write(*,*)
   write(*,*) 'Program ENDED'
   stop
   end program
   real*8 function f(psi)
   implicit real*8 (a-h,o-z)
   real*8 M
   common M,e
    f=psi-e*sin(psi)-M                       !KEPLER EQUATION
   return       
   end function