That's essentially what was requested in the first part of a problem my daughter asked me some explanation about the other day. Incidentally I had worked on a similar problem involving four points that morning and had used my HP-50g to solve the corresponding system of linear equations. But calculators are not allowed in her class, so I had to find another method. Her teacher's solution used Gauss elimination which doesn't involve many calculations in a 3x3 system, but a mistake done in the beginning leads to completely wrong answers (like mine, when doing it by hand that way). I don't like this kind of problem being posed to students, I mean problems involving number crunching when a calculator is not available. On the other hand, according to her math textbook, this problem had been asked on the admittance exam of an important university here (UNICAMP), so her teacher is not wrong by occasionally asking a bit of hand calculations from his students. However, I decided to find a faster method, perhaps an easier-to-remember formula, considering the time per question is usually short during this kind of exams.
Here is my approach:
Given three points
P1 = (x1, y2)For instance, using the data points from the textbook,
P2 = (x2, y2)
P3 = (x3, y3)
P1 = (1, 2)find the equation of the corresponding parabola,
P2 = (2, 2.7)
P3 = (3, 3.2)
y = ax2 + bx + cThe solution consists basically in solving a system of linear equations:
y1 = ax12 + bx1 + cOr, in matrix form,y2 = ax22 + bx2 + c
y3 = ax32 + bx3 + c
/ \ / \ / \Rather than solving it numerically, it should be better to solve it symbolically. Thus the resulting formula may be used again to solve similar problems, avoiding unnecessary and heavy matrix calculations.
| x12 x1 1 | | a | | y1 |
| | | | | |
| x22 x2 1 | | b | = | y2 |
| | | | | |
| x32 x3 1 | | c | | y3 |
\ / \ / \ /
The determinant of the square matrix above, a Vandermonde Matrix, is very easy to compute, requiring only three subtractions and three multiplications. Also not difficult to calculate are the other matrices associated to the Cramer's Rule for this system. So, that method appears to be more adequate to be used here:
| y1 x1 1 | | x12 y1 1 | | x12 x1 y1 |
| | | | | |
| y2 x2 1 | | x22 y2 1 | | x22 x2 y2 |
| | | | | |
| y3 x3 1 | | x32 y3 1 | | x32 x3 y3 |
a = ------------------- ; b = ------------------- ; c = -------------------
| x12 x1 1 | | x12 x1 1 | | x12 x1 1 |
| | | | | |
| x22 x2 1 | | x22 x2 1 | | x22 x2 1 |
| | | | | |
| x32 x3 1 | | x32 x3 1 | | x32 x3 1 |
y1(x2 - x3) + y2(x3 - x1) + y3(x1 - x2)
a = ---------------------------------------
(x1 - x3)(x2 - x3)(x1 - x2)
y1(x32 - x22) + y2(x12 - x32) + y3(x22 - x12)
b = ---------------------------------------------------
(x1 - x3)(x2 - x3)(x1 - x2)
y1(x22x3 - x32x2) + y2(x32x1 - x12x3) + y3(x12x2 - x22x1)
b = ---------------------------------------------------------------
(x1 - x3)(x2 - x3)(x1 - x2)
or, more mnemonically,
y1 y2 y3Now, solving for the original numerical example:
a = ------------------ + ------------------ + ------------------
(x1 - x2)(x1 - x3) (x2 - x1)(x2 - x3) (x3 - x1)(x3 - x2)
y1(x2 + x3) y2(x3 + x1) y3(x1 + x2)
b = - ------------------ - ------------------ - ------------------
(x1 - x2)(x1 - x3) (x2 - x1)(x2 - x3) (x3 - x1)(x3 - x2)
y1x2x3 y1x3x1 y3x1x2
c = ------------------ + ------------------ + ------------------
(x1 - x2)(x1 - x3) (x2 - x1)(x2 - x3) (x3 - x1)(x3 - x2)
x1 = 1 y1 = 2
x2 = 2 y2 = 2.7
x3 = 3 y3 = 3.22 2.7 3.2 2 2.7 3.2 .
a = --------------- + --------------- + --------------- = --- + ----- + ----- . . a = -0.1
(1 - 2)*(1 - 3) (2 - 1)*(2 - 3) (3 - 1)*(3 - 2) 2 -1 2
2*(2 + 3) 2.7*(3 + 1) 3.2*(1 + 2) -10 -10.8 -9.6 .
b = - --------------- - --------------- - --------------- = --- + ----- + ----- . . b = 1.0
(1 - 2)*(1 - 3) (2 - 1)*(2 - 3) (3 - 1)*(3 - 2) 2 -1 2
2*2*3 2.7*3*1 3.2*1*2 12 8.1 6.4 .
c = --------------- + --------------- + --------------- = --- + ----- + ----- . . c = 1.1
(1 - 2)*(1 - 3) (2 - 1)*(2 - 3) (3 - 1)*(3 - 2) 2 -1 2
.
. . y = -0.1*x2 + x + 1.1
Here is a QBASIC program that handles the cases from n = 2 through 4, that is, linear, quadratic, cubic and biquadratic equations, where n is the number of data points:
DEFDBL A-H, O-Z: DEFINT I-N
DO UNTIL N > 1 AND N < 5
CLS
INPUT "N: ", N
LOOP
DIM A(N), X(N), Y(N)
FOR I = 1 TO N
A(I) = 0
PRINT "X("; I; "), "; "Y("; I; ") ";
INPUT "", X(I), Y(I)
NEXT I
FOR I = 1 TO N
P = 1
FOR J = 1 TO N - 1
P = P * (X(I) - X(1 + (J + I - 1) MOD N))
NEXT J
T = Y(I) / P
A(1) = A(1) + T
M = N - 1
S% = 1
FOR K = 2 TO N
S% = -S%
IF K = N THEN M = 1
S = 0
FOR J = 1 TO M
P = 1
FOR L = 1 TO K - 1
P = P * X(1 + (I + (L - 2 + J) MOD (N - 1)) MOD N)
NEXT L
S = S + P
NEXT J
A(K) = A(K) + S% * T * S
NEXT K
NEXT I
FOR I = 1 TO N
PRINT "P("; I; ") = ("; X(I); ","; Y(I); ")"
NEXT I
FOR I = 1 TO N
PRINT "A("; N - I; ") = "; A(I)
NEXT I
END
Examples:
---------------------------------------
N: 3X( 1 ), Y( 1 ) 1,2
X( 2 ), Y( 2 ) 2,2.7
X( 3 ), Y( 3 ) 3,3.2
P( 1 ) = ( 1 , 2 )
P( 2 ) = ( 2 , 2.7 )
P( 3 ) = ( 3 , 3.2 )A( 2 ) = -.1000000000000001
A( 1 ) = 1
A( 0 ) = 1.1---------------------------------------
---------------------------------------
N: 4
X( 1 ), Y( 1 ) 1,5
X( 2 ), Y( 2 ) 2,41
X( 3 ), Y( 3 ) 3,127
X( 4 ), Y( 4 ) 4,269
P( 1 ) = ( 1 , 5 )
P( 2 ) = ( 2 , 41 )
P( 3 ) = ( 3 , 127 )
P( 4 ) = ( 4 , 269 )A( 3 ) = 1.000000000000007
A( 2 ) = 18.99999999999999
A( 1 ) = -28.00000000000005
A( 0 ) = 12.99999999999999
---------------------------------------