Numerical Method
(2) Write a MATLAB program that implements the least-square regression for the single-variable polynomial model f(x) = a0 + a1*x + a2*x^2 + ... + ag*x^g given a set of data [x] and [y] of any lengths. Your MATLAB code submission should have the following two M files:
(2.1) me2016_PolyRegression.m
This file is a general function so that a functional call of [A, err] = me2016_PolyRegression(X, Y, d) should return the polynomial coefficients A = [a0, a1, a2, ..., ag] and the sum square error err = e, where the input parameters include X as an input column vector of [x], Y as an input column vector of [y], and d is the degree of the polynomial. The implementation must be based on the matrix form as in Question (1), instead of nested loops with element-by-element operations. To solve the linear equation CA = B, you should use the single MATLAB command of A = C\B.
(2.2) test_me2016_PolyRegression.m
This file tests the above regression function with the following data points:
x: 1.6 2.0 2.5 3.2 4.0 4.5
y: 2 8 14 15 8 2
Fit polynomial models with degrees of 1, 2, 3, 4, 5, and 6 from the above data. In other words, your code should at least contain the following lines:
[A1, err1] = me2016_PolyRegression(X, Y, 1)
[A2, err2] = me2016_PolyRegression(X, Y, 2)
[A3, err3] = me2016_PolyRegression(X, Y, 3)
[A4, err4] = me2016_PolyRegression(X, Y, 4)
[A5, err5] = me2016_PolyRegression(X, Y, 5)
[A6, err6] = me2016_PolyRegression(X, Y, 6)