Main Content

polyval

R2026b

Polynomial evaluation

Description

y = polyval(coeffs,x) evaluates the polynomial defined by the coefficient vector coeffs at each point in x. The vector coeffs has length n+1 and contains the coefficients of an nth-degree polynomial p in descending powers:

p(x)=coeffs(1)⋅xn+coeffs(2)⋅xn−1+...+coeffs(n)⋅x+coeffs(n+1).

The polynomial coefficients in coeffs can be calculated for different purposes by functions like polyint, polyder, and polyfit, but you can specify any vector for the coefficients.

To evaluate a polynomial in a matrix sense, use polyvalm instead.

example

[y,delta] = polyval(coeffs,x,fitStats) uses the optional output structure fitStats produced by polyfit to generate error estimates. delta is an estimate of the standard error in predicting a future observation at x by coeffs(x).

example

y = polyval(coeffs,x,[],centerScale) or [y,delta] = polyval(coeffs,x,fitStats,centerScale) evaluates the polynomial at the normalized points x−centerScale(1)centerScale(2), where centerScale is produced by polyfit. Use the second syntax to evaluate a fitted polynomial and compute error estimates, with coefficients, fit statistics, and normalization parameters obtained from polyfit.

example

Examples

collapse all

Evaluate the polynomial p(x)=3x2+2x+1 at the points x=5,7,9. The polynomial coefficients can be represented by the vector [3 2 1].

coeffs = [3 2 1];
x = [5 7 9];
y = polyval(coeffs,x)
y = 1×3

    86   162   262

Evaluate the definite integral

I=∫-13(3x4-4x2+10x-25)dx.

Create a vector to represent the polynomial integrand 3x4-4x2+10x-25. The x3 term is absent and thus has a coefficient of 0.

coeffs = [3 0 -4 10 -25];

Use polyint to integrate the polynomial using a constant of integration equal to 0.

q = polyint(coeffs)
q = 1×6

    0.6000         0   -1.3333    5.0000  -25.0000         0

Find the value of the integral by evaluating q at the limits of integration.

a = -1;
b = 3;
I = diff(polyval(q,[a b]))
I = 
49.0667

Fit a linear model to a set of data points and plot the results, including an estimate of a 95% prediction interval.

Create a few vectors of sample data points (x, y). Use polyfit to fit a first-degree polynomial to the data. Specify two outputs to return the coefficients for the linear fit as well as the error estimation structure.

x = 0:100; 
y = -0.3*x + 2*randn(1,101); 
[coeffs,fitStats] = polyfit(x,y,1);

Evaluate the first-degree polynomial at the points in x. Specify the error estimation structure as the third input so that polyval calculates an estimate of the standard error. The standard error estimate is returned in delta.

[yfit,delta] = polyval(coeffs,x,fitStats);

Plot the original data, linear fit, and 95% prediction interval yfit±2Δ.

plot(x,y,"o")
hold on
plot(x,yfit)

xinterval = [x flip(x)];
yinterval = [yfit-2*delta flip(yfit)+2*delta];
interval = fill(xinterval,yinterval,"yellow",FaceAlpha=0.2);

legend("Sample data","First-degree polynomial","95% prediction interval")
hold off

Figure contains an axes object. The axes object contains 3 objects of type line, patch. One or more of the lines displays its values using only markers These objects represent Sample data, First-degree polynomial, 95% prediction interval.

Create sample population data for the years 1750 - 2000 and plot the data points.

x = 1750:25:2000;
y = 1e6*[791 856 978 1050 1262 1544 1650 2532 6122 8170 11560];

Use polyfit to fit a fifth-degree polynomial to the data. You can improve numerical conditioning by normalizing the query vector before fitting a polynomial. This example normalizes the year data and computes a fifth-degree polynomial fit. The function also returns the center and scale used for normalization.

[coeffsNorm,~,centerScale] = polyfit(x,y,5,Normalize=true);

Normalize the query points to the same center and scale, and then evaluate the polynomial by specifying centerScale when calling the polyval function. Then, plot the results.

yfit = polyval(coeffsNorm,x,[],centerScale);

plot(x,y,"o")
hold on
plot(x,yfit)
legend("Sample data","Fifth-degree polynomial")
hold off

Figure contains an axes object. The axes object contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent Sample data, Fifth-degree polynomial.

Input Arguments

collapse all

Polynomial coefficients, specified as a vector. coeffs has length n+1 and contains the polynomial coefficients in descending powers, where the polynomial degree is n. For example, the vector [1 0 1] represents the polynomial x2+1.

The polynomial coefficients in coeffs can be calculated by functions like polyint, polyder, and polyfit, but you can specify any vector for the coefficients. For more information, see Create and Evaluate Polynomials.

Data Types: double | single
Complex Number Support: Yes

Query points, specified as a vector. polyval evaluates the polynomial coeffs at the points in x and returns the corresponding function values in y.

Data Types: double | single
Complex Number Support: Yes

Fit statistics, specified as a structure returned by polyfit. The fitStats structure must contain these fields.

FieldDescription
RTriangular R factor (possibly permuted) from a QR decomposition of the Vandermonde matrix of x
dfDegrees of freedom of the fit
normrEuclidean norm of the residuals

rsquared (since R2024a)

Coefficient of determination (unadjusted R-squared)

Center and scaling values used to normalize the query points in x before evaluating the polynomial, specified as a two-element vector.

If you use polyfit with Normalize=true to compute the polynomial coefficients, then specify centerScale when calling polyval to normalize the query points to the same center and scale.

Output Arguments

collapse all

Function values, returned as a vector of the same size as the query points x. The vector contains the result of evaluating the polynomial coeffs at each point in x.

Standard error for prediction, returned as a vector of the same size as the query points x.

Generally, an interval of y ± Δ corresponds to a roughly 68% prediction interval for future observations of large samples, and y ± 2Δ a roughly 95% prediction interval.

If the coefficients in coeffs are least-squares estimates computed by polyfit, and the errors in the data input to polyfit are independent, normal, and have constant variance, then y ± Δ is at least a 50% prediction interval.

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

Version History

Introduced before R2006a