代码如下:
function getDerivativeByFormulaAndXDATA($formula, $x_data){
$xArray =explode("+", $formula);
$微分 = 0;
foreach ($xArray as $x_record) {
$tmpArray =explode("x^", $x_record);
if(count($tmpArray) == 2){
$coefficient = $tmpArray[0]==""?1:$tmpArray[0];
$exp = $tmpArray[1];
}
//constant
else {
$coefficient = $tmpArray[0];
$exp = 0;
}
$Derivative += $coefficient*$exp*pow($x_data,$exp-1);
}
$Derivative を返します。
}
function getValueByFormulaAndXDATA($formula, $x_data){
$xArray =explode("+", $formula);
$y_data = 0;
foreach ($xArray as $x_record) {
$tmpArray =explode("x^", $x_record);
if(count($tmpArray) == 2){
$coefficient = $tmpArray[0]==""?1:$tmpArray[0];
$exp = $tmpArray[1];
}
//constant
else {
$coefficient = $tmpArray[0];
$exp = 0;
}
$y_data += $coefficient*pow($x_data,$exp);
}
$y_data を返します;
}
function getMaxDerivativeByFormulaAndXDATAS($formula, $x_datas, &$matchs){
$derivatives = array();
$max_derivative = 0;
foreach ($x_datas as $x_data) {
$derivative = getDerivativeByFormulaAndXDATA($formula, $x_data);
$derivatives[$x_data] = $derivative;
$max_derivative = $max_derivative>=abs($derivative)?$max_derivative:abs($derivative);
//printf("x=%f, Derivative=%f n",$x_data, $derivative);
}
$matchs = array();
foreach ($derivatives as $x_data=>$derivative) {
if(abs($derivative) == $max_derivative){
$matchs[] = $x_data;
}
}
printf("最大導関数=%fn",$max_derivative);
foreach ($matchs as $x_match) {
printf("derivative=%f when x=%fn",$derivatives[$x_match], $x_match);
}
}
//数式の形式に注目してください: ax^b if b=0 は係数 a 以外を省略でき、if a=1 は係数を省略できます
$formula = "x^2+2x^1+1";
print "数式は $formula n";
//printf("2 の導関数は %f n",getDerivativeByFormulaAndXDATA($formula, 3.2));
//print getValueByFormulaAndXDATA($formula, 3.2)."n";
$sampleData = array(-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4, 5、6、7、8、9、10、11、12);
foreach ($sampleData as $x_data) {
$str.=$x_data.", ";
}
print "サンプル x 値: $str n";
getMaxDerivativeByFormulaAndXDATAS($formula, $sampleData, $matchs)."n";
?>
出力は次のようになります:
サンプル x 値: -12、-11、-10、-9、-8、-7、-6、-5、-4、-3、-2、-1、 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
最大導関数=26.000000
導関数=26.000000 (x=12.000000の場合)
以上はPHP写本による求多项式导数の関数数代コードの内容、より多くの関連文章请关注PHP中文网(www.php.cn)!