Fortran Coder

标题: 运行时一直运行不出来 [打印本页]

作者: bbbsssmmm    时间: 2019-9-19 13:43
标题: 运行时一直运行不出来
求助
请问我下面的程序,build时并没有问题,但是运行时一直运行不出来(就是我按回车他没有反应)。请问我下面的程序是有什么问题吗
[Fortran] 纯文本查看 复制代码
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
是一个方程 求解的问题
作者: li913    时间: 2019-9-19 19:37
本帖最后由 li913 于 2019-9-19 19:46 编辑

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

QQ截图20190919154500.png (221.98 KB, 下载次数: 208)

QQ截图20190919154500.png

作者: bbbsssmmm    时间: 2019-9-19 20:15
谢谢!





欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2