Algebraic Equations - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Algebraic Equations

Posts: 4Member
Hi,

I am in high school and have some knowlege of PASCAL, we are studying parabolas in my math class at the moment and I was wondering if someone could write me up a program that would determine the equation of a parabola with any given three points. Or rather how could I go about translating the process( by which the equation of a parabola is determined by three points) listed below into PASCAL?

As far as I have tried I couldn't figure out how make PASCAL solve my problems through the substitution method.

All help is appreaciated. Thank You
-john32

==========================================================================
The equation for a parabola is y = ax^2 + bx + c

So if you have three points, you can calculate the equation of the
parabola. But you need three points.

Let's say the points are (-4,15) (2,3) and (1,-5). Notice that these
points have an x value and a y value, and the equation has a place
for x and a place for y.

So for each point you could plug in the the x and y values.

(-4,15): y = ax^2 + bx + c <=== x=-4, y=15
15 = a(-4)^2 + b(-4) + c
15 = 16a - 4b + c

(2,3): y = ax^2 + bx + c <=== x=2, y=3
3 = a(2)^2 + b(2) + c
3 = 4a + 2b + c

(1,-5): y = ax^2 + bx + c <=== x=1, y=-5
-5 = a(1)^2 + b(1) + c
-5 = a + b + c

Now we have these three equations:

15 = 16a - 4b + c
3 = 4a + 2b + c
-5 = a + b + c

There are three equations and three unknowns, so if we can solve these
equations for a, b, and c, we will be able to turn y = ax^2 + bx + c
into the equation for the graph.

Step 1: pick an equation at random and solve for one unknown

-5 = a + b + c
c = -5 - a - b

Step 2: plug that into both of the other equations

15 = 16a - 4b + c <=== c = -5 - a - b
15 = 16a - 4b + (-5 - a - b)
15 = 15a - 5b - 5
20 = 15a - 5b

3 = 4a + 2b + c <=== c = -5 - a - b
3 = 4a + 2b + (-5 - a - b)
3 = 3a + b - 5
8 = 3a + b

Now we have only two equations with two unknowns:

20 = 15a - 5b
8 = 3a + b

Step 3: pick an equation at random and solve for one unknown

8 = 3a + b
b = 8 - 3a

Step 4: plug that into the other equation

20 = 15a - 5b <=== b = 8 - 3a
20 = 15a - 5(8 - 3a)
20 = 15a - 40 + 15a
60 = 15a + 15a
60 = 30a
60/30 = a
a = 2

Step 4: plug that into the answer for step 3

b = 8 - 3a <=== a=2
b = 8 - 3(2)
b = 2

Step 5: plug both of those into the answer from Step 1

c = -5 - a - b <=== a=2, b=2
c = -5 - (2) - (2)
c = -9

The equation for a parabola is y = ax^2 + bx + c and we have values
for a, b, and c, so:

y = ax^2 + bx + c <=== a=2, b=2, c=-9
y = 2x^2 + 2x - 9

And that's the equation of our parabola.
========================================================================

• Posts: 156Member
No, we can't write you're program. We don't do homework here. Besides, you have already solved the problem, you just heve to write it in code. I will give you some pointers.
1) If you heve 3 points, jou have three equations. You put the equations in 3 arrays or one 2 dinenstional like so:
[CODE]var equation: array[0..3,0..4]of real; [/CODE]
2) Solve the matrix
• Posts: 4Member
You guys are mean... I cried for hours!
Heres my parabola solver. As far as i know this can only be compiled using SCAR which can only be found at www.kaitnieks.com
======================================================
program Parabola;

function sqr(x: Extended): Extended;
begin
Result:= x * x;
end;

procedure FindCurveEquation2(x1, y1, x2, y2, x3, y3: Extended; var a, b, c: Extended);
var
begin
xdet:= ((sqr(x1)) * x2) + ((sqr(x3)) * x1) + ((sqr(x2)) * x3) - ((sqr(x1)) * x3) - ((sqr(x2)) * x1) - ((sqr(x3)) * x2);
adet:= (y1 * x2) + (y3 * x1) + (y2 * x3) - (y1 * x3) - (y2 * x1) - (y3 * x2);
bdet:= ((x1 * x1) * y2) + ((x3 * x3) * y1) + ((x2 * x2) * y3) - ((x1 * x1) * y3) - ((x2 * x2) * y1) - ((x3 * x3) * y2);
cdet:= ((x1 * x1) * x2 * y3) + ((x3 * x3) * x1 * y2) + ((x2 * x2) * x3 * y1) - ((x1 * x1) * x3 * y2) - ((x2 * x3) * x1 * y3) - ((x3 * x3) * x2 * y1);

b:= bdet / xdet;
c:= cdet / xdet;
end;

