I want to add a number to a var. This number should be bigger when var is small and smaller when var is big. I have calculate the optimum values: when var=1, function should add 125. When var=50 function should add 420. I was thinking about sin function, but I have no idea how to "personalize" this function to work with it. (I am using php)
For a function with the form:
f[x_] := x + Sin[y*x + z]
Subject to the constraints
f[1] == 1 + 125 && f[50] == 50 + 420
You have
{{y -> 1/49 (-ArcSin[125] + ArcSin[420]),
z -> 1/49 (50 ArcSin[125] - ArcSin[420])}}
which is approximately
{{y -> 0. - 0.0247338 I, z -> 1.5708 - 5.49671 I}}
Between 0 and 70, this gives:
An approximate function, using only real values, is:
f(x) = x + 121.94629730754633 cosh(0.02473378688005212 x) +
121.94219707312345 sinh(0.02473378688005212 x)
The sine function is periodic and probably not suitable for the task.
Your example is not clear: you say add 'bigger number when var is small and smaller number when var is bigger', but add 125 to 1 and 420 to 50, which contradicts the text.
One possibility is the reciprocal function - it meets your stated requirements but not your example requirements.
Given just 2 data points, we can deduce a linear relationship:
y = 125 + (420 - 125) / (50 - 1) * (x - 1)
which is approximately:
y = 119 + 6x
Check:
x = 1; y = 125
x = 50; y = 419
The approximate factor 6 is a rounding of 6.0204081632 ... which is an intriguing sequence in the fractional part.
Try to make a linear equation projection.
VarAdd = Var*Slop+Start; eq [1]
125=1*Slop+Start ---1
420=50*Slop+Start –2
Solve Slop and Start then apply eq[1] any time.
Related
What I want to do is generate a float between 1.00 to 200.00, let's call this number X. The X determines how much a user "wins", think of it like a multiplier on a casino. The user bets 10$, X is 23.21, the user wins 10*23.21.
If the house and the user should have the same odds (+/- 0) in the long run, the chance of X being, for example, 200.00 should be 1/199 - 1/200. This means Y=1/(X-1) - 1/X where Y = percentage of the chance of X.
The percentage Y should be randomized and I was thinking to do a frand(0, 100, 15). Where:
function frand($min, $max, $decimals = 0)
{
$scale = pow(10, $decimals);
return mt_rand($min * $scale, $max * $scale) / $scale;
}
This means we will know Y, and therefor the next step should be using Y=1/(X-1) - 1/X to determine X. However, I do not know how I can achieve this in PHP. Other ways for the same function:
Y = 1/(X - 1) - 1/X
X^2 - X = 1/Y
X(X - 1) = 1/
So, my question is; how do I solve the X OR is there a better way to achieve what I am trying?
Say i have a slider with the Minimum value of -1, and the maximum value of 1. If the user puts the slider handle in the middle, then we would have a value of 0. I just don't know how to calculate that into the 50% of -1 to 1
I then need to do the same thing i reverse, first get the percentage of the slider (say it's 50%). I now need to calculate how much 50% is of -255 to 255 (which should return 0). I've been stuck with this one for a while now, and would appreciate any help i could get.
Thanks in advance!
Well you have two scales, one going from 0 to 100, the other going from -255 to 255. The formula to go from one to the other is quite simple :
progress = (position - min)/range
new_pos = new_min + new_range * progress
Note that a percentage already represents this "progress" value, as 20% = (20 - 0)/100. Also note that the range can be defined as max - min.
So assuming you go from -255 to 255 included, that's 511 discrete values (counting the 0, assuming you use int's), and assuming percentage is a value between 0 and 100 (as opposed to between 0 and 1) :
from percentage to pixels : -255 + 511 * percentage / 100
from pixels to percentage : 0 + 100 * (pixel + 255) / 511
The same goes for your scale from -1 to 1, which I guess uses continuous values, thus has a range the size of the interval [-1, 1], i.e. min = -1 and range = 2
This should do the trick:
function percentage($slider){
return ($slider + 1) * 50;
}
function slider($percentage){
return ($percentage/100) * 510 - 255;
}
I am not good with math and I can't wrap my head around this, I want to generate EVERY possible 3 positive numbers there is that sum to N example 100, for instance:
0 - 0 - 100
0 - 1 - 99
1 - 1 - 98
You don't need to answer me with PHP code, just a general idea on how I can generate those numbers would be sufficient.
Thanks.
Brute force is an option in your case: you can just use 2 nested loops
and it takes less than 10000 tests only.
// pseudo code
for (i = 0; i <= 100; ++i)
for (j = 0; j <= 100; ++j) {
if ((i + j) > 100)
break;
k = 100 - i - j;
print(i, j, k);
}
If duplicates e.g. 0, 0, 100 and 0, 100, 0 should be excluded, you can use slightly modified code:
// pseudo code
for (i = 0; i <= 100; ++i)
for (j = i; j <= 100; ++j) {
if ((i + j) > 100)
break;
k = 100 - i - j;
if (j <= k)
print(i, j, k);
}
As for just an algorithm, consider first just pairs of numbers whose sum are less than or equal to 100. These should be easy to list. I.e
0 1 2 100
{{0,0}, {0,1}, {0,2},.........., {0,100}
{1,1}, {1,2},..., {1,99}
.
.
...............................{50,50}}
But then each of those pairs, taking their sums can also be paired with precisely one number such that the entire triplet sum is 100.
So to summarize; if you could make first a list of these pairs (would require a double loop i in [0,100], j in [0:50]); and then loop through all pairs in this list calculating the third number you should get all triplets without duplication. Furthermore, if done correctly you wouldn't actually need any lists at all, with proper loop indexing you could calculate them in position.
edit Noticed you wanted duplicates - (though you could permute each triplet).
another approach with slightly better time complexity.
n=int(input())
for i in range(0,int(n/2+1)):
for j in range(0,int(i/2+1)):
print(j," ",i-j," ",(int)(n-i))
l=n-i
for j in range(0,int((n-i)/2+1)):
print(j," ",l-j," ",(int)(i))
it is just the extension of this algorithm which produces two numbers whose sum is equal to n
n=int(input())
for i in range(0,int(n/2+1)):
print(i," ",n-i)
I see you need those duplicates also just change the limits to full in line number 2,3 and 6
n=int(input())
for i in range(0,n):
for j in range(0,i):
print(j," ",i-j," ",(int)(n-i))
l=n-i
for j in range(0,l):
print(j," ",l-j," ",(int)(i))
I need to create a business logic or php function to compute the following: given some input $rank (which is the alexa ranking) I need to compute some $points in such a way that $points will be high for the top ranking website and will decrease with increasing $rank value.
I imagine something like this:
function($rank)
{
$points = x*$rank;
return $points;
}
How do I get $points in such a way that
if the rank is 1 then the points returned is maximum (e.g. 10000).
if rank is 2 then $points returned will be 9500 or nearby.
if rank is 4 then $points returned will be 6000 or nearby.
if rank is 200 $points returned will be 2 or whatever the function will return.
Rule: if $rank is less then $points should be more. Maximal value of $points is 10000 which is for $rank=1.
Now as the $rank increases the $points value should decrease accordingly.
There are many formulas which might satisfy your requiremements.
Nested powers
One possibility:
$points = 10000 * pow(0.993575964272119, pow($rank, 3.16332422407427) - 1)
This gives you the following results:
f(1) = 10000
f(2) = 9500
f(4) = 6000
f(9) = 12.065
f(10) = 0.84341
f(200) = 0
So the three values you fixed (1, 2 and 4) are all satisfied, but the result for 200 indicates that this might not be exactly what you're looking for. The curve looks like this:
By the way, I found this using python and mpmath, by fixing the form of the formula and determining the numbers with the many digits numerically:
>>> import mpmath
>>> print(mpmath.findroot((lambda a,b: 10000*a**(2**b - 1) - 9500,
... lambda a,b: 10000*a**(4**b - 1) - 6000),
... (0.995, 2.7)))
[0.993575964272119]
[ 3.16332422407427]
If you decide on a different form of the function, this approach might be adapted.
Exp of a polynomial
A possible different form with the desired properties would be this:
$points = exp(9.14265175282929 + $rank*(0.127179575914116 - $rank*0.0594909567672230))
This does not decrease quite as quickly as the one above:
f( 1) = 10000
f( 2) = 9500
f( 4) = 6000
f( 13) = 2.1002
f( 14) = 0.47852
f(200) = 0
It was obtained by solving this system of equations:
a + b + c = log(10000)
a + 2b + 4c = log( 9500)
a + 4b + 16c = log( 6000)
to obtain the coefficients a through c for the polynomial. One can add another degree to match f(200)=2 as well, but in that case, the last coefficient will become positive, which means that points will start to increase with rank for very large ranks.
If you want to match that f(200)=2 as well, you can do so using
$points = exp(max(8.86291000469285 - $rank*0.0408488141206645,
9.14265175282929 + $rank*(0.127179575914116 - $rank*0.0594909567672230)))
although this will result in a bend in your curve.
To compare these alternatives to the above:
function getPoints($rank)
{
$returnValue = -0.005 * $rank * $rank - 0.035 * $rank + 100.040;
if ($returnValue < 0) $returnValue = 0;
return $returnValue;
}
This was my thinking.
Function is not forking for large values:
it should atleast give some small value for large ranks...
like if rank is 2000000 then points will be 2.
Thnx btw
Wow, this one is though!
I'm trying to find in PHP the % of a value relative to the Y axis. If we refer to this graph : http://en.wikipedia.org/wiki/Semi-log_graph (2009 outbreak of influenza A), let's say that I want to find what % is a value "256" on the chart.
Visually, it's easy : it's a bit more than a 1/3 or 33%. If we look at the 1024 value, it's around 50% of the height of the Y axis. 131072 would be 100%.
So how do I calculate this with PHP?
Let's take this graph and take X = day 0 and Y = 256. What is 256 as a % of Y ?
Thanks a lot to anyone can compute this baby :)
percent = 100 * ( log(y) - log(y1) ) / ( log(y2) - log(y1) )
where
y = value
y1 = smallest value in y-axis
y2 = largest value in y-axis.
when y1 = 1.0 then you can simplify the other answers given here (since log(1)=0 by definition)
percent = 100 * log(y)/log(y2)
Note that not all log charts have 1.0 as the lowest value.
ln(131072) = 11.783 is 100%
ln(1024) = 6.931 is 58.824%
in PHP, that function is called log()
no need to set the base, as you are dividing them to find the relative value.
Alex got it, but to generalize for you and PHPize
logPercent = log(x) / log(top) * 100;
If you take the log of your max y value (131072 in your case) and the log of your y value (256), you end up with a height and a y value which is linear in relation to your drawn axis. you can divide them to get a decimal of the height and times by 100 for %:
using log base 2 seen as it gives integers (though any base should be fine).
log(256) / log(131072) = 8/17 = 0.47 = 47%
in php:
(log(256, 2) / log(131072, 2))*100;