tags:

views:

133

answers:

3

Hi!

Is there a tool which converts a graphical representation of an equation to that equation? (Graphical representation to aprox. math equation)

+3  A: 

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.

aioobe
@aioobe: Any continuous function can be approximated very closely by polynomials, be it trigonometric or a worse combination of them. A high degree polynomial should be good enough for most functions, especially ones that can be graphed. So even though the actual equation might be different, the polynomails will give a very good approximation of the graph. See: http://mathworld.wolfram.com/WeierstrassApproximationTheorem.html
Moron
Well, have you heard of [Runge's phenomenon](http://en.wikipedia.org/wiki/Runge%27s_phenomenon)? Sure, a high-degree polynomial will "solve" it but it will still probably not be what you're looking for.
aioobe
@Moron: Not any continuous function. For example, `y=sin(1/x)` is continuous over the open interval `0 < x < 1`, but can't be approximated by a polynomial since it has an infinite number of turning points. The most you can say is "any function which kind of looks like a polynomial can be approximated by a polynomial".
Mike Seymour
@Mike: Any function continuous on a bounded interval can be approximated with polynomials. Since we are talking of graphs with start and end x co-ordinates, I believe that still applies. I could have been clearer, but the page I linked to should dispel an doubts about that.
Moron
I suppose it depends on how you define approximate.
aioobe
@aioobe: Have you heard of Bernstein polynomial?
Moron
@aioobe: btw, I was only trying to support your answer (at least in theory). My guess is any interpolation you do will have issues like Runge phenomenon.
Moron
+1  A: 

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

lhf
+1  A: 

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).

alt text

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()
tom10
+1 for the effort of explanation. Just want to point out that there are other methods like maximum likelihood extimation that are more accurate in some instances. See this paper for a nice introductory explanation http://www.scribd.com/doc/7372377/Tutorial-on-Maximum-Likelihood-Estimation
nico
@nico - You're right that MLE is more accurate in some instances, but not this one. Least squares is an accurate, common, quick and easy one liner, and is the right choice for a question where I need to define "curve fitting". Anyway, although I'm usually with you in thinking that normal distributions are over-used, here it's a reasonable assumption, and for this case, least squares and MLE are the same thing.
tom10
Using any fitting approach requires you to have a guess (or set of guesses) as to what functional form the solution will have. Accordingly this approach is useful in some cases and not in others.
dmckee
@tom10: sure, I was just pointing out another method :)
nico