Code for finding polynomial derivatives written in PHP

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.