php graph an equation
Copy link to clipboard
Copied
Hi All,
I am struggling with how to graph an equation over its full range. How would one code x squared then call it up to be graphed at each 0.1 increment?
The closest that I have gotten is:
$equ = pow($x,2);
$data = array();
for($x = 0; $x <= 5; $x+.1)$data[ ] = array(' ', $equ);
Thank you All.
Gary
Copy link to clipboard
Copied
Is this what you're looking for?
<?php
# PHPlot Example: Line graph, 2 lines
require_once 'phplot.php';
# Generate data for:
$end = 5;
$delta = 0.1;
$data = array();
for ($x = 0; $x <= $end; $x += $delta)
$data[] = array('', $x, pow($x,2));
$plot = new PHPlot(800, 600);
$plot->SetImageBorderType('plain');
$plot->SetPlotType('lines');
$plot->SetDataType('data-data');
$plot->SetDataValues($data);
# Main plot title:
$plot->SetTitle('Line Plot, X Squared');
# Make a legend for the 2 functions:
$plot->SetLegend(array('x^2'));
# Select a plot area and force ticks to nice values:
$plot->SetPlotAreaWorld(0, 0, 5, 25);
# Even though the data labels are empty, with numeric formatting they
# will be output as zeros unless we turn them off:
$plot->SetXDataLabelPos('none');
$plot->SetXTickIncrement(0.5);
$plot->SetXLabelType('data');
$plot->SetPrecisionX(1);
$plot->SetYTickIncrement(2);
$plot->SetYLabelType('data');
$plot->SetPrecisionY(1);
# Draw both grids:
$plot->SetDrawXGrid(True);
$plot->SetDrawYGrid(True);
$plot->DrawGraph();
Copy link to clipboard
Copied
Thank you Servitor.
I am still fixing some issues with the larger equation that I am using(not x^2).
I appreciate your response and answer.
Gary

