目的是想建立一个通用的牛顿求解算法的框架,就是说,对于不同的问题,只需要将初值A0,以及问题FX,导数DFX输入,调用牛顿算法,就可以求出结果。
我写的程序如下:有错误,请高手指点一下应该在怎样写?多谢多谢!!
[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
|