background image

PHP 代码:PHP 写的求多项式导数的函数代码

PHP 写的求多项式导数的函数代码,需要的朋友可以参考下
代码如下

:

 
<?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