1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Quadratic Equation from 3 points

  1. May 18, 2010 #1
    Okay so this seems like a very simple problem to me but I can't figure it out so I definitely need some help. I'm not a mathematician but a programmer and in this case I need to figure out how to get the curve that passes through 3 points. I know this can be done easily with substitution but that tends to requires human logic in order to decide what equations to use in which order. I need another method. I need to be able to do this programmatically.

    I know when I put 3 points into the TI-83 and ask it to do a Quadratic Regression it will give me a function and plot a graph in y = ax2 + bx + c

    That's what I am looking for a method of solving for y = ax2 + bx + c from 3 points hopefully without substitution.

    I hope I am making sense.
    Thanks for any help.
  2. jcsd
  3. May 18, 2010 #2
    You will need to substitute the points somehow in order to find the coefficients. If you have a library available that can handle common matrix operations, this can be made pretty simple, however.
  4. May 18, 2010 #3
    Hmmm care to give us the three points? or is it hypothetical exercise?

    Anyway I do a bit of programing and have a spattering of maths too.

    So I assume some points such as (1,6) (7,8)(10,2) or whatever, and you need to find A B &C? And you want to write a progam to do it?
  5. May 18, 2010 #4
    [itex]ax_1^2+bx_1+c=y_1 [/itex]

    \left[\begin{array}{ccc}a\\b\\c\end{array}\right] =
    \left[\begin{array}{ccc}y_1\\y_2\\y_3\end{array}\right] \left[\begin{array}{ccc}x_1^2&x_1&1\\x_2^2&x_2&1\\x_3^2&x_3&1\end{array}\right] ^{-1}

    Last edited: May 18, 2010
  6. May 18, 2010 #5
    I might have a go using the points I have given and see how far I get, or not, as the case maybe. :rofl:
  7. May 18, 2010 #6
    Do you realize that three points may not necessarily all lie on a parabola? For example, there is no parabola that contains (0, 0), (1, 1) and (2, 2).

    However, there is an infinite number of parabolae that contain any two distinct points. For example, the points (-1, 0) and (1, 0). For this particular case, the axis of symmetry is at 0, so the parabolae are of the form [itex]y = ax^2 - a[/itex] where a is a real number.
  8. May 18, 2010 #7
    OK I just started out with an idea and coubbled togeather a bit of a program.
    It's not completed, just a bit of a framework to get and idea of what I am doing.

    I am trying stick in some values for a b and c and work out the values which give the lowest
    error, then when I have that I will have to try and narrow the range and step based on the results.

    Anyway here is how far I got, no comment, obviously ;)

    Code (Text):

    #include <stdio.h>

        int  argc;
        char *argv[];


        // y = ax2 + bx + c
        // (1,6) (7,8)(10,2)

        float y1,y2,y3,x1,y2,x3, sa,sb,sc, na,nb,nc, step, ea, olodea;

        x1=1;y1=6;x2=7; y2=8;x3=10; y3=2; oldea=100000;


        for(a=-sa; a<sa; a+=step){
            for(b=-sb; a<sb; b+=step){
                for(c=-sc; a<sc; c+=step){

                    e1 = a*36 + b*6 +c -1;
                    e2 = a*64 + b*8 +c -7;
                    e3 = a*4  + b*2 + c-10;
                    et = e1 + e2 + e3;
                    if (ea<oldea) {
                        na=a; nb=b; nc=c; //n=new, so i am saving the nearest guess



        printf("\n a=%f b-%f c=%c"; na,nb,nc);


    Obviously it won't even compile at the moment!!

    Might give you an idea though.
    Last edited: May 18, 2010
  9. May 18, 2010 #8
    So I think after I had got some results I would run the whole thing again using say 30% above and below my range and with and appropiate smaller step.

    Then you just repeat the process keeping your fingers crossed!! :rofl:
    Last edited: May 18, 2010
  10. May 18, 2010 #9
    OK so I have version running, I can hear the fan has kicked in and I can smell burning!!
    250^3 is a big number I think I might reduce that!!
  11. May 18, 2010 #10

    Last edited: May 18, 2010
  12. May 18, 2010 #11
    OK got a bit further:-

    Code (Text):

    #include <stdio.h>
    #include <math.h>
        int  argc;
        char *argv[];
        // y = ax2 + bx + c // (1,6) (7,8)(10,2)
        float y1,y2,y3,x1,x2,x3, sa,sb,sc, na,nb,nc, step, ea, oldea, a, b, c, e1,e2,e3,et;
        x1=1;y1=6;x2=7; y2=8;x3=10; y3=2; oldea=10000000;
        int cc=0;

        for(a=-sa; a<sa; a+=step){
            for(b=-sb; b<sb; b+=step){
                for(c=-sc; c<sc; c+=step){
                    e1 = a*36 + b*6 +c -1;
                    e2 = a*64 + b*8 +c -7;
                    e3 = a*4  + b*2 + c-10;
                    et = e1 + e2 + e3;
                    printf("ea %f et %f oldea %f \n",ea,et,oldea); //fflush(stdout);
                    if (ea< oldea) {
                        na=a; nb=b; nc=c; //n=new, so i am saving the nearest guess
                    printf("sa %f sb %f sc %f \n",a,b,c); fflush(stdout);
            if (cc>10) break;
        printf("\n a=%f b= %f c=%f", na,nb,nc);



    Code (Text):

    sa 9.000000 sb 9.000000 sc 7.000000
    ea 1086.000000 et 1086.000000 oldea 0.000000
    sa 9.000000 sb 9.000000 sc 8.000000
    ea 1089.000000 et 1089.000000 oldea 0.000000
    sa 9.000000 sb 9.000000 sc 9.000000

     a=-1.000000 b= 8.000000 c=-2.000000
    Last edited: May 18, 2010
  13. May 18, 2010 #12
    The matrix of the system is called Vandermonde matrix.
  14. May 18, 2010 #13
    So for first point, 1,6, which in the program is 6,1 due to error

    You have 6 = -1 x 1 + 8x1 -2 = 5, so not far out

    Second 7,8 or 8,7 due to error

    7 = -1 x 64 + 64 -2 = 5 so 2 out

    Third 10,2 but 2,10 in program

    10 = -1 x 4 + 16 + 2 = 14 so 4 out.

    So it seems to work in a rough manner

    Now if I plugged some new values in based on those results and also reduced the step size
    I would hopefully home in on the solution. However if I get the estimates wrong I
    might miss it.

    I will try and modify it later to do that.
  15. May 18, 2010 #14

    I hate matrices!!! :rofl:
  16. May 18, 2010 #15
  17. May 18, 2010 #16
    I need to make some adjustment to my prog, will try that tomorrow and see how I get on.
  18. May 19, 2010 #17
    You could use the Lagrange interpolation formula if you can guarantee that the [itex]x[/itex] coördinates of your points will be distinct.

    That is, if your points are [itex](x_1,y_1),(x_2,y_2),(x_3,y_3)[/itex] with no two of [itex]x_1,x_2,x_3[/itex] the same


    In the case [itex](0,0),(1,1),(2,2)[/itex] mentioned by Unit this will just give you a straight line. If you have duplicate [itex]x[/itex] coördinates there won't be an equation of the form you asked for that goes through the three points unless the corresponding [itex]y[/itex] coördinates are also duplicated, in which case you have only one or two points and you can drop the duplicate(s) and include an arbitrary extra point(s) with distinct [itex]x[/itex] coördinate(s).

    The formula generalizes in an obvious way to a different number of points.

    You're asking for a hyperbola with a vertical axis in general. Is that really what you want? E.g. a unique circle will go thro' three distinct points. Also if you may want to expand it to more points it would be a good idea to think in terms of spline functions. See http://en.wikipedia.org/wiki/Spline_(mathematics [Broken]).
    Last edited by a moderator: May 4, 2017
  19. May 19, 2010 #18
    haha Phizo, that looks like a fun experiment. Not exactly sure it's what I am after... not exactly sure I understand it. You should do some speed tests on it to solve something simple like:
    (2, 0) , (-2, 0) , (0, 4)

    Currently I'm writing the code in AS3; however, in the end it will need to be in C so I'll probably get better speed later. Anyway using matrices as suggested by Xitami I was able to solve this rather easily, that was exactly what I was looking for.

    There's a Matrix library for as3 called KMatrix that worked great. I'm sure there are many in C as well.

    Anyway it took 5ms to solve for a, b, and c from the above numbers. So I'm happy with that.

    I had seen the Lagrange interpolation formula. However, I didn't want to use it because it looks to require a lot more calculations in the long run. I could do a test and see which is faster over a period of time but I'm pretty happy with the Matrix solution at the moment. I haven't tested it in an actual useful situation yet so I'll find out soon.
    Last edited: May 19, 2010
  20. May 19, 2010 #19
    (x1,y1), (x2, y2), (x3, y3)

    [tex]a=\frac{ \left(y_{2} - y_{3}\right) x_{1}
    + \left( y_{1} - y_{2}\right) x_{3}
    + \left(y_{3} - y_{1}\right) x_{2}
    - x_{2}\right) x_{1}^2
    + \left(-x_{3}^2
    + x_{2}^2\right) x_{1}
    + \left(x_{2} x_{3}^2
    - x_{2}^2 x_{3}\right) }[/tex]

    [tex]b= \frac{ \left(-y_{2}
    + y_{3}\right) x_{1}^2
    + \left(-y_{1} x_{3}
    + y_{1} x_{2}\right) x_{1}
    + \left( \left(-y_{1}
    + y_{2}\right) x_{3}^2
    + y_{1} x_{2} x_{3}
    - y_{3} x_{2}^2\right) }{ \left(x_{3}
    - x_{2}\right) x_{1}^2
    + \left(-x_{3}^2
    + x_{2}^2\right) x_{1}
    + \left(x_{2} x_{3}^2
    - x_{2}^2 x_{3}\right) } [/tex]

    [tex] c=\frac{ \left(y_{2} x_{3}
    - y_{3} x_{2}\right) x_{1}^2
    + \left( \left(y_{1}
    - y_{2}\right) x_{3}^2
    - y_{1} x_{2} x_{3}
    + y_{3} x_{2}^2\right) x_{1}}{ \left(x_{3}
    - x_{2}\right) x_{1}^2
    + \left(-x_{3}^2
    + x_{2}^2\right) x_{1}
    + \left(x_{2} x_{3}^2
    - x_{2}^2 x_{3}\right) }[/tex]

    [tex]\mu S[/tex] i think
    Last edited: May 19, 2010
  21. May 19, 2010 #20

    that's perfect!

    I've only tested it once but it worked brilliantly.
    Also takes less than 1ms to complete. That's at least 5x the speed of using the matrix class.

    Thanks a lot :D
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Quadratic Equation from 3 points
  1. Quadratic equations (Replies: 1)

  2. Quadratic Equation (Replies: 3)