Created
April 13, 2009 23:02
-
-
Save dirceu-jr/94817 to your computer and use it in GitHub Desktop.
This file contains 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
# Ruby + Math | |
# Math.exp(1) = Euler's number | |
# http://en.wikipedia.org/wiki/E_(mathematical_constant) | |
# Math.sin(x) = sen(x) | |
# Math.log(x) = Logaritmo natural. Se escrito Math.log(x,b) b representara a base do logaritmo | |
# Busca de raizes | |
# Aproximação desejada | |
ERRO = 10**-3 | |
# Intervalo padrão | |
@intervalo = -10000000...10000000 | |
# Exemplos do livro | |
@exemplo1 = lambda { |x| x*Math.log(x)-3.2 } | |
@ex11 = lambda { |x| (x**2)-3 } | |
@ex12 = lambda { |x| x**2+Math.log(x) } | |
@ex13 = lambda { |x| (Math.exp(1)**-x) - Math.sin(x) } | |
@exemplo2 = lambda { |x| (x**4) - 26*(x**2) + (24*x) + 21 } | |
@ex21 = lambda { |x| Math.cos(x) + x } | |
@ex22 = lambda { |x| (Math.exp(1)**x) + x } | |
def metodo_da_bisseccao(intervalo, funcao) | |
a = intervalo.begin.to_f | |
b = intervalo.end.to_f | |
loop do | |
xn = (a+b)/2.0 | |
fxn = funcao.call(xn) | |
if funcao.call(a)*fxn < 0.0 | |
b = xn | |
else | |
a = xn | |
end | |
p "raiz proxima: #{xn}" | |
break if fxn.abs <= ERRO | |
end | |
end | |
def metodo_da_falsa_posicao(intervalo, funcao) | |
a = intervalo.begin.to_f | |
b = intervalo.end.to_f | |
loop do | |
fa = funcao.call(a).to_f | |
fb = funcao.call(b).to_f | |
xn = a - ((b-a)*fa/(fb-fa)) | |
fxn = funcao.call(xn) | |
if fa*fxn < 0 | |
b = xn | |
else | |
a = xn | |
end | |
p "raiz proxima: #{xn}" | |
break if fxn.abs <= ERRO | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment