PHP寫的求多項(xiàng)式導(dǎo)數(shù)的函數(shù)代碼
更新時(shí)間:2012年07月04日 19:11:06 作者:
PHP寫的求多項(xiàng)式導(dǎo)數(shù)的函數(shù)代碼,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
<?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
相關(guān)文章
php 刪除記錄同時(shí)刪除圖片文件的實(shí)現(xiàn)代碼
php 刪除記錄同時(shí)刪除圖片文件的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-05-05
php實(shí)現(xiàn)遍歷目錄并刪除指定文件中指定內(nèi)容
這篇文章主要介紹了php實(shí)現(xiàn)遍歷目錄并刪除指定文件中指定內(nèi)容,本文實(shí)現(xiàn)的工具可以用來刪除服務(wù)器上的一句話木馬,需要的朋友可以參考下2015-01-01
php中刪除字符串中最先出現(xiàn)某個(gè)字符的實(shí)現(xiàn)代碼
刪除字符串中最先出現(xiàn)某個(gè)字,就是通過explode的靈活用法,需要的朋友可以參考下2013-02-02
php獲取ip的三個(gè)屬性區(qū)別介紹(HTTP_X_FORWARDED_FOR,HTTP_VIA,REMOTE_ADDR)
獲取用戶IP地址的三個(gè)屬性的區(qū)別HTTP_X_FORWARDED_FOR,HTTP_VIA,REMOTE_ADDR,需要的朋友可以參考下2012-09-09
php站內(nèi)搜索關(guān)鍵詞變亮的實(shí)現(xiàn)方法
這篇文章主要介紹了php站內(nèi)搜索關(guān)鍵詞變亮的實(shí)現(xiàn)方法,以實(shí)例形式較為詳細(xì)的分析了數(shù)據(jù)庫表的創(chuàng)建及針對(duì)數(shù)據(jù)庫的搜索關(guān)鍵字高亮的實(shí)現(xiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
php訪問數(shù)組最后一個(gè)元素的函數(shù)end()用法
這篇文章主要介紹了php訪問數(shù)組最后一個(gè)元素的函數(shù)end()用法,實(shí)例分析了php中end函數(shù)訪問數(shù)組元素的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03

