0
program newton_raphson
implicit none
real,parameter::error=1e-4
integer::i
real::xo,x1,f,fd
print*,"Please enter the initial guess !!!"
read*,xo
i=1
10 x1=xo-(f(xo)/fd(xo))
if(abs((x1-xo)/x1)<error) then
print*,"root is", x1,"no. of iteration=",i
else
xo=x1
i=i+1
goto 10
endif
end
real function f(x)
real::x
f=cos(x)
end
real function fd(x)
real::x
fd=-sin(x)
end
该程序正常工作时,我给1作为初始猜测,但失败或不显示任何东西,当我给0作为初始猜测。我的代码有任何错误吗?牛顿拉夫森不与某些初始猜测收敛
它失败的方式很重要。但是,如果您对此有疑问,请显示错误消息以及您对行为的期望。更有可能你有一个数学问题而不是计算问题,那就是:当评估'f(0)/ fd(0)'时,你会发生什么? – francescalus
Arghh .........抱歉,感谢您澄清.... –