All problems
Quadratic Least Squares Fit
Problem
Fit a quadratic y = a + b*x + c*x^2 to data by least squares.
Solve the 3x3 normal equations built from the power sums, using Gaussian elimination with partial pivoting. Return [a, b, c], or None if the normal matrix is singular (fewer than three distinct x values).
Hint
The normal equations for y = a + b*x + c*x^2 form a symmetric 3x3 system of power sums.
Test cases
case 1
in: [[0,1,2],[1,2,5]]
expected: [1.0000000000000009,-8.881784197001252e-15,1.0000000000000044]
case 2
in: [[0,1,2,3],[2,3,4,5]]
expected: [1.9999999999999978,1.0000000000000067,-2.1649348980190643e-15]
case 3
in: [[0,1,2],[0,0,3]]
expected: [3.3306690738754696e-16,-1.5000000000000049,1.5000000000000024]
case 4
in: [[1,1,1],[1,2,4]]
expected: null
case 5
in: [[0,0,1],[1,2,4]]
expected: null
Starter codePython
def quadratic_fit(xs, ys):
# Your code here
pass5 test cases · run your code against them in the editor