Math Graph to Equation
Is there a tool which converts a graphi开发者_如何转开发cal representation of an equation to that equation? (Graphical representation to aprox. math equation)
This is a tricky problem, usually referred to as interpolation. For simple polynomial graphs it's an easy problem. (You can always find an "exact match".) Have a look at polynomial interpolation. But you could also have a graph that represents some trigonometric function. Or how about exponential functions or logarithmic functions. Or worse, combinations! Even for simple graphs there may be thousands of interesting potential equations.
Even if you do check all interesting equations, you should still be careful. Consider the equation y = A * sin(B*x)
, with extremely large values for A
and B
. How does that graph look? Well, it goes up and down between A
and -A
over and over again, really really fast, and "hits" or "almost hits" just about all points. It's a "simple" formula, which mathematically looks like a good approximation, but it's still most likely not something you would want in the end.
A common problem that might fit your description is called curve fitting: you have some data (that, in your case, you've read from a graph) and you have in mind a form of an equation, and you want to find what parameters you need to best fit the equation to the graph.
A useful approach to this is fit to least squares error. A least squares package will be available in most data analysis tool kits.
Here's an example: Say the equation is A*sin(2*pi*100.x)*x^B, and I need to find the values of A and B that give me the best fit (A=10.0 and B=3.0 in this example).
Here's the code used to generate this fit. It uses Python and Scipy and is modified from an the example here.)
from numpy import *
from scipy.optimize import leastsq
import matplotlib.pyplot as plt
def my_func(x, p): # the function to fit (and also used here to generate the data)
return p[0]*sin(2*pi*100.*x)*x**p[1]
# First make some data to represent what would be read from the graph
p_true = 10., 3.0 # the parameters used to make the true data
x = arange(.5,.5+12e-2,2e-2/60)
y_true = my_func(x, p_true)
y_meas = y_true + .08*random.randn(len(x)) # add some noise to make the data as read from a graph
# Here's where you'd start for reading data from a graph
def residuals(p, y, x): # a function that returns my errors between fit and data
err = y - my_func(x, p)
return err
p0 = [8., 3.5] # some starting parameters to my function (my initial guess)
plsq = leastsq(residuals, p0, args=(y_meas, x)) # do the least squares fit
# plot the results
plt.plot(x, my_func(x, plsq[0]), x, y_meas, '.', x, y_true)
plt.title('Least-squares fit to curve')
plt.legend(['Fit', 'Graph', 'True'])
plt.show()
I've seen some tools that fit equations to graphs in images but I can't recall their names right now. A quick Google search turned up this commercial application: http://imagedig-2d-3d-image-digitizer.smartcode.com/info.html
精彩评论