To evaluate a polynomial in a matrix sense, use
y = polyval(
use the optional output
mu produced by
polyfit to center and scale the
std(x). Using these values,
x at zero and scales it to
have unit standard deviation,
This centering and scaling transformation improves the numerical properties of the polynomial.
Evaluate Polynomial at Several Points
Evaluate the polynomial at the points . The polynomial coefficients can be represented by the vector
[3 2 1].
p = [3 2 1]; x = [5 7 9]; y = polyval(p,x)
y = 1×3 86 162 262
Integrate Quartic Polynomial
Evaluate the definite integral
Create a vector to represent the polynomial integrand . The term is absent and thus has a coefficient of 0.
p = [3 0 -4 10 -25];
polyint to integrate the polynomial using a constant of integration equal to
q = polyint(p)
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
Linear Regression with Error Estimate
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 = 1:100; y = -0.3*x + 2*randn(1,100); [p,S] = polyfit(x,y,1);
Evaluate the first-degree polynomial fit in
p 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
[y_fit,delta] = polyval(p,x,S);
Plot the original data, linear fit, and 95% prediction interval .
plot(x,y,'bo') hold on plot(x,y_fit,'r-') plot(x,y_fit+2*delta,'m--',x,y_fit-2*delta,'m--') title('Linear Fit of Data with 95% Prediction Interval') legend('Data','Linear Fit','95% Prediction Interval')
Use Centering and Scaling to Improve Numerical Properties
Create a table of population data for the years 1750 - 2000 and plot the data points.
year = (1750:25:2000)'; pop = 1e6*[791 856 978 1050 1262 1544 1650 2532 6122 8170 11560]'; T = table(year, pop)
T=11×2 table year pop ____ _________ 1750 7.91e+08 1775 8.56e+08 1800 9.78e+08 1825 1.05e+09 1850 1.262e+09 1875 1.544e+09 1900 1.65e+09 1925 2.532e+09 1950 6.122e+09 1975 8.17e+09 2000 1.156e+10
polyfit with three outputs to fit a 5th-degree polynomial using centering and scaling, which improves the numerical properties of the problem.
polyfit centers the data in
year at 0 and scales it to have a standard deviation of 1, which avoids an ill-conditioned Vandermonde matrix in the fit calculation.
[p,~,mu] = polyfit(T.year, T.pop, 5);
polyval with four inputs to evaluate
p with the scaled years,
(year-mu(1))/mu(2). Plot the results against the original years.
f = polyval(p,year,,mu); hold on plot(year,f) hold off
S — Error estimation structure
Error estimation structure. This structure is an optional output from
[p,S] = polyfit(x,y,n) that can be used to obtain
S contains the following fields:
|Triangular factor from a QR decomposition of the
Vandermonde matrix of |
|Degrees of freedom|
|Norm of the residuals|
If the data in
y is random, then an
estimate of the covariance matrix of
Rinv is the inverse of
mu — Centering and scaling values
Centering and scaling values, specified as a two-element vector. This
vector is an optional output from
polyfit(x,y,n) that is used to improve the numerical
properties of fitting and evaluating the polynomial
std(x). These values are
used to center the query points in
x at zero with unit
mu to evaluate
p at the
(x - mu(1))/mu(2).
delta — Standard error for prediction
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
p are least-squares estimates
polyfit, and the errors in the data input
polyfit are independent, normal, and have constant
variance, then y ± Δ is at least a 50% prediction interval.
Calculate with arrays that have more rows than fit in memory.
Usage notes and limitations:
is a tall array, then it must be a column vector.
For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Introduced before R2006a