0hc.net Menu
Home Overview
About Menu
Impressum

Newton's method

Required is a soluton for the transcendental equation: x*ln(x)=1 => x=1.76322283435189671022520177695 A simple Newton's method implementation: main :: IO () main = print $ newton 1 5 f x = x * log x - 1 f' x = log x + 1 newton :: Double -> Int -> Double newton x i | i > 1 = newton y (i-1) | otherwise = y where y = x - f x / f' x To specify the value exactly, Maxima is a good possibility: fpprec : 30; newton (f,x) := ([y,df,dfx], df: diff(f('x),'x), do (y:ev(df),x:x-f(x)/y,if abs(f(x))<5e-20 then return (x)))$ f(x) := x*log(x)-1; newton(f,1); bfloat(newton(f,1)); Now we need a Haskell program which offers the same comfort: main :: IO () main = print $ newton (\x -> x * log x - 1) 1 diff :: (Double -> Double) -> Double -> (Double -> Double) diff f dx = \x -> ((f(x+dx)) - (f x)) / dx newton :: (Double -> Double) -> Double -> Double newton f y | abs z < 1.0e-15 = y | otherwise = newton f (newton' y f) where newton' y f = y - z / (diff f 1.0e-10 y) z = f y If you want MORE DIGITS, an effective way is the CReal library: import CReal main :: IO () main = putStrLn $ showCReal 100 $ newton 1.76322283435189671022520177695 2 newton :: CReal -> Int -> CReal newton x i | i > 1 = newton y (i-1) | otherwise = y where y = x - f x / f' x f x = x * l - 1 f' x = l + 1 l = log x Produces a result with one hundred digits: 1.763222834351896710225201776951707080436017986667473634570456905547275847186995 7367890838910506811056
0hc.net    © 2001-2014 Harald Wolfsgruber