Fortran Coder

查看: 11055|回复: 2
打印 上一主题 下一主题

[求助] 调试时变量不能赋值

[复制链接]

2

帖子

1

主题

0

精华

新人

F 币
20 元
贡献
7 点
跳转到指定楼层
楼主
发表于 2016-9-13 17:17:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
编译和调试时,a不能被赋值,求大神指导
[Fortran] 纯文本查看 复制代码
program solver
    implicit none
    real,external::getroot,polyeq
    real::tol=0.0001
    real::coeff(4)=(/-1.,1.,0.,-1./)!多项式系数向量
    real::root
    root=getroot(polyeq,coeff,4,0.,1.)!求根函数
    write(*,*)root
    end program
    real function getroot(polyeq,coeff,n,a,b)
    implicit none
    real,external::polyeq
    integer,intent(in)::n
    real,intent(in)::coeff(n)
    real::a,b
    integer::i
    real::root
    do
        if(abs((b-a)/2) .lt. 0.0001)exit
        root=(b+a)/2
        if(abs(polyeq(coeff,n,root)) .lt. 0.0001)exit
        if(polyeq(coeff,n,a)*polyeq(coeff,n,root) .lt. 0.0)then
            b=root
        else
            a=root
        end if
    end do
    getroot=root
!    return getroot
    end function getroot
    real function polyeq(coeff,n,x)!多项式
    implicit none
    integer,intent(in)::n
    integer::i
    real,intent(in)::coeff(n)
    real,intent(in)::x
    real::vec(n)
    real::res=0.0
    vec=[((x**(i-1)),i=1,n)]
    res=res+dot_product(coeff,vec)
    polyeq=res
!    return polyeq
    end function polyeq
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

1967

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1370 元
贡献
581 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

沙发
发表于 2016-9-13 19:31:42 | 只看该作者
因为你的 a 是虚参,而对应的实参是 0.0 是一个常数。而常数不能被更改。所以 a 不能被赋值。
你可以在主程序定义2个变量 a 和 b。然后传入子程序即可
[Fortran] 纯文本查看 复制代码
program solver
  implicit none
  real,external::getroot,polyeq
  real::tol=0.0001
  real::coeff(4)=(/-1.,1.,0.,-1./)!多项式系数向量
  real::root,a=0.0,b=1.0
  root=getroot(polyeq,coeff,4,a,b)!求根函数
  write(*,*)root
end program

2

帖子

1

主题

0

精华

新人

F 币
20 元
贡献
7 点
板凳
 楼主| 发表于 2016-9-13 19:39:15 | 只看该作者
多谢裙子支持
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-17 14:06

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表