Jump to content

Newbie Help: Graphing Quad. Equations


Kardafol

Recommended Posts

I'm wondering if anyone has any idea how I should start making a XY graph, then plot a quadratic equation on it.

So far, I have the following (Solves the equation aslong as it's in a 'ax^2+bx+c=0' form)

class Quadratic
{
//varaibles
public $pat = '/^(-?[0-9.]*)x\^2([-+][0-9.]*)x([+-][0-9.]+)=0$/i';
public $a = 0;
public $b = 0;
public $c = 0;

function setABCByEquation($expr)
{
	if (preg_match($this->pat, $expr, $ans)) {
		$this->a = $ans[1];
		$this->b = $ans[2];
		$this->c = $ans[3];
		return $this->checkVars();
	} else
		return 0;
}
function setABCByValues($va = 1, $vb = 0, $vc = 1)
{
	$this->a = $va;
	$this->b = $vb;
	$this->c = $vc;
	return $this->checkVars();
}
function checkVars()
{
	if ($this->a == '-')
		$this->a = -1;
	else if ($this->a == 0)
		$this->a = 1;
	if ($this->b == '-') 
		$this->b = -1;	
	else if ($this->b == '' || $this->b == '+') 
		$this->b = 1;
	$this->b = ereg_replace("[^-0-9.]", "", $this->b);
	$this->c = ereg_replace("[^-0-9.]", "", $this->c);
	return 1;
}
function solve()
{
	if ($this->a == 0 || $this->c == 0) return 'err';
	$pre = sqrt(abs(($this->b*$this->b) - (4*$this->a*$this->c)));
	$ans[0] = (-$this->b + $pre) / (2 * $this->a);
	$ans[1] = (-$this->b - $pre) / (2 * $this->a);
	return $ans;
}
// Helper functions (clear();, getEquation()
}
/* Syntax:
$equation = new Quadratic();
$equation->setABCByEquation('x^2+4x+8=0');
$ans = $equation->Solve();
echo 'x = '.$ans[0].', '.$ans[1];
//Assuming no errors were found.
*/

As the equation is quadratic, the line should be smooth, and curve eather up or down.

I've never used the image functions with PHP, and I'm not quite sure where to start. It would be nice if someone could help me get started on this.  ;D

Link to comment
https://forums.phpfreaks.com/topic/36837-newbie-help-graphing-quad-equations/
Share on other sites

there is a person who made an awesome calculator, cant remember his/her name right now,that i think did this. is studied quadratics in maths and they would be hard to put on a graph in php. unless there is a seperate function for it.

 

if you go here, there is a list of all the image functions a tad way down the page:

http://au2.php.net/manual/en/ref.image.php

The first phpfreaks competition focused on creating a php based "graphing calculator".  I recommending checking out some of the submissions to that competition:

 

http://www.phpfreaks.com/forums/index.php/board,64.0.html

 

Specifically this page:

http://www.phpfreaks.com/forums/index.php/topic,115293.15.html

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.