PHP code used to calculate the polynomial derivative. For more information, see
The code is as follows:
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 cocould omit limit t coefficient a, if a = 1 cocould 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, -, 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