Computing a parabola: what am I doing wrong?
I followed this thread and copied the code in my project. Playing with him turns out that this is not very accurate.
Recall the formula: y = ax ^ 2 + bx + c
Since the first given point i has the value x1 = 0, we already have c = y1. We just need to find a and b. Using:
y2 = ax2 ^ 2 + bx2 + c y3 = ax3 ^ 2 + bx3 + c
Solving the equations for b gives:
b = y / x - ax - cx
Now set both equations equal to each other so that b falls out
y2 / x2 - ax2 - cx2 = y3 / x3 - ax3 - cx3
Now the solution for a gives me:
a = (x3 * (y2 - c) + x2 * (y3 - c)) / (x2 * x3 * (x2 - x3))
(it is right?!)
And then again using b = y2 / x2 - ax2 - cx2 to find b. However, until now I have not found the correct a and b coefficients. What am I doing wrong?
Edit
Ok, I got it, but I had to use CAS because I don't know how to manually invert symbolic matrices. (Gauss ego doesn't seem to work)
Writing it down as a matrix:
| 0 0 1 | |a|
| x2^2 x2 1 | * |b| = Y
| x3^2 x3 1 | |c|
Let's call the matrix M and multiply from the left by M ^ (- 1)
|a|
|b| = M^(-1)*Y
|c|
Then I came out of the maple tree:
a = (-y1 * x2 + y1 * x3 - y2 * x3 + y3 * x2) / x2 / x3 / (-x2 + x3)
Guess I made a stupid mistake somewhere above.
This gives me the same output as the formula in the above thread.
a source to share
Your problem is that you have three unknowns (coefficients a, b and c) and only one equation that I see: y = y1 when x = 0; this gives c = y1 as you said.
Without further information, all you can do is say how b relates to a. It. There is no one solution, there are many solutions.
If you tell me that you have two other points (x2, y2) and (x3, y3), then you must plug them all into the equation and solve. Start with:
(source: equation sheet .com )
Now replace the three points (x1, y1), (x2, y2) and (x3, y3):
(source: equation sheet .com )
This is a matrix equation to be inverted. You can use Cramer's rule or LU decomposition. Another possibility is Wolfram Alpha :
http://www.wolframalpha.com/input/?i=inverse{{x1*x1,+x1,+1},+{x2*x2,+x2,+1},+{x3*x3,+x3,+1}}
Take the inverse that the reference gives and multiply the right vector by it to compute three coefficients.
It's pretty easy to code if you notice that
det = (x2 x1 ^ 2-x3 x1 ^ 2-x2 ^ 2 x1 + x3 ^ 2 x1-x2 x3 ^ 2 + x2 ^ 2 x3)
Divide all entries in the matrix by this value. The numerators are pretty simple:
(source: equation sheet .com )
Divide that by a determinant and you get the opposite.
If you have more points than three, you need to make the least number of squares. Do the same trick, substituting all the points you have (x1, y1) ... (xn, yn). You will have more equations than unknowns. We multiply both sides by the transpose of the nx3 matrix and solve. Voila - you will have a set of coefficients that minimize the squared errors between points and function values.
a source to share