fork download
  1. def explog(a,n):
  2. if not n:return 1
  3. if not n%2:
  4. exp = explog(a,n//2)
  5. return exp * exp
  6. return a * explog(a, n-1)
  7.  
  8. def evaluarPol(coeficientes,x):
  9. cont= 0
  10. grado = len(coeficientes) - 1
  11. for i in range(grado + 1):
  12. cont += coeficientes[i]*explog(x, grado - i)
  13. return cont
  14.  
  15. def solucion_polynomio(coeficientes,rango=(-100,100),err=0.000001):
  16. a, b = rango
  17. if evaluarPol(coeficientes,a)* evaluarPol(coeficientes,b) > 0:return []
  18. L= []
  19. while b-a >= err:
  20. m =(a+b)/2
  21. if not evaluarPol(coeficientes,m):
  22. L.append(m)
  23. a=m
  24. b=m
  25. if evaluarPol(coeficientes,m)*evaluarPol(coeficientes, a)< 0:
  26. b=m
  27. else:a = m
  28. L.append((a+b)/2)
  29. return L
  30.  
  31. # Ejemplo de uso
  32. coeficientes = [3, 2, 6, 2]
  33. print("Coeficientes del polinomio:", coeficientes)
  34. print("Soluciones:", solucion_polynomio(coeficientes))
  35.  
  36.  
  37.  
  38.  
Success #stdin #stdout 0.04s 9684KB
stdin
Standard input is empty
stdout
Coeficientes del polinomio: [3, 2, 6, 2]
Soluciones: [-0.35286955535411835]