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 .........抱歉,感謝您澄清.... –