skij
asked on
Adjust number proportionately
x may be any positive number.
y may be any positive number.
f should be the the value of x, adjusted proportionately so that:
• if the value of y is 1, the value of f should be 1
• if the value of y is 50, the value of f should be x
• if the value of y is 100, the value of f should be 1
• if the value of y is less than 1, the value of f should be less than 1
• if the value of y is more than 100, the value of f should be less than 1
Eventually, I will incorporate this into a PHP script.
Using math, math functions, absolute values and algebra are acceptable.
However, I would like the solution to be as simple as possible.
y may be any positive number.
f should be the the value of x, adjusted proportionately so that:
• if the value of y is 1, the value of f should be 1
• if the value of y is 50, the value of f should be x
• if the value of y is 100, the value of f should be 1
• if the value of y is less than 1, the value of f should be less than 1
• if the value of y is more than 100, the value of f should be less than 1
Eventually, I will incorporate this into a PHP script.
Using math, math functions, absolute values and algebra are acceptable.
However, I would like the solution to be as simple as possible.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Is f always positive?
Proportional is subjective. You said you didn't want linear proportionality... so did you want hyperbolic, harmonic, exponential, or some other type of proportionality?
ASKER
f is based on y so that if y is positive, f should also be positive. Because y is always positive, f is also always positive. Exponential proportionality would be preferred.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
We could use between 1 and 101 instead of 1 and 100 if it would peak more symmetrically.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And just in case you have only data and need to know how to calculate everything, including debug:
<?php
$x = 10; // test value (input)
echo 'x = ' . $x . '<br>';
$data = array(1,4,9,16,25); // test data (input)
$ave = array_sum($data) / count($data); // MEAN (average)
echo 'm = ' . $ave . '<br>';
$ss = 0;
echo 'data[';
$d= '';
foreach ($data as $key => $value){
$ss = $ss + pow(($value - $ave), 2); // add each deviation squared to running total
$d .= $value . ',';
}
$d = rtrim($d, ',');
echo $d . ']<br>';
echo 'ss = ' . $ss . '<br>'; // SUM OF SQUAREs deviations
$v = $ss / count($data); // VARIANCE (population)
echo 'v = ' . $v . '<br>';
$stdev = sqrt($v); // STANDARD DEVIATION (population)
echo 'stdev = ' . $stdev . '<br>';
$z = ($x - $ave)/$stdev; // Z-SCORE of X
echo 'z = ' . $z . '<br>';
$y = exp(-pow($z, 2)); // Gauss proportionality to mean.
echo 'y = ' . $y . '<br>';
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If x is 0 then f should be 0 regardless of the value of y.
y will never be 0.