x = 1:100;
y = -0.3*x + 2*randn(1,100);
[p,S] = polyfit(x,y,1)
p = 1×2
-0.3142 0.9614
S = struct with fields:
R: [2×2 double]
df: 98
normr: 22.7673
rsquared: 0.9407
x の各点で p の 1 次多項式近似を評価します。polyval の 3 番目の入力として誤差推定の構造体を指定し、標準誤差の推定値を計算します。標準誤差の推定値は delta に返されます。
[y_fit,delta] = polyval(p,x,S);
元のデータ、線形近似、および 95% の予測区間 をプロットします。
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')