bbbsssmmm 发表于 2019-9-19 13:43:43

运行时一直运行不出来

求助
请问我下面的程序,build时并没有问题,但是运行时一直运行不出来(就是我按回车他没有反应)。请问我下面的程序是有什么问题吗:'(
program main
implicit none
external roots1
external roots2
integer::x,y
real::a,b,c,m,n

write(*,*) "please input two positive numbers"
read(*,*) x,y
a=real(x)
b=real(y)
call roots2(a,b,c)

write(*,*) "the first root:"
call roots1(a,c,m)

write(*,*) "the second root:"
call roots1(c,b,n)

end program

subroutine roots1(a,b,c)
    implicit none
    real,intent(in)::a,b
    real::p,q
    real,intent(out)::c

    p=a
    q=b
    do
      if(f1(p)*f1(q)<0)then
            c=(p+q)/2
            if(abs(f1(c))<0.0000001)then
                write(*,100) c
                100 format(F10.4)
                exit
            elseif(f1(p)*f1(c)<0)then
                q=c

            else
                p=c

            end if
      end if
    end do

    contains
    real function f1(x)
    implicit none
    real,intent(in)::x
    f1=x**3-5*x+3
    end function

end subroutine

subroutine roots2(a,b,c)

    real,intent(in)::a,b
    real::p,q
    real,intent(out)::c

    p=a
    q=b
    do
      if(f2(p)*f2(q)<0)then
         c=(p+q)/2
         if(abs(f2(c))<0.0000001)then
            c=(p+q)/2
            exit
         elseif(f2(p)*f2(c)<0)then
            q=c

         else
            p=c

          end if
      end if

    end do

    contains
   real function f2(x)
      implicit none
      real,intent(in)::x
      f2=3*x**2-5
   end function

end subroutine

bbbsssmmm 发表于 2019-9-19 13:44:27

是一个方程 求解的问题

li913 发表于 2019-9-19 19:37:50

本帖最后由 li913 于 2019-9-19 19:46 编辑

死循环,跳出循环的条件 abs(f2(c))<0.0000001 太苛刻。改为 abs(f2(c))<1.0e-5。用盛金公式直接求得所有根,快多了。

bbbsssmmm 发表于 2019-9-19 20:15:39

谢谢!
页: [1]
查看完整版本: 运行时一直运行不出来