Last active
October 17, 2023 22:16
-
-
Save ereboucas/2ddab523edc05636bf5c53e5a9e00d6c to your computer and use it in GitHub Desktop.
Gabriel Newton
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
import sympy | |
def newton(x0, tol, f): | |
x = sympy.Symbol("x") #Minhas funções serão em função de X | |
x1 = 0 | |
df = sympy.diff(f, x) | |
sub = 1 | |
while(abs(sub) > tol): | |
valorf = f.subs(x, x0) #Substituir x0 em x na função | |
valordf = df.subs(x, x0) #Substituir | |
x1 = x0 - (valorf / valordf) #Formula aplicada em Newton-Raphson | |
sub = x1 - x0 #Novo valor da subtração | |
x0 = x1 | |
return sympy.N(x1,5) #N:Representa o valor em decimal, caso não usasse, apareceria em formato de fração | |
x0 = float(input("Digite o x inicial ")) | |
tol = float(input("Digite a tolerancia ")) | |
entrada = input("Digite a função ") | |
funcao = sympy.sympify(entrada) #Transformar string em expressão da biblioteca sympy | |
print("A raiz é " + str(newton(x0, tol, funcao))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment