In MATLAB, the polyfit function is used to fit a polynomial to a set of data points using least squares. It returns the coefficients of the polynomial that best fits the data.
✅ Syntax:
[poly, r] = polyfit(x, y, n)
x: Vector of x-values (input data).y: Vector of y-values (output data).n: Degree of the polynomial to fit (default is 1, i.e., a linear fit).poly: A vector of coefficients of the polynomial (from highest degree to lowest).r: A vector of residuals (the difference between the actual and predicted values).