Search code examples
phpmathmathematical-optimization

How can I convert this 'math model' to php?


i have mathematical problem which I'm unable to convert to PHP or explain differently. Can somebody send my in the right direction?

I have two number which are in order (sequence). #1 and #2. I want to somehow compare those two numbers and get a positive number lower than 100 as the result of the comparison. The higher the values, the higher the result should be. However, if #2 is also high, then the result should be "dimmed" accordingly...

These are the "expected results":

#1: 5           #2: 10         => ~ 5
#2: 10          #2: 5          => ~ 8
#1: 50          #2: 60         => ~ 12
#1: 50          #2: 100        => ~ 8
#1: 100         #2: 50         => ~ 20
#1: 500         #2: 500        => ~ 25
#1: 500         #2: 100        => ~ 50
#1: 100         #2: 500        => ~ 15

The number (1 or 2) values are ranged between 0 and 1000. The results are just estimates, they will obviously be different. I just want to show how it is related.


Solution

  • At first you should try to set up a clean mathematical model by defining a (mathematical) function f(x, y) = z where x = #1, y = #2 and z = your output. I couldn't state such a function by your data, but that's the basis which you need whenever you want to implement your problem in a programming language.

    So let's say you want something like

      f(x, y) =  {
           50 + (x - y) * 50/y   if  y > x
           (y - x) * 50/x        if  y 
    
    

    this function compares #1 (x) and #2 (y) and gives as a result a number between 50 and 100 or 0 and 50, depending whether x or y is bigger.

    Implementing a mathematical function right in any programming language like PHP is very easy:

    function f(x, y) { return (y > x) ? ( 50 + (x-y)*50/y ) : ( (y-x) * 50/x ); }

    Now you can call that function from your code or e.g. via some HTML form.