Analysis of PHP-implemented equation solving examples and php equation solving examples
This example describes how to solve the equation implemented by PHP. We will share this with you for your reference. The details are as follows:
I. Requirements
1. returns an average X, in turn, to obtain the average X of the three numbers X1, X2, X3, the maximum value and the minimum value of the difference is less than 0.4 (X1-X3 is to retain the number of 1 decimal)
2. The three numbers X1, X2, and X3 represent the three groups. The following formula is met: X1 = [(m1-m2)/(m1-m0)] * 100 (@ 1 );
The following conditions are imposed on the three numbers of m0, m1, and m2:
1) 48 <m0 <51
2) 0.45 <m1-m1 <0.55
3) m1, m2, and m3 are all 4 decimal places.
II. Implementation
As needed, I thought of using two functions for processing.
1) One is the known average, and the createX () function is used to obtain the three numbers that meet the condition of the average ()
2) If X is known, calculate the three decimals that meet the formula (@ 1) and boundary conditions: m0, m1, m2.
The following is the specific code implementation. For more information, see
<? Php // run echo "start running \ n"; run (); echo "End running \ n"; function run () {/* {*/$ data = array (40.9, 40.5, 44.3, 47.8, 48.5, 42.1, 46.2); $ res = array (); foreach ($ dataas $ key) {/* {*/echo "processing {$ key} \ n"; $ resX = createX ($ key ); foreach ($ resXas $ keyX) {$ keyStr = (string) $ key; $ keyXStr = (string) $ keyX; $ res [$ keyStr] [$ keyXStr] = createParams4M ($ keyX) ;}/ * }}*/error_log (print_r ($ res, true ). "\ n", 3, '/tmp/result. log'); v Ar_dump (99999, $ res); exit; return $ res;}/*} * // 1. generate the mean function createX ($ ave) {/* {*/$ sum = 3 * ($ ave * 1000); $ x1 = $ x2 = $ x3 = 0; $ rand4X1X2 = rand (0,300); // $ x2> $ x1> $ x3; for ($ I = 0; $ I <$ ave * 1000 + 550; $ I ++) {$ x2 = $ I; $ x1 = $ x2-$ rand4X1X2; $ x3 = $ sum-($ x1 + $ x2 ); $ positive = $ x1> 0 & $ x2> 0 & $ x3> 0; $ flag = ($ x2-$ x3 <400 & $ x2-$ x3> 200); if ($ positive & $ flag) {// echo "captures \ n "; $ RoundX1 = round ($ x1/1000, 1); $ roundX2 = round ($ x2/1000, 1); $ roundX3 = round ($ x3/1000, 1); $ res = array ($ roundX1, $ roundX2, $ roundX3); $ flag = $ roundX1! = $ RoundX2 & $ roundX3! = $ RoundX2 & $ roundX3! = $ RoundX1; if ($ flag) {// echo "caught \ n"; return $ res ;}} echo "Fail not caught \ n "; returnarray ($ x1, $ x2, $ x3);}/* }}* // 2. generate the mean function createParams4M ($ aveX) {/* {*/$ begin = 48000; $ end = 51000; $ m0 = $ m1 = $ m2 = 0; $ rand4M1M2 = rand (450,550); $ m0 = rand ($ begin, $ end); $ m1 = $ m0 + $ rand4M1M2; $ m2 = $ m1-($ m1-$ m0) * $ aveX/100); // echo "Capture \ n"; returnarray (round ($ m0/1000, 4), round ($ m1/1000, 4), round ($ m 2/1000, 4);}/* }}} */?>
Iii. problems and shortcomings
1. [array key values cannot be the same] the original three values of the obtained average may be the same, but the keys in the array cannot be the same, so we processed them.
At the same time, the key value is converted to a string.
2. [decimal processing] also involves four decimal places and loop traversal. If the output meets the condition is inconvenient, multiply the number by 1000, and then divide it by 1000 to restore.