
356 GPGPU Programming for Games and Science
minimum of F occurs at the vertex. The gradient equation leads to a system
of two linear equations in the unknowns A and B,namely,(0, 0) = ∇F =
2
n
i=0
[(Ax
i
+ B − y
i
](x
i
, 1). The linear system is listed next, where we use
the statistical concept of expected value of a uniformly distributed random
variable U,namely,E[U ]=(
n
i=0
u
i
)/n;
E[X
2
] E[X]
E[X]1
A
B
=
E[XY ]
E[Y ]
(7.3)
The solution to this system provides the coefficients for the least squares fit.
A
B
=
1
E[X
2
] −E[X]
2
E[XY ] −E[X]E[Y ]
E[X
2
]E[Y ] − E[XY ]E[X]
(7.4)
Although the construction is mathematically correct, in practice when us-
ing floating-point num