3

I don't have much mathematical background except for highschool and I'm struggling to design a very simple function.

I need a function f(x, y) that for the absolute difference of x and y would return a number between 0 and 4. If x, y are equal, it would return 4 lets say. If they have difference over some specific number (like 8) it would return value that is zero, or one that is very close to zero, and same goes for higher difference. It also is a requirement that the value that the function returns does not change linearly but lets say if the difference is 0, it would be 4, if its 2-3 it would already return ~2.

Then I need another function that does the same thing but instead of being based on the absolute difference of x and y, it is based on their percentage difference. Like, if x is ~35% different then y to return 0, if they are equal to return 4, again changing in non-linear way.

I'm probably terrible to describe - I need this for a system that compares vehicles from a database. So this is one component of it, lets say VehicleA has year of manufacturing 2000, and VehicleB is 2004, I want to assign a weight how similar they are:

My current out of my fingers solution:

(20 / (5 + ABS(2000 (this is `x`) - 2004 (this is `y`)) * 2))

The part with the percentage difference is if I compare vehicle engine size and power. I tried but did not succeed to create such function.

I "designed" it by playing with examples, I know there must be a more scientific way but I don't know where to look for :(

ddinchev
  • 133
  • Are you allowed to use conditionals? (e.g. 'if $|x-y|>8$ then ....') If not, are you allowed to use the floor function? – zuggg Jul 16 '13 at 06:39
  • Yes! It's for programming solution. Basically in SQL, which allows IF, CEIL, FLOOR, GREATEST(x,y), LEAST(x,y), POW and other common flow and math functions. – ddinchev Jul 16 '13 at 07:34

1 Answers1

3

Your function consists of "steps", there are 3 usual methods to achieve this effect:

Play with it and tweak it to your needs. It is worth noting, that all the above are, in fact, piecewise somewhere inside. For the other function, change $|x-y|$ to what you need, i.e. percentage, ratio, whatever.

I hope this helps ;-)

dtldarek
  • 37,381
  • Brilliant, upvoted! I will play with this later at home and accept when I get home when I implement a solution. – ddinchev Jul 16 '13 at 07:39