ojsimon Posted September 21, 2009 Share Posted September 21, 2009 Hi In my script i am trying to maximise a function, is it possible to do this with php? can you differentiate a function using php is what i am essentially asking? Thanks Quote Link to comment Share on other sites More sharing options...
syed Posted September 21, 2009 Share Posted September 21, 2009 What do you mean maximize a function. Please explain. Quote Link to comment Share on other sites More sharing options...
ojsimon Posted September 21, 2009 Author Share Posted September 21, 2009 using calculus, to differentiate the function then setting it to zero and solving. finding the maximum on the curve esentially. it is also known as optimization. Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted September 21, 2009 Share Posted September 21, 2009 so talking mathematical functions not php functions? Do I have that right? Quote Link to comment Share on other sites More sharing options...
ojsimon Posted September 21, 2009 Author Share Posted September 21, 2009 yes you do Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 21, 2009 Share Posted September 21, 2009 Reminds me of one of the examples in one of my text books. infix 6 ++ --; infix 7 ** //; datatype fexpr = Const of real | X | ++ of fexpr * fexpr | -- of fexpr * fexpr | ** of fexpr * fexpr | // of fexpr * fexpr | Sin of fexpr | Cos of fexpr | Ln of fexpr | Exp of fexpr; fun D(Const _) = Const 0.0 | D X = Const 1.0 | D(Const y ** f) = Const y ** D f | D(f1 ++ f2) = D f1 ++ D f2 | D(f1 -- f2) = D f1 -- D f2 | D(f1 ** f2) = D f1 ** f2 ++ f1 ** D f2 | D(f1 ++ f2) = (D f1 ** f2 -- f1 ** D f2) // (f2 ** f2) | D(Sin f) = Cos f ** D f | D(Cos f) = ((Const ~1.0) ** Sin f) ** D f | D(Exp f) = Exp f ** D f; Even though it's fairly limited it actually goes a long way. You could port that to PHP and extend it to support differentiation of many more functions, but the implementation would be a lot more clumsy because PHP doesn't have the kind of pattern matching that some functional programming languages do. Quote Link to comment Share on other sites More sharing options...
ojsimon Posted September 21, 2009 Author Share Posted September 21, 2009 i have no idea how to port that, i am a bit off a newbie to php. But also my function does not involve anything complex sin etc, it is just a quadratic equation with two variables, anything that i could use for that? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 21, 2009 Share Posted September 21, 2009 If it's always a quadratic equation, you can just hard code the derivative. If you have [imath]y = ax^2+bx+c[/imath] then [imath]\frac{dy}{dx} = 2ax+b[/imath]. So [imath]2ax+b=0 \Rightarrow x = \frac{-b}{2a}[/imath]. Thus the extremum of any quadratic equation is given by [imath]\frac{-b}{2a}[/imath]. Quote Link to comment Share on other sites More sharing options...
ojsimon Posted September 21, 2009 Author Share Posted September 21, 2009 ha, thanks didn't think of that, so obvious now Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted September 21, 2009 Share Posted September 21, 2009 Don't forget that quadratic equations can have 2 solutions, both of them imaginary if the discriminant is negative. Using the PHPExcel function library, the following code will calculate the solution(s) (real or imaginary) for a quadratic equation, given a, b and c: <html> <head> <title>Quadratic Equation Solver</title> </head> <body> <?php /** Error reporting **/ error_reporting(E_ALL); /** Include path **/ set_include_path(get_include_path() . PATH_SEPARATOR . 'PHPExcel_NewCalc/Classes/'); ?> <form action="Quadratic2.php" method="POST"> <table border="0" cellpadding="0" cellspacing="0"> <tr><td>A</td> <td><input name="A" type="text" size="8" value="<?php echo (isset($_POST['A'])) ? htmlentities($_POST['A']) : ''; ?>"></td> </tr> <tr><td>B</td> <td><input name="B" type="text" size="8" value="<?php echo (isset($_POST['B'])) ? htmlentities($_POST['B']) : ''; ?>"></td> </tr> <tr><td>C</td> <td><input name="C" type="text" size="8" value="<?php echo (isset($_POST['C'])) ? htmlentities($_POST['C']) : ''; ?>"></td> </tr> </table> <input name="submit" type="submit" value="calculate"> </form> <?php /** If the user has submitted the form, then we need to execute a calculation **/ if (isset($_POST['submit'])) { /** So we include PHPExcel to perform the calculations **/ include 'PHPExcel/Calculation.php'; /** Calculate and Display the results **/ echo '<hr /><b>Roots:</b><br />'; $callStartTime = microtime(true); $discriminantFormula = '=POWER('.$_POST['B'].',2) - (4 * '.$_POST['A'].' * '.$_POST['C'].')'; $discriminant = PHPExcel_Calculation::getInstance()->calculateFormula($discriminantFormula); $r1Formula = '=IMDIV(IMSUM(-'.$_POST['B'].',IMSQRT('.$discriminant.')),2 * '.$_POST['A'].')'; $r2Formula = '=IF('.$discriminant.'=0,"No second root",IMDIV(IMSUB(-'.$_POST['B'].',IMSQRT('.$discriminant.')),2 * '.$_POST['A'].'))'; echo PHPExcel_Calculation::getInstance()->calculateFormula($r1Formula).'<br />'; echo PHPExcel_Calculation::getInstance()->calculateFormula($r2Formula).'<br />'; $callEndTime = microtime(true); $callTime = $callEndTime - $callStartTime; echo '<hr />Call time for Quadratic Equation Solution was '.sprintf('%.4f',$callTime).' seconds<br /><hr />'; echo ' Peak memory usage: '.(memory_get_peak_usage(true) / 1024 / 1024).' MB<br />'; } ?> </body> <html> Quote Link to comment Share on other sites More sharing options...
ojsimon Posted September 21, 2009 Author Share Posted September 21, 2009 cheers thanks a lot for that very useful Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 21, 2009 Share Posted September 21, 2009 Mark, he is not after the solutions/roots to a quadratic equation, but the extremum. They're not at all the same. Quote Link to comment Share on other sites More sharing options...
ojsimon Posted September 21, 2009 Author Share Posted September 21, 2009 Ah yeah so the first solution is the one i will use anyway. Thanks all for your imput though, still useful for future refernece. Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted September 21, 2009 Share Posted September 21, 2009 Mark, he is not after the solutions/roots to a quadratic equation, but the extremum. They're not at all the same.Sorry! Guess I was jumping to conclusions... I'd only written it over the weekend as a test script, saw the words "quadratic equation" and didn't read any further. Quote Link to comment Share on other sites More sharing options...
ojsimon Posted September 22, 2009 Author Share Posted September 22, 2009 ok so thought this was solved but just realised this solution won't work, as i am forming the equation from two simultaneous equations: u=S*(1+((x/P)-1)*-E) m=(u*(x-C))-F S, P, E, C and F are provided by the user hence i can use the following equation m=S*(1+((x/P)-1)*-E)(x-C)-F I want to find the maximum value for M given all the variables in the equation above are given apart from x this with those values should simplify to a quadratic which you could find a derivative for and optimize. Does anyone know how to do this with php? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 22, 2009 Share Posted September 22, 2009 Well, it's still the same. Substitute u into m. Optionally, simplify. Then find the derivative of m. Solve for dm/dx=0. Quote Link to comment Share on other sites More sharing options...
ojsimon Posted September 22, 2009 Author Share Posted September 22, 2009 well, no i can't simplify because i dont have the user given variables Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 22, 2009 Share Posted September 22, 2009 You can still simplify it. See export of a Maple worksheet. So the extremum is at [math]x = \frac{1}{2} \cdot \frac{P+EC+EP}{P}[/math] [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.