开发者

Calculating a parabola: What am I doing wrong?

I was following this thread and copied the code in my project. Playing around with it turns out that it seems not to be very precise.

Recall the formula: y = ax^2 + bx +c

Since the first given point I have is at 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开发者_JAVA百科 b yields:

b = y/x - ax - cx

Now setting both equations equal to each other so b falls out

y2/x2 - ax2 - cx2 = y3/x3 - ax3 - cx3

Now solving for a gives me:

a = ( x3*(y2 - c) + x2*(y3 - c) ) / ( x2*x3*(x2 - x3) )

(is that correct?!)

And then using again b = y2/x2 - ax2 - cx2 to find b. However so far I haven't found the correct a and b coeffs. What am I doing wrong?

Edit

Ok I figured out, but had to use a CAS because I don't know how to invert symbolic matrices by hand. (Gauss algo doesn't seem to work)

Writing it down in Matrix form:

| 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 with M^(-1)

|a|
|b| = M^(-1)*Y
|c|

Then I got out of maple:

a = (-y1 * x2 + y1 * x3 - y2 * x3 + y3 * x2) / x2 / x3 / (-x2 + x3)

Guess I did a stupid mistake somewhere above.

Which gives me the same result as the formula in the thread quoted above.


Your problem is that you have three unknowns (the coefficients a, b, and c) and only one equation that I can see: y = y1 when x = 0; this gives c = y1, as you said.

Without more information, all you can do is tell how b is related to a. That's it. There isn't one solution, there are many solutions.

If you're telling me that you have two other points (x2, y2) and (x3, y3), then you should substitute all of them into the equation and solve. Start with:

Calculating a parabola: What am I doing wrong?


(source: equationsheet.com)

Now substitute the three points (x1, y1), (x2, y2), and (x3, y3):

Calculating a parabola: What am I doing wrong?


(source: equationsheet.com)

This is the matrix equation that you need to invert. 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 link gives you and multiply the right hand side vector by it to solve for your three coefficients.

It's a pretty easy thing to code if you note that

det = (x2 x1^2-x3 x1^2-x2^2 x1+x3^2 x1-x2 x3^2+x2^2 x3)

Divide all the entries in the matrix by this value. The numerators are pretty simple:

Calculating a parabola: What am I doing wrong?


(source: equationsheet.com)

Divide this by the determinant and you've got your inverse.

If you have more points than three you need to do a least squares fit. Do the same trick of substituting all the points you have (x1, y1)...(xn, yn). You'll have more equations than unknowns. Multiply both sides by the transpose of the nx3 matrix and solve. Voila - you'll have the set of coefficients that minimize the squares of errors between the points and the function values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