php - X 天数内减少数量的分布

标签 php formula discrete-mathematics

我正在尝试绘制已知数量在 X 天内以递减方式的交付分布图。我需要有关可以在 php 中实现的公式的帮助。

常数为 10,000 个单位,起始值为平均分布的 300%(抱歉,术语不好)。

例如:

10,000 个单位将在 10 天内分发。第一天的交付量比正常平均水平高出 300%,即 3,000 个。这个数字将在接下来的 9 天内下降,直到全部交付完毕。

我可以通过 Excel 计算出一个系数,将每次交付乘以以减少它(本示例为 0.71),从而伪造上述示例。

我将在 php 中实现这个。天数的范围可以从 3 天到 365 天不等。

因此,理想情况下,该解决方案将允许我执行以下操作:

$units = array();
$startValue = (10000 / $daysToDeliver) * 3;
for ($x= 0, $x < $daysToDeliver, $x++) {
   //add the next deliver quantity onto the array
   $units[] = awesomefunction($lastDeliverAmount, $daysLeftToDeliver);  // guessing here
}

我知道我过度简化了函数的输入,只是想给出一个粗略的想法。

感谢您的时间和考虑!

最佳答案

正如 Manny Ramirez 提到的,这就像指数衰减函数;只是它不是连续的,而是离散的。您基本上想要生产:

seq(3a/n*(1-r)^(i-1)) for i from 1 to n

在你的情况下

a = 10000
n = 10
r = necessary value such that sum(seq(...)) == a == 10000

挑战是找到 r。我使用 a = Σ3a/n*(1-r)^(i-1) 表示 i 从 1 到 n。进行一些代数运算,我将其简化为:

3(1-r)^n+r*n-3=0

但是,老实说,如果没有计算机代数系统,就没有简单的方法来解决这个问题。坦率地说,尽管我很喜欢 PHP,但它在这种事情上很糟糕。 Python 中可能有一个模块可以为您执行此操作。也就是说...

一旦您使用了其中一种方法(或另一种方法)来查找 r,那么您需要构建数组:

function getUnits ($total, $days, $rate) {
  $start = $total * 3 / $days;
  $units = array();
  for ($i = 1; $i <= $days; $i++) {
    $units[] = $start * pow((1 - $rate), $i - 1);
  }
  return $units;
}

//try with the numbers we have here
$units = getUnits(10000, 10, 0.290271556501);
print_r($units);
echo array_sum($units);

这给出

>> Array ( [0] => 3000 [1] => 2129.185330497 [2] => 1511.1433905345 [3] => 1072.5014464679 [4] => 761.18478225207 [5] => 540.23449072289 [6] => 383.41978422523 [7] => 272.1239266649 [8] => 193.13409091071 [9] => 137.07275772865 )
>> 10000.000000004

关于php - X 天数内减少数量的分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21867814/

相关文章:

php - mysql_fetch_array问题

google-sheets - Google 电子表格 : conditional formatting with custom formula based on values from different sheet

rotation - Libgdx 两点度数计算

c++ - 使用集合测试关系的对称性

c++ - 方程式相等性测试(在C++或Unix工具中)(代数函数同构)

PHP 关联数组

php - PHP-MySQL 中 SELECT 参数值的编码

php - 从同一查询中的同一列获取两个值

Python - 打印输出而不是公式

algorithm - 找到满足约束的所有组合?