matlab中polyconf,Polynomial confidence intervals

Fit a polynomial to a sample data set, and estimate the 95% prediction intervals and the roots of the fitted polynomial. Plot the data and the estimations, and display the fitted polynomial expression using the helper function polystr, whose code appears at the end of this example.

Generate sample data points (x,y) with a quadratic trend.

rng('default') % For reproducibility

x = -5:5;

y = x.^2 - 20*x - 3 + 5*randn(size(x));

Fit a second degree polynomial to the data by using polyfit.

degree = 2; % Degree of the fit

[p,S] = polyfit(x,y,degree);

Estimate the 95% prediction intervals by using polyconf.

alpha = 0.05; % Significance level

[yfit,delta] = polyconf(p,x,S,'alpha',alpha);

Plot the data, fitted polynomial, and prediction intervals. Display the fitted polynomial expression using the helper function polystr.

plot(x,y,'b+')

hold on

plot(x,yfit,'g-')

plot(x,yfit-delta,'r--',x,yfit+delta,'r--')

legend('Data','Fit','95% Prediction Intervals')

title(['Fit: ',texlabel(polystr(round(p,2)))])

hold off

408cfacff1b3b8191a9e8cc88829c898.png

Find the roots of the polynomial p.

r = roots(p)

r = 2×1

17.5152

-0.1017

Because the roots are real values, you can plot them as well. Estimate the fitted values and prediction intervals for the x interval that includes the roots. Then, plot the roots and the estimations.

if isreal(r)

xmin = min([r(:);x(:)]);

xrange = range([r(:);x(:)]);

xExtended = linspace(xmin - 0.1*xrange, xmin + 1.1*xrange,1000);

[yfitExtended,deltaExtended] = polyconf(p,xExtended,S,'alpha',alpha);

plot(x,y,'b+')

hold on

plot(xExtended,yfitExtended,'g-')

plot(r,zeros(size(r)),'ko')

plot(xExtended,yfitExtended-deltaExtended,'r--')

plot(xExtended,yfitExtended+deltaExtended,'r--')

plot(xExtended,zeros(size(xExtended)),'k-')

legend('Data','Fit','Roots of Fit','95% Prediction Intervals')

title(['Fit: ',texlabel(polystr(round(p,2)))])

axis tight

hold off

end

cefdbc9669bcb92cd2971dec5d82df3a.png

Alternatively, you can use polytool for interactive polynomial fitting.

polytool(x,y,degree,alpha)

6b367a7c1b53e6844fd1550d357dc1ce.png

Helper Function

The polystr.m file defines the polystr helper function.

type polystr.m % Display contents of polystr.m file

function s = polystr(p)

% POLYSTR Converts a vector of polynomial coefficients to a character string.

% S is the string representation of P.

if all(p == 0) % All coefficients are 0.

s = '0';

else

d = length(p) - 1; % Degree of polynomial.

s = []; % Initialize s.

for a = p

if a ~= 0 % Coefficient is nonzero.

if ~isempty(s) % String is not empty.

if a > 0

s = [s ' + ']; % Add next term.

else

s = [s ' - ']; % Subtract next term.

a = -a; % Value to subtract.

end

end

if a ~= 1 || d == 0 % Add coefficient if it is ~=1 or polynomial is constant.

s = [s num2str(a)];

if d > 0 % For nonconstant polynomials, add *.

s = [s '*'];

end

end

if d >= 2 % For terms of degree > 1, add power of x.

s = [s 'x^' int2str(d)];

elseif d == 1 % No power on x term.

s = [s 'x'];

end

end

d = d - 1; % Increment loop: Add term of next lowest degree.

end

end

end

方便大家使用的最小二乘法曲线拟合的Matlab程序-untitled2.fig 非常方便用户使用,直接按提示操作即可;这里我演示一个例子: 请以向量的形式输入x,y. x=[1,2,3,4] y=[3,4,5,6] 通过下面的交互式图形,你可以事先估计一下你要拟合的多项式的阶数,方便下面的计算. polytool()是交互式函数,在图形上方[Degree]框输入阶数,右击左下角的[Export]输出图形 回车打开polytool交互式界面 回车继续进行拟合     输入多项式拟合的阶数   m = 4 Warning: Polynomial is not unique; degree >= number of data points. > In polyfit at 72   In zxecf at 64       输出多项式的各项系数          a = 0.0200000000000001          a = -0.2000000000000008          a = 0.7000000000000022          a = 0.0000000000000000          a = 2.4799999999999973       输出多项式的有关信息 S         R: [4x5 double]        df: 0     normr: 2.3915e-015 Warning: Zero degrees of freedom implies infinite error bounds. > In polyval at 104   In polyconf at 92   In zxecf at 69          观测数据     拟合数据        x         y       yh     1.0000    3.0000    3.0000     2.0000    4.0000    4.0000      3     5     5     4.0000    6.0000    6.0000       剩余平方和            Q = 0.000000         标准误差        Sigma = 0.000000         相关指数           RR = 1.000000 请输入你所需要拟合的数据点,若没有请按回车键结束程序.     输入插值点             x0 = 3     输出插值点拟合函数值   y0 = 5.0000 >> 结果: untitled.fig Figure88.jpg 最小二乘法曲线拟合的程序 untitled2.fig Figure89.jpg 最小二乘法曲线拟合的程序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值