polyvalEvaluating Polynomial and Standard Error of Prediction for Polynomial Regression
In the following, we use
where
where
Let
To improve numerical stability, we obtain the inverse
where
where
This formula is used by polyfit to computer delta.
y = polyval(p, x)It evaluates a given polynomial at each element of x. Coefficients of the polynomial are given by p.
x should be a numeric array.
p should be a vector containing coefficents of the polynomial.
p(1) corresponds to the highest order term.p(end) corresponds to the zeroth order term.p should be non-empty.The output has the same size as x.
y = polyval(p, x, [], mu)x and p are as described in y = polyval(p, x) above.mu is a vector of two numbers, where mu(0) and mu(1) are, respectively, the mean and standard deviation of x.y gives values of the polynomial p evaluated at (x - mu(0)) / mu(1).y has the same size as x.[y, delta] = polyval(p, x, S)x and p are as described in y = polyval(p, x) above.
y contains values of the polynomial evaluated at x.
delta contains the standard errors of the predictions y.
delta(i) is the standard error of the prediction delta is computed using the last formula shown before.delta has the same size as x.S should be a structure obtained by polyfit with fields R, normr and df.
[y, delta] = polyval(p, x, S, mu)mu is a vector of two numbers, where mu(0) and mu(1) are, respectively, the mean and standard deviation of x.y and delta are computed using the transformed (x - mu(0)) / mu(1).[y, delta] = polyval(p, x, S) above for the description of the outputs y and delta.r = [2 3 4 5];
% Creating a polynomial with roots r
p = poly(r);
x = linspace(1.8,5.2);
% Evaluating the polynomial
y = polyval(p,x);
plot(x,y,'-o','MarkerSize',2)
clear
% x and y are data points.
x=linspace(0,2);
y=x.^2+normrnd(0,0.2,[1 length(x)]);
% Get polynomial of degree 2 that fits the data.
[p,S]=polyfit(x,y,2)
scatter(x,y)
hold('on')
% Plot the polynomial and standard errors.
xx=0:0.01:2;
[yy,delta]=polyval(p,xx,S);
plot(xx,yy)
plot(xx,yy+2*delta,'k--')
plot(xx,yy-2*delta,'k--')
hold('off')