[Fortran] 纯文本查看 复制代码
subroutine module_and_algorithm
implicit none
double precision,external :: cubic_roots,der_cubic_roots
integer,parameter :: length=4
double precision Co(length)
double precision :: x0 =1d0
double precision y
co=(/3d0,12d0,0d0,1d0/)
call newton(x0,cubic_roots,der_cubic_roots,y)
print *, "this is the roots near X0",y
end subroutine
subroutine cubic_roots(Co,length,y)
implicit none
integer length,i
double precision x
double precision y
double precision temp(length)
do i=1,length
temp(i)=co(i)*x**(i-1)
end do
y=sum(temp)
return
end subroutine
subroutine der_cubic_roots(Co,length,y)
implicit none
integer length,i
double precision x
double precision y
double precision temp(length-1)
do i=2,length
temp(i)=dble(i-1)*co(i)*x**(length-2)
end do
y=sum(temp)
return
end subroutine
subroutine newton(a,f,df,b)
implicit none
double precision :: epsilon=1d-6
double precision a,b,fb
double precision,external :: f,df
b=a-f(a)/df(a)
fb=f(b)
print *, "i am here (1)",fb
do while(abs(fb)>epsilon)
a=b
b=a-f(a)/df(a)
fb=f(b)
print *, "i am here (2)", fb
end do
return
end subroutine
program main
call module_and_algorithm
end