views:

96

answers:

2

Hi, 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.

+1  A: 

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.

sven
very nice explanation. This seems to produce some of the results i was hoping for. I can manage from here, thanks very much!
Ropstah
Could you describe this 'kind' of equation/formula? It's name or something?
Ropstah
+1  A: 

Sounds like a opportunity to fit a curve to the data and them use those coefficients to give a result for two input variables.

To get started, plot out you expected results on graph paper, see if you have a plot that makes sense.

Don
This sounds logical to me, however I'm not a mathematician... I'm trying to build a formula to calculate a battle between two units in a war simulator. The first values are attack points, the second are defence points. There should always be a result higher than 0, as every attack inflicts damage, however those should gradually increase as the values increase. Is there perhaps a better solution?
Ropstah