I've been wanting to see a graphical representation of functional output for sometime. I've been using software like Cabri and so forth, but there's a big hassle in first programming a discontinuous function into a graph and then transferring it into code. I decided I could write up a quick php script to generate a basic SVG representation (doesn't have to be overly accurate) that would give me the ability to see the graphical output of linear, quadratic, sigmoid and discontinuous functions.
However, it seems to mess up with anything non-linear; even $x2 gets messed up, producing results such as:
<line x1="1.93359375" y1="3" x2="1.953125" y2="3" />
<line x1="1.953125" y1="3" x2="1.97265625" y2="3" />
<line x1="1.97265625" y1="3" x2="1.9921875" y2="3" />
<line x1="1.9921875" y1="3" x2="2.01171875" y2="0" />
<line x1="2.01171875" y1="0" x2="2.03125" y2="0" />
<line x1="2.03125" y1="0" x2="2.05078125" y2="0" />
<line x1="2.05078125" y1="0" x2="2.0703125" y2="0" />
<line x1="2.0703125" y1="0" x2="2.08984375" y2="0" />
The code is fairly short and I've included it below:
<?php header("Content-type: image/svg+xml");
echo '<?xml version="1.0" standalone="no"?>';
?><?php //This line is to stop compiler from highlighting php as html because of xml end tag
error_reporting('E_NONE');
define('SIZE',512);
$yLim=chkLim('y');
$xLim=chkLim('x');
$res=($xLim[2]/SIZE);
function chkLim($dim){
$i=$dim.'min'; $a=$dim.'max';
if ($_GET[$a]>$_GET[$i]) {
$lim[0]=$_GET[$i]; $lim[1]=$_GET[$a];
} else { $lim[0]=0; $lim[1]=10;
} $lim[2]=$lim[1]-$lim[0];
$lim[3]=1/($lim[2]/SIZE);
return $lim;
}
function sig($x){
//return (10*2)/(1+log(2)^(0-(5*($x-512)/1024)));
//$step=log(2)^(0-.015*(x-512));
//return (1-$step)/(1+$step)*6+10;
return $x^2;
//return $x*4;
}
?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<?php
echo '<svg width="'.SIZE.'px" height="'.SIZE.'px" version="1.1" xmlns="http://www.w3.org/2000/svg">';
echo chr(13).'<text x="300" y="300" style="font-size:14">'.$xLim[0]." ".$xLim[1]." ".$xLim[2]." ".$xLim[3].'</text>';
echo '<g transform="scale(1,-1) translate(0,-'.SIZE.')">';
echo '<g style="stroke:#000000;stroke-width:'.(1/($xLim[3])*2).'" transform="scale('.$xLim[3].','.$yLim[3].')">';
for($x=0;$x<$xLim[2];$x+=$res){
$x1=$x;
$x2=$x+$res;
$y1=sig($x1);
$y2=sig($x2);
if (($y1>$yLim[1]) && ($y2>$yLim[1]) || ($y1<$yLim[0]) && ($y2<$yLim[0]))
$extLim++;
else
echo chr(13).'<line x1="'.$x1.'" y1="'.$y1.'" x2="'.$x2.'" y2="'.$y2.'" />';
}
?>
</g>
<line x1="0" x2="<?php echo SIZE; ?>" y1="0" y2="0" style="stroke:#666666;stroke-width:4" />
<line x1="0" x2="0" y1="0" y2="<?php echo SIZE; ?>" style="stroke:#666666;stroke-width:4" />
</g></svg>