Jump to content

convert formula into generated outputs


marcus

Recommended Posts

<?php
$equation = "x^3 + 2x^2 + 5x - 3";
$solutions = 10;

$new = str_replace(" ",null,$equation);

$find = "/([a-z])\^([\d]+)/";
$repl = '\1<sup>\2</sup>';

$do = preg_replace($find,$repl,$equation);


echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" style=\"border:1px solid #000\">\n";
echo "<tr><td colspan=\"2\" align=\"center\"><b>".$do."</b></td></tr>\n";
echo "<tr><td align=\"center\">X</td><td align=\"center\">Y</td></tr>\n";
for($i=(0-$solutions);$i<=$solutions;$i++){
$xf = "/([a-z])\^([\d]+)/";
$xr = pow('\$1','\$2');
$xf2 = "/([\d]+)([x])/";
$xr2 = '\1*'.$i;
$xf3 = "/([\d]+)\^([\d]+)/";
$xr3 = 'pow(\1,\2)';
$orange = preg_replace($xf2,$xr2,$new);
#$apple = preg_replace($xf,$xr,$orange);
$apple = preg_replace ("/([a-z])\^([\d]+)/","pow($1,$2)",$orange);
$banana = preg_replace($xf3,$xr3,$apple);
$final = str_replace("x",$i,$banana);
echo "<tr><td align=\"center\" style=\"border:1px solid #000\">".$i."</td><td align=\"center\" style=\"border:1px solid #000\">".$final."</td></tr>\n";
}
echo "</table>\n";
?>

 

From the code you can see I'm making the equation's variable replaced with $i, how can I successfully create the exact output of each f(x) ?

Link to comment
https://forums.phpfreaks.com/topic/143071-convert-formula-into-generated-outputs/
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.