Skip to content

Instantly share code, notes, and snippets.

@abuiles
Created April 13, 2010 23:00
Show Gist options
  • Save abuiles/365216 to your computer and use it in GitHub Desktop.
Save abuiles/365216 to your computer and use it in GitHub Desktop.
--f x = exp((-x^2)+2) - x*sin(2*x-3) + 2
-- Incremental
r = map (\x -> (f x * f (x+1e-1) < 0,x,(x+1e-1))) [-5,-5+1e-1..5.0]
b = filter (\(a,b,c) -> a == True ) r
t i n = let m = (n+i)/ 2
in f m
-- Algoritmo De Biseccion
--Iteraciones n en intervalo a b con una toleracia t (E < TOL > => (b-a)2^n < 10^t
interParaTolerancia a b t = (log (b-a) + t*log(10))/log 2
startBisec xi xu i | f xi == 0 = putStrLn ("La raiz esta en "++show xi)
| f xu == 0 = putStrLn ("La raiz esta en "++show xu)
| f xi * f xu > 0 = putStrLn " Error: No hay raiz es ese intervalo"
| otherwise = let m = (xi + xu ) /2
fxm = f m
in
if fxm == 0
then
putStrLn ("El resultado es="++ show m ++ " " ++ "Iteracion "++ show i)
else do
putStrLn ("xi= " ++ show xi ++ " xu = "++show xu ++" pm=" ++ show m ++" Fxm= "++ show fxm++" Error = NaN " )
if fxm * (f xi) > 0
then
bisec m xu fxm i 1
else
bisec xi m fxm i 1
--E <-> DC -> 0.5*e-dc ......... Er <-> cs -> 5.0e-cs
bisec xi xu xprev i ni
| i == ni = putStrLn "El valor no fue encontrado en las iteraciones dadas!!!"
| otherwise = let m = (xi+xu)/2
fxm = f m
error = abs ((fxm - xprev))
in
if fxm == 0
then
putStrLn ("El resultado es="++ show m ++ " " ++ "Iteracion "++ show ni)
else
do
putStrLn ("xi= " ++ show xi ++ " xu = "++show xu ++" pm=" ++ show m ++" Fxm= "++ show fxm++" Error="++ show error )
if error < 0.0001 --Tolerancia
then
putStrLn ("El resultado es="++ show m ++ " " ++ "Iteracion "++ show ni)
else
if fxm*(f xi) > 0
then
bisec m xu fxm i (ni+1)
else
bisec xi m fxm i (ni+1)
-----------------
--PUNTO FIJO
--g x = exp ((-x^2)-5) - x^2 + 3* x
--g x =(5 - log ((x^2) - 2*x)) / x
--g x = -sqrt (-5-log((x^2)-2*x))
--g x = x^2-(exp((-x^2)-5))
--g x = exp (-
--f x = exp ((-x)) - x
--g x =
g x = x - ( (exp(((-x^2)-5)) - x^2 + 2*x)/((-2)*exp(((-x^2)-5)) - x^2 + 2))
--f x = undefined
--g x = undefined
-- Evaluar con almenos 3 puntos
f x = ((-0.5*(x^2)) + (2.5 * x )+ 4.5 )
--g x = x - ((f x)/(fp x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment