Polynomials and Fitting

Matlab can treat a vector as a polynomial. It will assume that the numbers represent the coefficients of the polynomial going from highest-order to lowest order.

>>p = [1 2 2 4 1]

can represent the polynomial x^4 + 2x^3 + 2x^2 + 4x + 1. There are a number of functions that use this representation of polynomials:


gives you the roots of the polynomail represented by the vector p.


gives you the value of the polynomial p when x = 4. Similarly,


gives you the value of the polynomial evaluated at each of the points in the vector. (It returns another vector the same size.)

You can fit polynomials to data sets using this representation and a function called curvefit.

>>[p, fitted] = curvefit(x,y,n)

fits the data in x and y to an nth order polynomial (using 1 gives you a straight line, 2 gives you a quadratic, etc), and plots both the data and the fitted curve for you. It returns p, the polynomial representing the equation of the fitted curve, and fitted, the data points you get from the curvefit for each of the x's in your data set.

You can use polyval and the fitted polynomial p to predict the y value of the data you've fitted for some other x values.

>>ypred = polyval(p,xvalues)