program main
implicit none
integer::i,n,k=-1
real8::a,b,c,x
integer,dimension( : ),allocatable:: s
print,‘输入方程的最高次数n:’
read*,n
allocate(s(n+1))
open(111,file=‘A.txt’)
open(222,file=‘B.txt’)
open(333,file=‘ans.txt’)
read(111,)(s(i),i=1,n+1) !由低阶系数到高阶系数输入
read(222,)b,a,c !上限b,下限a,精度c
call erfenfa(n,s,b,a,c,k,x)
write(333,)‘二分次数为:’,k
write(333,)‘近似解为:’,x
end program
subroutine erfenfa(n,s,b,a,c,k,x)
implicit none
integer::i,k,n
real8::a,b,c,x,f1,f2,fx
integer,dimension(n+1):: s
f1=0
f2=0
do i=1,n+1
f1=f1+s(i)a*(i-1)
f2=f2+s(i)b*(i-1)
enddo
do
x=(a+b)/2
k=k+1
fx=0
do i=1,n+1
fx=fx+s(i)x*(i-1)
enddo
if(fxf1>0)then
a=x
else
b=x
endif
if(abs(fx)
print*,‘二分次数为:’,k
print*,‘近似解为:’,x
end subroutine
!输入:n=3
!输入:s=-1 -1 0 1
!输入:b=1.5,a=1.0,c=0.005
!输出:k=6
!输出:x=1.32421875