Skip to content

Instantly share code, notes, and snippets.

@loderunner
Created March 17, 2015 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loderunner/c6fbed74b358461363c4 to your computer and use it in GitHub Desktop.
Save loderunner/c6fbed74b358461363c4 to your computer and use it in GitHub Desktop.
from numpy.polynomial.polynomial import polyval
from scipy.optimize import newton
from random import random
def poly(coeffs):
def polyfunc(x):
return polyval(x, coeffs)
return polyfunc
# Polynomial: coeffs[0]*x^0 + coeffs[1]*x^1 + coeffs[2]*x^2 + ...
# coeffs are random numbers between 0 and 1
coeffs = [random() for i in range(41)]
print 'y = ' + ' + '.join(['%.3f*x^%d' % (coeff, i) for i, coeff in enumerate(coeffs)])
print 'x0 = 0'
print newton(poly(coeffs), 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment