Just like two points uniquely defines a line, I realized that three points uniquely defines a parabola. It's fun to determine the equation from the three points and sometimes I'd use this method on homeworks/tests instead of memorizing other formulas, because I found this much more intuitive..
read moreSample input:
-1.0
-3.0
-4.0
5.0
-2.0x^2 +5.0x +1.0
import java.awt.Point;
import java.awt.geom.Point2D;
public class determineParabolaFromThreePoints {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//make sure the points are in descending x order
Point one = new Point (0,1);
Point two = new Point (3,-2);
Point three = new Point (1,4);
//calculate two slopes
double yslopea = (two.getY()-one.getY())/(two.getX()-one.getX());
System.out.println(yslopea);
double yslopeb = (three.getY()-two.getY())/(three.getX()-two.getX());
System.out.println(yslopeb);
//calculate the x values of those slopes, we can do this because
//we know concavity is a constant for the entire domain
double xslopea = (one.getX()+two.getX())/2;
double xslopeb = (two.getX()+three.getX())/2;
//calculate a constant of concavity
double concavity = (yslopeb-yslopea)/(xslopeb-xslopea);
System.out.println(concavity);
//now that we know concavity, we calculate the slope at point one
double slopeAtOne = yslopea-((xslopea-one.getX())*concavity);
System.out.println(slopeAtOne);
//because we know the equation is of the form ax^2 + bx + c
//we know its derivative is 2ax + b
//we know its second derivative is 2a, so we set 2a = concavity and solve for a
double a = (concavity/2);
double b=(-2*one.getX()*a)+slopeAtOne;
double c=((-a*(one.getX()*one.getX()))-(b*one.getX()))+one.getY();
if(c==0&&b==0){
System.out.println(a+"x^2");
}
if(c==0&&b!=0){
System.out.println(a+"x^2 +"+b+"x");
}
if(c!=0&&b==0){
System.out.println(a+"x^2 +"+c);
}
if(c!=0&&b!=0){
System.out.println(a+"x^2 +"+b+"x +"+c);
}
}
}