We need to deal with the approximation. There is an array, let's say a format (the format can be any, for example)
[1517443199] => 10285.10000000 [1517702399] => 9199.96000000 [1517961599] => 7652.14000000 [1518220799] => 8683.92000000 [1518479999] => 8903.00000000 [1518739199] => 10000.09000000 [1518998399] => 10383.43000000 [1519257599] => 10437.60000000 [1519516799] => 9694.51000000 [1519775999] => 10569.04000000 [1520035199] => 11039.00000000 [1520294399] => 11454.00000000 [1520553599] => 9271.64000000 [1520812799] => 9533.57000000 [1521071999] => 8170.00000000 [1521331199] => 7824.80000000 [1521590399] => 8909.98000000 [1521849599] => 8898.03000000 [1522108799] => 8134.23000000 [1522367999] => 7090.14000000 [1522627199] => 6813.01000000 [1522886399] => 6796.10000000 [1523145599] => 6895.80000000 [1523404799] => 6843.90000000 [1523663999] => 7877.41000000 [1523923199] => 8064.92000000 [1524182399] => 8278.00000000 [1524441599] => 8787.02000000 [1524700799] => 8869.99000000 [1524959999] => 9348.00000000 [1525219199] => 9071.48000000 [1525478399] => 9713.99000000 [1525737599] => 9429.02000000 where the key is the coordinates along the oX axis, the value is the coordinates along the oY axis.
The goal is to approximate the data so that the graph comes out as smooth as possible (in fact, the amount of data in the array is many, many more). For this, as you understand, interpolation is actually needed, but I decided to look at the results of the approximation of my graph.
Found a method for piecewise linear approximation on c ++. A quick movement of the hand transformed it under php.
function approximat($X, $Y, $x) { // Π²ΡΡΠΈΡΠ»Π΅Π½ΠΈΠ΅ Π±Π»ΠΈΠΆΠ°ΠΉΡΠ΅Π³ΠΎ Π±ΠΎΠ»ΡΡΠ΅Π³ΠΎ ΠΈ Π΅Π³ΠΎ ΠΈΠ½Π΄Π΅ΠΊΡΠ° $n = count($Y); $max = $X[0]; $indexMax = 0; // ΠΈΠ½Π΄Π΅ΠΊΡ for ($i = 0; $i < $n; $i++) { if ($X[$i] <= $max && $x <= $X[$i]) { $max = $X[$i]; $indexMax = $i; } } // Π²ΡΡΠΈΡΠ»Π΅Π½ΠΈΠ΅ Π±Π»ΠΈΠΆΠ°ΠΉΡΠ΅Π³ΠΎ ΠΌΠ΅Π½ΡΡΠ΅Π³ΠΎ ΠΈ Π΅Π³ΠΎ ΠΈΠ½Π΄Π΅ΠΊΡΠ° $min = $X[$n]; $indexMin = 0; for ($i = 0; $i < $n; $i++) { if ($X[$i] >= $min && $x >= $X[$i]) { $min = $X[$i]; $indexMin = $i; }; } // Π²ΡΡΠΈΡΠ»Π΅Π½ΠΈΠ΅ Π½ΡΠΆΠ½ΠΎΠ³ΠΎ Y $Y = (($max - $x) / ($max - $min)) * $Y[$indexMin] + (($x - $min) / ($max - $min)) * $Y[$indexMax]; return $Y; } We have not figured out how to apply it yet, we actually need to get a new array, both with points in intermediate values ββand with new values ββin existing ones.
The main question is - please help me deal with the approximation algorithm itself.
for ($i = $data_x[0]; $i < $data_x[count($data_x) - 1]; $i = $i + 100) { $data_nx[] = $i; } $inter = []; foreach ($data_nx as $x) { $inter[$x] = approximat($data_x, $data_y, $x); } data_nx - new xs data_y and data_x arrays of coordinates.
I assume that my function does not work quite right (based on the failure at the beginning of the graph). But in general, ok.