# Generate Functions/Formulae From Graph

How do you mathematically convert a graph into a function or set of functions?

I am building an app and I need a specific animation pattern, I can visualize it in a graph, but I want to also change that graph to a formula that I can easily plug in values and regenerate that graph pattern, but I have no idea where to start.

Below is an example graph, all in the positive axis. Each line represents 1 unit.

• Would you have an example graph? – Matti P. Jul 20 '20 at 7:26
• I'm not sure if this would work for your case, but if you have the discrete values of some function you can use a graphing software to obtain a curve-fitting. If it's a really "weird curve" you can choose to fit a polynomial, and you'll get an expression similar to a Taylor polynomial, so locally you can make a pretty good aproximation. If the data already looks exponential, logarithmic or something like that, you can try fitting a function like this and see if it's close enough to what you want. – Robert Lee Jul 20 '20 at 7:30
• @MattiP. I have added an example graph – James Okpe George Jul 20 '20 at 9:00
• It would be tremendously useful if you knew a specific form that all the functions have to have. For example a polynomial: $$f(x) = a_0 + a_1 x + a_2 x^2 + a_3 x^3 + \ldots + a_n x^n$$ for some real numbers $a_i$ and integer $n$. So that's the first question that I would research. If this turns out to be true, I think the problem can be solved; otherwise I can't guarantee ... – Matti P. Jul 20 '20 at 9:05

If the only thing you have to base your analysis on is the graph, without any idea if the function is a polynomial, exponential, logarithmic, or any other combination of elementary functions, I can think of 2 methods you can use to find a formula.

## Method 1: Brute force

1. You can import the image of your graph into a software that allows you to establish a coordinate system. Using this, you can obtain the coordinates of several points on your graph:

In the example above I used a software called Tracker. The pink lines represent the $$x$$ and $$y$$ axis. I fixed the axis in the place they are to coincide with the $$x$$ and $$y$$ axis of the graph. I also establish a scale using a scale bar (which you can see as the blue line in the image) which makes the measurements I take on this software be in a $$1:1$$ ratio with the graph in the original image.

Using this program, I obtain the $$x$$ and $$y$$ coordinates of several points that are (approximately) on the graph (which in the image above are the red points).

1. Once you have a list of $$(x,y)$$ coordinates on your graph, you can import these values into a graphing software. Once here, you can plot these values as discreet data and tell the program to do a curve fitting on these points. A curve fitting is a process in which the program looks for a function (of the type you indicate it to look for) which is closest to the discreet values you plotted. This is an example of how a fitting looks like:

For the test above, I used a graphing software called OriginPro, but you can use a variety of tools. Wolfram Mathematica, MATLAB, and Gnuplot are all good alternatives that will get also get this step done.

In the picture above, I told the program to fit a polynomial of degree $$6$$. The resulting function (which in the picture is seen as the red line) was the following: $$f(x)\ =\ 1.42604\ +\ 5.6822x-2.11675x^{2}+0.33197x^{3}-0.02483x^{4}+0.000770751x^{5}-0.00000422806x^{6}$$

1. After this, you're pretty much done. You can overlay the function and the original image of the graph to see how much they deviate from each other:

As you can see, the function does not perfectly match up to the original graph but depending on how close you need the results to be, you can get a really good approximation.

### Notes:

• The reason I chose a polynomial as the "type" of function to be fitted (instead of an exponential or trigonometric function, for example) was, as I explained in a previous comment, inspired by the idea of Taylor polynomials. If you have a well-behaved image of a graph, then Taylor's theorem likely applies to it, which means you can obtain a relatively good local approximation of any nice function using a polynomial.

• I told the program to give a degree 6 polynomial since the fittings of lesser degree deviated even more from the original data, and fittings of higher degree started to have many "twists and turns" in sections where the original graph looks straight. If what you only care about is that the data is close to the function, a higher degree will always approximate it better (since it has more liberty to get closer to the discreet values), but it may not look as "nice" as the original image.

## Method 2: Analytical solution

If you already have an idea of the "type" of function that the graph is supposed to be (i.e. polynomial, trigonometric, exponential, etc) you can obtain a function using analytic methods.

In our example, one could make the assumption that the graph in the image represents some cubic polynomial since we can kind of see that it has a shape similar to this family of functions. Following this idea, we then want to find constants $$a,b,c$$ such that $$f(x) = ax^3 + bx^2 + cx$$ Observation: Here we're using the assumption that the function crosses the origin, hence there is no $$+d$$ constant term. This assumption again comes from looking at the shape of the graph.

Once this is established, we only need to know 3 points that are on the graph (one for each of the constants we want to find). By using software like the one used in step $$1$$ of the brute force method, we can find the coordinates of some of these points. For example, we can choose the points: $$(2.479, \ 6.227) \qquad (10.95, \ 2.828) \qquad (6.948, \ 4.317)$$ And on the image they look like this:

Now, since we want each of these points to be a solution to our function, we can obtain the following system of equations:

$$\begin{cases} 6.227 = a(2.479)^3 + b(2.479)^2 + c(2.479) \\ 2.828 = a(10.95)^3 + b(10.95)^2 + c(10.95) \\ 4.317 = a(6.948)^3 + b(6.948)^2 + c(6.948) \\ \end{cases}$$

From here you just need to solve for the values of $$a,b,c$$ and you have your function. You can use WolframAlpha to solve the system of equations, which results in the approximate values for $$a,b,c$$ being: $$a \approx 0.0392303, \qquad b \approx -0.792865, \qquad c \approx 4.23633$$ which means our cubic polynomial (which passes through the points we picked) is given by $$f(x) = 0.0392303x^3 -0.792865x^2 + 4.23633x$$ If we were to plot this function on top of our image, we can see how much they look alike:

As you can see, this approximation is even worse than the one from method 1. This is likely due to the fact that the function in the graph was never a cubic polynomial to begin with, so we were never going to get a cubic function that matched it.

The analytical method can be a lot simpler and much less time consuming than the brute force method, however, you need to know what type of function you're dealing with. Otherwise (as we've just shown) you won't get a good approximation.

As a final note, I'll also leave you with the Desmos graph I used in the pictures, in case you want to change some of the values and see if you get a resulting function you like more.

I'm not sure if any of these methods gives you what you were looking for, but I hope some of this helps. Good luck!

• The second method is perfect, it is what I want, thank you. – James Okpe George Jul 21 '20 at 8:36