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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.