Polynomial evaluation
y = polyval(p,x) y = polyval(p,x,S) [y,delta] = polyval(p,x,S)
a (n+1)-by-1 matrix of doubles, the coefficients of the polynomial, with powers in decreasing order
a nx-by-my matrix of doubles, the points where to evaluate the polynomial
the optional data structure from polyfit
a nx-by-my matrix of doubles, the value of the polynomial at x
If p is a vector of length n+1 whose elements are the coefficients of
a polynomial, then y=polyval(p,x)
is the value of the polynomial,
defined by its coefficients p, evaluated at x.
These coefficients are ordered with powers in decreasing order:
If x is a matrix, the polynomial is evaluated at all points in x.
[y,delta] = polyval(p,x,S)
uses the optional output generated by
polyfit
to generate error estimates, y+/-delta.
p=[3 2 1]; y=polyval(p,[5 7 9]) expected=[86 162 262] // Evaluate for x matrix x = linspace(0,%pi,10); y = sin(x); p = polyfit(x,y,3); // Evaluate at x matrix x = linspace(0,%pi,12); x=matrix(x,3,4) f=polyval(p,x) y=sin(x) // Evaluate 95% confidence bounds, // i.e. +/- 2*delta // Source: [1,2] cdate=(1790:10:1990)'; pop=[ 3.929214 5.308483 7.239881 9.638453 12.860702 17.063353 23.191876 31.443321 38.558371 50.189209 62.979766 76.212168 92.228496 106.02154 123.20262 132.16457 151.3258 179.32317 203.30203 226.5422 248.70987 ]; scf(); plot(cdate,pop,"+") // Calculate fit parameters [p,S] = polyfit(cdate,pop,2); // Evaluate the fit and the prediction error estimate (delta) [pop_fit,delta] = polyval(p,cdate,S); // Plot the data, the fit, and the confidence bounds plot(cdate,pop_fit,"g-") plot(cdate,pop_fit+2*delta,"r:") plot(cdate,pop_fit-2*delta,"r:") // Annotate the plot xlabel("Census Year"); ylabel("Population (millions)"); title("Quadratic Polynomial Fit with Confidence Bounds") | ![]() | ![]() |
[1] http://en.wikipedia.org/wiki/Polynomial_interpolation
[2] http://www.mathworks.fr/fr/help/matlab/data_analysis/programmatic-fitting.html
[3] Numerical Computing with MATLAB, Cleve Moler, 2004