const scale = 30;

var
a, b, c: Extended;
x, y: Extended;
dx, dy: Integer;
dic: TCanvas;
x1, y1, x2, y2, x3, y3: Extended;
begin
x1:= -1.0;
y1:= 1.0;
x2:= 0;
y2:= 0;
x3:= 1.0;
y3:= 1.0;
FindCurveEquation2(x1, y1, x2, y2, x3, y3, a, b, c);
Writeln('a=' + FloatToStr(a));
Writeln('b=' + FloatToStr(b));
Writeln('c=' + FloatToStr(c));
DisplayDebugImgWindow(500, 500);
dic:= GetDebugCanvas;
dic.Brush.Color:= clBlack;
dic.Brush.Style:= bsSolid;
dic.Pen.Color:= clBlack;
dic.Rectangle(0, 0, 500, 500);
//display axes
dic.Pen.Color:= clWhite;
dic.MoveTo(0, 250);
dic.LineTo(500, 250);
dic.MoveTo(250, 0);
dic.LineTo(250, 500);
//display graph
x:= -500/scale;
repeat
dx:= Round(x * scale) + 250;
y:= a * x * x + b * x + c;
dy:= Round(-y * scale) + 250;
dic.Pixels[dx, dy]:= clGreen;
x:= x + 0.5 / scale;
until(x > 500/scale);
//display the starting points
dic.Pen.Color:= clYellow;
dic.Ellipse(Round(x1 * scale + 250)-2, Round(-y1 * scale) + 250-2, Round(x1 * scale + 250)+2, Round(-y1 * scale) + 250+2);
dic.Ellipse(Round(x2 * scale + 250)-2, Round(-y2 * scale) + 250-2, Round(x2 * scale + 250)+2, Round(-y2 * scale) + 250+2);
dic.Ellipse(Round(x3 * scale + 250)-2, Round(-y3 * scale) + 250-2, Round(x3 * scale + 250)+2, Round(-y3 * scale) + 250+2);
end.
=======================================================================
• Posts: 757Member
Mean? I think you should thank bpajk. Anyone else here would've said the same. We are not here to do homework for people, we are here to help people figure things out after they've put at least some effort in. It looks like you got it, although I don't think anyone is going to check your code over for you since it's all messed. At least take the time to read the Information page:
http://www.programmersheaven.com/c/msgboard/boardinfo.asp?BoardID=16&Setting=A9999F0001

When you post code, use the [ code ] [ /code ] markers. It makes it cleaner and easier for everyone and people will be more prone to help you out.

Phat Nat

: You guys are mean... I cried for hours!
: Heres my parabola solver. As far as i know this can only be compiled using SCAR which can only be found at www.kaitnieks.com
: ======================================================
: program Parabola;
:
: function sqr(x: Extended): Extended;
: begin
: Result:= x * x;
: end;
:
: procedure FindCurveEquation2(x1, y1, x2, y2, x3, y3: Extended; var a, b, c: Extended);
: var
: xdet, adet, bdet, cdet: Extended;
: begin
: xdet:= ((sqr(x1)) * x2) + ((sqr(x3)) * x1) + ((sqr(x2)) * x3) - ((sqr(x1)) * x3) - ((sqr(x2)) * x1) - ((sqr(x3)) * x2);
: adet:= (y1 * x2) + (y3 * x1) + (y2 * x3) - (y1 * x3) - (y2 * x1) - (y3 * x2);
: bdet:= ((x1 * x1) * y2) + ((x3 * x3) * y1) + ((x2 * x2) * y3) - ((x1 * x1) * y3) - ((x2 * x2) * y1) - ((x3 * x3) * y2);
: cdet:= ((x1 * x1) * x2 * y3) + ((x3 * x3) * x1 * y2) + ((x2 * x2) * x3 * y1) - ((x1 * x1) * x3 * y2) - ((x2 * x3) * x1 * y3) - ((x3 * x3) * x2 * y1);
:
: b:= bdet / xdet;
: c:= cdet / xdet;
: end;
:
: const scale = 30;
:
: var
: a, b, c: Extended;
: x, y: Extended;
: dx, dy: Integer;
: dic: TCanvas;
: x1, y1, x2, y2, x3, y3: Extended;
: begin
: x1:= -1.0;
: y1:= 1.0;
: x2:= 0;
: y2:= 0;
: x3:= 1.0;
: y3:= 1.0;
: FindCurveEquation2(x1, y1, x2, y2, x3, y3, a, b, c);
: Writeln('a=' + FloatToStr(a));
: Writeln('b=' + FloatToStr(b));
: Writeln('c=' + FloatToStr(c));
: DisplayDebugImgWindow(500, 500);
: dic:= GetDebugCanvas;
: dic.Brush.Color:= clBlack;
: dic.Brush.Style:= bsSolid;
: dic.Pen.Color:= clBlack;
: dic.Rectangle(0, 0, 500, 500);
: //display axes
: dic.Pen.Color:= clWhite;
: dic.MoveTo(0, 250);
: dic.LineTo(500, 250);
: dic.MoveTo(250, 0);
: dic.LineTo(250, 500);
: //display graph
: x:= -500/scale;
: repeat
: dx:= Round(x * scale) + 250;
: y:= a * x * x + b * x + c;
: dy:= Round(-y * scale) + 250;
: dic.Pixels[dx, dy]:= clGreen;
: x:= x + 0.5 / scale;
: until(x > 500/scale);
: //display the starting points
: dic.Pen.Color:= clYellow;
: dic.Ellipse(Round(x1 * scale + 250)-2, Round(-y1 * scale) + 250-2, Round(x1 * scale + 250)+2, Round(-y1 * scale) + 250+2);
: dic.Ellipse(Round(x2 * scale + 250)-2, Round(-y2 * scale) + 250-2, Round(x2 * scale + 250)+2, Round(-y2 * scale) + 250+2);
: dic.Ellipse(Round(x3 * scale + 250)-2, Round(-y3 * scale) + 250-2, Round(x3 * scale + 250)+2, Round(-y3 * scale) + 250+2);
: end.
: =======================================================================
:

