gpt4 book ai didi

php - 更新图形上的样条类,使最高峰为标记位置 - JPGraph

转载 作者:可可西里 更新时间:2023-10-31 23:27:32 25 4
gpt4 key购买 nike

我使用 Jpgraph ( http://www.jpgraph.com ) 创建了一个图表。

Jpgraph 有一个样条类,可以平滑折线图上的线条。但是它没有像我预期的那样工作。如您所见,该线的最高峰超出了实际标记(红色方 block )。周五晚上 10 点是 2.0,但似乎比周五早上 6 点左右高。

enter image description here

下面是我所期望的一个糟糕的笔记本电脑触摸板绘制示例 :) enter image description here

我已经设法追踪到 jpgraph_regstat.php 的类。知道如何使峰的中心成为实际标记吗?

//------------------------------------------------------------------------
// CLASS Spline
// Create a new data array from an existing data array but with more points.
// The new points are interpolated using a cubic spline algorithm
//------------------------------------------------------------------------
class Spline {
// 3:rd degree polynom approximation

private $xdata,$ydata; // Data vectors
private $y2; // 2:nd derivate of ydata
private $n=0;

function __construct($xdata,$ydata) {
$this->y2 = array();
$this->xdata = $xdata;
$this->ydata = $ydata;

$n = count($ydata);
$this->n = $n;
if( $this->n !== count($xdata) ) {
JpGraphError::RaiseL(19001);
//('Spline: Number of X and Y coordinates must be the same');
}

// Natural spline 2:derivate == 0 at endpoints
$this->y2[0] = 0.0;
$this->y2[$n-1] = 0.0;
$delta[0] = 0.0;

// Calculate 2:nd derivate
for($i=1; $i < $n-1; ++$i) {
$d = ($xdata[$i+1]-$xdata[$i-1]);
if( $d == 0 ) {
JpGraphError::RaiseL(19002);
//('Invalid input data for spline. Two or more consecutive input X-values are equal. Each input X-value must differ since from a mathematical point of view it must be a one-to-one mapping, i.e. each X-value must correspond to exactly one Y-value.');
}
$s = ($xdata[$i]-$xdata[$i-1])/$d;
$p = $s*$this->y2[$i-1]+2.0;
$this->y2[$i] = ($s-1.0)/$p;
$delta[$i] = ($ydata[$i+1]-$ydata[$i])/($xdata[$i+1]-$xdata[$i]) -
($ydata[$i]-$ydata[$i-1])/($xdata[$i]-$xdata[$i-1]);
$delta[$i] = (6.0*$delta[$i]/($xdata[$i+1]-$xdata[$i-1])-$s*$delta[$i-1])/$p;
}

// Backward substitution
for( $j=$n-2; $j >= 0; --$j ) {
$this->y2[$j] = $this->y2[$j]*$this->y2[$j+1] + $delta[$j];
}
}

// Return the two new data vectors
function Get($num=50) {
$n = $this->n ;
$step = ($this->xdata[$n-1]-$this->xdata[0]) / ($num-1);
$xnew=array();
$ynew=array();
$xnew[0] = $this->xdata[0];
$ynew[0] = $this->ydata[0];
for( $j=1; $j < $num; ++$j ) {
$xnew[$j] = $xnew[0]+$j*$step;
$ynew[$j] = $this->Interpolate($xnew[$j]);
}
return array($xnew,$ynew);
}

// Return a single interpolated Y-value from an x value
function Interpolate($xpoint) {

$max = $this->n-1;
$min = 0;

// Binary search to find interval
while( $max-$min > 1 ) {
$k = ($max+$min) / 2;
if( $this->xdata[$k] > $xpoint )
$max=$k;
else
$min=$k;
}

// Each interval is interpolated by a 3:degree polynom function
$h = $this->xdata[$max]-$this->xdata[$min];

if( $h == 0 ) {
JpGraphError::RaiseL(19002);
//('Invalid input data for spline. Two or more consecutive input X-values are equal. Each input X-value must differ since from a mathematical point of view it must be a one-to-one mapping, i.e. each X-value must correspond to exactly one Y-value.');
}


$a = ($this->xdata[$max]-$xpoint)/$h;
$b = ($xpoint-$this->xdata[$min])/$h;
return $a*$this->ydata[$min]+$b*$this->ydata[$max]+
(($a*$a*$a-$a)*$this->y2[$min]+($b*$b*$b-$b)*$this->y2[$max])*($h*$h)/6.0;
}
}

可以在此处找到使用样条曲线的代码示例 (http://jpgraph.net/download/manuals/chunkhtml/example_src/splineex1.html)

最佳答案

如果您真的想使用样条插值数据值,并且希望最大程度地减少实际样本(数据点)上下的偏差,那么您将需要更多样本。您可以通过样本之间的线性插值生成伪造/人工数据点,然后重新生成样条曲线来获得它们(在这种情况下,您最终会得到更像触摸板绘图的东西)。但是,如果您试图生成一个“平滑”的图表,以最大限度地减少与现实的某种平均偏差,那么您可能需要使用样条曲线以外的其他东西。

关于php - 更新图形上的样条类,使最高峰为标记位置 - JPGraph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46313237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com