Created
April 13, 2010 23:02
-
-
Save abuiles/365217 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Incremental | |
r = map (\x -> (f x * f (x+1) < 0,x,(x+1))) [0..10.0] | |
-- | |
b = filter (\(a,b,c) -> a == True ) r | |
f x = cos (x) - x | |
fp x = -sin (x) - 1 | |
--f x = exp ( -(2*x)) - (x * cos x) + 5 | |
--fp x = (-2) * exp (-(2*x)) + x * sin (x) - cos x | |
--f x = x^3 - 4*x^2 - 2 | |
--fp x = 3*x^2 - 8*x | |
--f x = exp ((-x)) - log (x) | |
--fp x = -exp((-x)) - (1/x) | |
--f x =(x^2) - 612 | |
--fp x = 2*x | |
g x = x - ((f x) / (fp x)) | |
startNewton xo = let | |
fxn = f xo | |
fpx = fp xo | |
in do | |
putStrLn ("xn =" ++show xo ++ " f(xn)="++show fxn ++ " f\'(xn)="++show fpx ++ " Error=Nan") | |
if (fxn == 0) | |
then | |
putStrLn ("EL resultado es =" ++ show xo) | |
else | |
if (fpx == 0) then | |
putStrLn ("La derivada de F(x) es cero no se puede calcular g (x)") | |
else | |
newton (g xo) xo 2 | |
newton xn xprev i = let | |
fxn = f xn | |
fpx = fp xn | |
error = abs ((xn - xprev)/xn) | |
in do | |
putStrLn ("xn =" ++show xn ++ " f(xn)="++show fxn ++ " f\'(xn)="++show fpx ++ " Error="++ show error) | |
if (fxn == 0) | |
then | |
putStrLn ("EL resultado es =" ++ show xn ++ "En la iteracion "++ show i) | |
else | |
if error < 5.0e-4 | |
then | |
putStrLn ("El resultado es="++show xn ++ "En la iteracion "++ show i) | |
else | |
if (fpx == 0) | |
then | |
putStrLn ("La derivada de F(x) es cero, luego no se puede calcular el proximo g (x)") | |
else | |
newton (g xn) xn (i+1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment