I am attempting to build a program that finds (roughly) the equation of a graph given the coordinates of a bunch of points on the graph. The assumption is made that the coordinates given represent the entirety of the graph (ie: the behavior does not change outside the given domain).
I use the simple function below to do this:
#x and y are arrays of the x coordinates and corrosponding y coordinates
def get_equation(x,y):
degree = 2
coefs, res, _, _, _ = np.polyfit(x,y,degree, full = True)
ffit = np.poly1d(coefs)
print (ffit)
return ffit
This works reasonably well for the coordinates on the graphs of basic equations such as x^2, but does not work at all for more complicated graphs such as the graph below.
How do I find the equation for more complex graphs like the one above given the coordinates on the graph?
Also, is it possible to figure out what the degree of the graph is, or does that always have to be entered manually?