fortran编程出现错误,怎么修改啊

2024-12-18 20:55:06
推荐回答(2个)
回答1:

0.618法求极值的程序吧,我给你改了改,可以用了,最优值 q=-1.499.756,修改后的代码如下

program main
implicit none
real,parameter:: k= 0.00001
real:: a=-5.0 , b=0.0
real:: x,y,fx,fy
real, external :: func

do while (b-a>k)
x=a+0.382*(b-a)
y=a+0.618*(b-a)
fx=func(x)
fy=func(y)
if (fx>=fy) then
a=x
x=y
y=a+0.618*(b-a)
else
b=y
y=x
x=a+0.382*(b-a)
end if
end do

write (*,*) "q=",(b+a)/2
stop
end program

real function func(x)
real x
func=x**2+3*x+4
end function

回答2:

Error: Syntax error, found '(' when expecting one of:
real function (x,y)
--------------^
Error executing df.exe.
huangjinfengefa.exe - 1 error(s), 0 warning(s)

语法错误。因这一语句中漏写了子函数的名称func。应为:
real function func(x,y)