PHP写的求多项式导数的函数代码
程序员文章站
2023-02-26 14:07:43
复制代码 代码如下:
<?php
function getderivativebyformulaandxdata($formula, $x_data){
$xarray = explode("+", $formula);
$derivative = 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);
}
return $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);
}
return $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("max derivative=%f\n",$max_derivative);
foreach ($matchs as $x_match) {
printf(" derivative=%f when x=%f\n",$derivatives[$x_match], $x_match);
}
}
//notice the format of formula: ax^b if b=0 could omit except coefficient a, if a=1 could omit coefficient
$formula = "x^2+2x^1+1";
print "the formula is $formula \n";
//printf("derivative of 2 is %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 "sample x values: $str \n";
getmaxderivativebyformulaandxdatas($formula, $sampledata, $matchs)."\n";
?>
the output will be:
sample x values: -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
max derivative=26.000000
derivative=26.000000 when x=12.000000
复制代码 代码如下:
<?php
function getderivativebyformulaandxdata($formula, $x_data){
$xarray = explode("+", $formula);
$derivative = 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);
}
return $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);
}
return $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("max derivative=%f\n",$max_derivative);
foreach ($matchs as $x_match) {
printf(" derivative=%f when x=%f\n",$derivatives[$x_match], $x_match);
}
}
//notice the format of formula: ax^b if b=0 could omit except coefficient a, if a=1 could omit coefficient
$formula = "x^2+2x^1+1";
print "the formula is $formula \n";
//printf("derivative of 2 is %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 "sample x values: $str \n";
getmaxderivativebyformulaandxdatas($formula, $sampledata, $matchs)."\n";
?>
the output will be:
sample x values: -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
max derivative=26.000000
derivative=26.000000 when x=12.000000
上一篇: php 阴历-农历-转换类代码
下一篇: php数组中删除元素的实现代码