• Posts: 156Member
It is good to know how you're code works, so I will gude you trough.

We have three points:
A(X1,Y1)
B(X2,Y2)
C(X3,Y3)

We put the points into the equation Y=aX^2+bX+c and we get:
aX1^2+bX1+c=Y1
aX2^2+bX2+c=y2
aX3^2+bX3+c=y3

Then we put the equations into the matrix:
[CODE]
a b c
[X1^2 X1 1 | Y1]
|X2^2 X2 1 | y2|
[X3^2 X3 1 | Y3]
[/CODE]

In you're code you use Cramer's rule to get the parameters a, b and c. This is done like so:
[CODE]
[Y1 X1 1]
det|Y2 X2 1|
[Y3 X3 1]
a = ------------------
[X1^2 X1 1]
det|X2^2 X2 1|
[X3^2 X3 1]

[X1^2 Y1 1]
det|X2^2 Y2 1|
[X3^2 Y3 1]
b = ------------------
[X1^2 X1 1]
det|X2^2 X2 1|
[X3^2 X3 1]

[X1^2 X1 Y1]
det|X2^2 X2 Y2|
[X3^2 X3 Y3]
c = ------------------
[X1^2 X1 1]
det|X2^2 X2 1|
[X3^2 X3 1]
[/CODE]

This means the folowing:
[CODE]
(Y1*X2+Y2*X3+Y3*X1-Y3*X2-Y1*X3-Y2*X1)
a=-----------------------------------------------
(X1^2*X2+X2^2*X3+X3^2*X1-X3^2*X2-X1^2*X3-X2^2*X1)

(X1^2*Y2+X2^2*Y3+X3^2*Y1-X3^2*Y2-X1^2*Y3-X2^2Y1)
b=-----------------------------------------------
(X1^2*X2+X2^2*X3+X3^2*X1-X3^2*X2-X1^2*X3-X2^2*X1)

(X1^2*X2*Y3+X2^2*X3*Y1+X3^2*X1*Y2-X3^2*X2*Y1-X1^2*X3*Y2-X1^2*X1*Y3)
c=-------------------------------------------------------------------
(X1^2*X2+X2^2*X3+X3^2*X1-X3^2*X2-X1^2*X3-X2^2*X1)
[/CODE]

There is a nother way to do this. I will give you an example:
We have the function y=x^2+1, so the points are:
T(0,1)
T(1,2)
T(-1,2)

And the functions for the points are:
a*0^2+b*0+c=1
a*1^2+b*1+c=2
a*(-1)^2+b*(-1)+c=2

We put them into the matrix and manipulate the matrix like so:
[CODE]
a b c Y
[0 0 1 | 1]
|1 1 1 | 2|=
[1 -1 1 | 2]

[1 1 1 | 2]
|0 0 1 | 1|=
[1 -1 1 | 2]

[1 1 1 | 2]
|1 -1 1 | 2|=
[0 0 1 | 1]

[1 1 1 | 2]
|0 -2 0 | 0|=
[0 0 1 | 1]

[1 1 1 | 2]
|0 1 0 | 0|
[0 0 1 | 1]
[/CODE]

This means:
1c=1
c=1

1b=0
b=0

1a+1b+1c=2
a+0+1=2
a=1

And this is the result.

As you may see the matrix isn't a movie, but it is a two dimensional array:
[CODE]
var x: array[0..3, 0..4] of real;
[/CODE]