CC BY 4.0 (除特别声明或转载文章外)
如果这篇博客帮助到你,可以请我喝一杯咖啡~
module Newton_Raphson where
squareroot2 :: Float -> Integer -> Float
squareroot2 x0 n=squareroot 2 x0 n
squareroot :: Float -> Float -> Integer -> Float
squareroot r x0 0=x0
squareroot r x0 n=(x+r/x)/2 where x=squareroot r x0 (n-1)
sqrtSeq :: Float -> Float -> [Float]
sqrtSeq r x0=x0:sqrtSeq r (squareroot r x0 1)
squareroot' :: Float -> Float -> Float -> Float
squareroot' r x0 eps=f (sqrtSeq r x0) eps where f (y:ys) eps=if abs((head ys)-y)<eps then y else f ys eps