Solved

Linear Decrease Algorithm

Posted on 2008-06-19
19
406 Views
Last Modified: 2011-10-19
Hello,

Could someone please provide a PHP Algorithm which will result in a smaller value being returned when a larger value is provided.

E.g.

x(25); //returns say 50
x(50); //returns say 49.3
x(100000); //returns say 1 etc

function x($value) {
code..
return($value) {
}

Also i would like it where negative values will not be returned.

Regards Shinji.
0
Comment
Question by:nerotech
  • 7
  • 5
  • 4
  • +2
19 Comments
 
LVL 2

Expert Comment

by:kszurek
ID: 21822699
Maybe this:
function x($a)
{
	return rand(0, $a-1);
}
echo x(25);

Open in new window

0
 

Author Comment

by:nerotech
ID: 21822767
Thanks for the response, but this is not what i'm after.

I need it so that the return value is constant, not randomized.
0
 

Author Comment

by:nerotech
ID: 21822861
Note: i should have named the topic "nonlinear decrease algorithm", as i wish to cause the decrease to become more significant as the provided value increases.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 21822867
Nerotech: What rules would you apply to determine how much smaller the return value should be?
0
 
LVL 2

Expert Comment

by:kszurek
ID: 21822937
Strange solution:
<?PHP
function x($a)
{
	$b = substr(abs(crc32($a)), 0, 1);
	return ($a-$b > 0 ? $a-$b : 1);
}
echo x(46);
?>

Open in new window

0
 

Author Comment

by:nerotech
ID: 21823038
Well initially I would prefer the returned value to be ~10% less of the provided value, as the value increments I would like the percentage to decrease more rapidly and Eventually the returned value would be 99.9% less than the original.

The point at which 99.9% less should be >=10,000

To summarize:

Start value >=0 && <=25 (-10%)
End value >=10,000 (-99.9%)

Percentage removed per increment will initially start slow, but will become more apparent at ~1,000.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21823130
What kind of calculations are you doing? Something like showing an animation in x seconds with a varying start/end or something?
0
 
LVL 20

Expert Comment

by:virmaior
ID: 21823204

function non_linear_decrease($input)
{
  if ($input < 0 ) { return 0; }
  
  $input_temp = $input;
  $multiplier = 1;
  while (($input_temp / 10) )  > 10) {
    $multiplier = $multiplier * .9;
    $input_temp = $input_temp / 10; 
  }
 
  return $input * $multiplier;
}

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 21823250
Still not sure I understand the function.  Could you sketch a curve that would show what you're seeking?
nerotech-function.gif
0
 

Author Comment

by:nerotech
ID: 21823363
Yes Ray, thats exactly what i'm after.
0
 
LVL 20

Expert Comment

by:virmaior
ID: 21823398
shinji,
how do the results of the function i supplied compare to what you want?
0
 

Author Comment

by:nerotech
ID: 21823538
Virmaior: its not what i'm after.

Test values:
25-100 (0% difference)
101 (9.1% difference)
10,000 (19% difference)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 21823571
Shinji: I have to leave and can't work on this, but it looks like you need a parabola with a horizontal axis.

http://www.intmath.com/Plane-analytic-geometry/4_Parabola.php

Good luck! ~Ray
0
 
LVL 20

Expert Comment

by:virmaior
ID: 21823627
K‰“Uc_hFg‚mSŒoiFgYK

function nonlinear_decrease($input)
{
   if (($input > 25) and ($input < 100)) { return $input; }
  return $input * (1 - log($input,1.62));
}

that gets the values you are looking for
(19% for 10000) and (9% for 100)
0
 
LVL 20

Expert Comment

by:virmaior
ID: 21823729
or rather return $input * (1 - ( log($input,1.62) / 100));

kore de ha nihon no kanji wo kaite dekimasen. shikashi, saisho no hoshikatta no kotae to ima no sagashiteru no kotae ha chigaimashita no?
0
 

Author Comment

by:nerotech
ID: 21823768
Sorry Virmaior, i should have been less ambiguous with my response (and original question).
Those were the test values & results i used on the function that you provided earlier.

The values i need are:

Start value >=0 && <=25 (-10%)
End value >=10,000 (-99.9%)

with nonlinear increase (see the graph Ray posted).

Ray is correct in pointing out that I require a parabolic function (Quadrant I).
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 21827656
nerotech, I looked at this again.  I think you may want an asymptotic curve - not truly a half-parabola, but it meets all the needs you expressed.  HTH, ~Ray
<?php
 
// CONSTRAINED ASYMPTOTE ALGORITHM
// http://en.wikipedia.org/wiki/Asymptote
 
 
define("MINMIN", 0.0);
define("MAXMIN", 25.0); // NOT USED, PRESENT FOR ILLUSTRATION
define("MAXMAX", 10000.0);
define("MINPCT", 0.1);
define("MAXPCT", 0.999);
 
 
function asymptote($value) {
 
// NO VALUES BELOW MINIMUM THRESHOLD
	if ($value <= MINMIN) {
		return FALSE;
	}
 
// COMPUTE A PERCENTAGE
	$percent = $value / MAXMAX;
 
// CONSTRAIN MINIMUM
	if ($percent < MINPCT) {
		return $value - ( $value * MINPCT );
	}
 
// CONSTRAIN MAXIMUM
	if ($percent > MAXPCT) {
		return $value - ( $value * MAXPCT );
	}
 
// WITHIN CONSTRAINTS RETURN DIMINISHED VALUE
	return $value - ( $value * $percent );
 
} // END function asymptote()
 
 
 
$kount = 1.0;
while ($kount < MAXMAX) {
	$asymptote = asymptote($kount);
	$pctg = 1.0 - $asymptote / $kount; // PERCENTAGE OF REDUCTION INCREASES AS VALUE INCREASES 
	echo "<br /> $kount $asymptote $pctg \n";
	$kount++;
}
 
?>

Open in new window

0
 

Author Comment

by:nerotech
ID: 21829081
Great, thanks alot Ray, works perfect! :)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 21830625
Glad to help!  BTW, that algorithm (when unconstrained) is the central element of single-server queueing theory.  Response Time = Service Time divided by (1 - Percent Busy).  I have used it for years to predict computer performance under increasing loads.  Nice to see it serves your needs, too.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Test if unique ID is in log file 5 31
Correct syntax for subdirectories 9 33
How do I show metrics with PHP or Javascript 6 46
MySQL Persistent Connections 10 30
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question