Jump to content

Maximise Function


ojsimon

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/175013-maximise-function/#findComment-922449
Share on other sites

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].

Link to comment
https://forums.phpfreaks.com/topic/175013-maximise-function/#findComment-922466
Share on other sites

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>

 

 

Link to comment
https://forums.phpfreaks.com/topic/175013-maximise-function/#findComment-922525
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/175013-maximise-function/#findComment-922533
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/175013-maximise-function/#findComment-922982
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.