# Thread: Reverse engineering a function

1. ## Reverse engineering a function

I'm not sure if this is the correct forum for my question, so please move it as needed.

It has been many years since I did any advanced math, and I'm having some trouble determining how to come up with a function for a given set of inputs. I have a compiled program but no source code, and I want to use the function in a new program. I can enter values into the existing program and get a result, but I don't know the function for calculating the output. For example, 40x + 60y + 200z = 42 and 30x + 60y + 300z = 58.

Should I create equations for 3 sets of coefficients with solutions and solve the set of simultaneous equations?

2. It would be helpful to have a little more information here. Can you assume that the output is a linear combination of inputs? If so, then a linear system such as you suggest would work. (You'd want to test your model to make sure it works, once you have it all done. That is, compare your theoretical model with the output of the existing program and see if they're the same.) Do you have any other information as to what this function is computing?

Originally Posted by Ackbeet
It would be helpful to have a little more information here. Can you assume that the output is a linear combination of inputs? If so, then a linear system such as you suggest would work. (You'd want to test your model to make sure it works, once you have it all done. That is, compare your theoretical model with the output of the existing program and see if they're the same.) Do you have any other information as to what this function is computing?
I have input a set of values for x, y, and z and produced a set of outputs (in the last column) as shown below.

30 60 300 58
30 64 300 60
30 68 300 61
30 72 300 63
30 76 300 64
30 80 300 66

20 60 200 42
20 66 220 48
20 72 240 54
20 78 260 60

I don't know if I can assume the output is a linear combination of inputs. Some sets of combinations make it appear that the output is linear, and others make me unsure.

4. It's definitely not a linear function; at least, if your function outputs are exact. I used the three data points

30, 60, 300, 58
30, 64, 300, 60
20, 66, 220, 48

to obtain the function

$f(x,y,z)=\dfrac{83x}{30}+\dfrac{y}{2}-\dfrac{11z}{60}.$

This function does not satisfy all the data points, hence the function is not linear (as is).

You can see just by looking at the data that the function is not linear. In the first six data points, you're increasing the y component by the same amount each time, but the function value does not go up by the same amount each time.

Question, though: are these function outputs rounded off in any way? The behavior I'm seeing there would be reasonably linear if there was some rounding going on. Is there any way you can give me more significant figures?

5. ## Significant figures

Originally Posted by Ackbeet
It's definitely not a linear function; at least, if your function outputs are exact. I used the three data points

30, 60, 300, 58
30, 64, 300, 60
20, 66, 220, 48

to obtain the function

$f(x,y,z)=\dfrac{83x}{30}+\dfrac{y}{2}-\dfrac{11z}{60}.$

This function does not satisfy all the data points, hence the function is not linear (as is).

You can see just by looking at the data that the function is not linear. In the first six data points, you're increasing the y component by the same amount each time, but the function value does not go up by the same amount each time.

Question, though: are these function outputs rounded off in any way? The behavior I'm seeing there would be reasonably linear if there was some rounding going on. Is there any way you can give me more significant figures?
Unfortunately, the program interface forces all of the inputs and outputs to be integers. I agree w/ you that it may probably be a linear function but rounding or truncating of the output makes it appear that it's not linear.

The function I'm seeking doesn't need to be exact, so I'm sure what you've given me is sufficient. I will run some tests and compare the outputs of your function vs. the existing program.

BTW, what method did you use to find the function? Frankly, my inner geek is more interested in how to find the function than in actually using it. :-)

6. To get the function I just solved the system

30a + 60b + 300c = 58
30a + 64b + 300c = 60
20a + 66b + 220c = 48

for a, b, c. Then I set

f(x,y,z) = ax + by + cz.

It occurred to me that you could get a more accurate function (assuming it actually is linear!) by running least-squares fits over the data. The procedure would work as follows:

Step 1: find out how the function depends on x by obtaining at least nine points of data where all you vary is x.
Step 2: find out how the function depends on y by obtaining at least nine points of data where all you vary is y.
Step 3: find out how the function depends on z by obtaining at least nine points of data where all you vary is z.
Step 4: put all the information together by conglomerating the expressions, and double-checking the constants.

Then I think you'd get a pretty accurate assessment of the results.