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. Result on face I assume that my function does not work quite right (based on the failure at the beginning of the graph). But in general, ok.

1 answer 1

Let's understand:

$ X is an array with oX;

$ Y is an array with oY;

$ x is the new oX value for which we want to get oY.

So, if your array is called $ARRAY , then we know how to stuff the first 2 values ​​there:

$ X = array_keys ($ ARRAY);

$ Y = array_values ​​($ ARRAY);

The third parameter is the new value.

Based on this, for example, you have an $ARRAY with values, and you also need to get values ​​for the $NEW array, with coordinates in oX : 1, 2 ΠΈ 3 . Sobsna:

 $ARRAY = array(...); // Π’Π°Ρˆ массив $X = array_values($ARRAY); // ΠŸΠΈΡ…Π°Π΅ΠΌ ΠΊΠ»ΡŽΡ‡ΠΈ (наши oX) $Y = array_keys($ARRAY); // ΠŸΠΈΡ…Π°Π΅ΠΌ значСния (наши oY) $NEW = array(1,2,3); // Массив с oX, для ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Π½Π°Π΄ΠΎ Π½Π°ΠΉΡ‚ΠΈ oY $RESULT = array(); // Новый массив, Π³Π΄Π΅ ΠΊΠ»ΡŽΡ‡ Π±ΡƒΠ΄Π΅Ρ‚ oX, Π° Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ oY foreach ($NEW as $NEW_X) { $RESULT[$NEW_X] = approximat($X, $Y, $NEW_X); } 

Now, to build a graph, you have your points in $ARRAY , and those that were unknown (and only oX was known) in $RESULT , and then figure it out.

PS This is by no means an optimal example, but the optimization will take place on your own.

  • Thank you, I tried in a similar way, but something went wrong) - sinica
  • Made changes to the question - sinica