MATLAB:绘制符合预测的数据

MATLAB: Plot data fit with prediction

我正在尝试绘制随连接长度增加的强度。在下面的示例中,创建了与我期望的相似的随机数据,并对其进行了拟合。 问题是我想确定每个长度(每个 x 值)的预测水平,而不是整个数据集的预测水平。从图中可以看出,低 x 值的结果比高 x 值的结果分散得多。

任何人都可以给我提示如何创建这种类型的图表(其中预测线远离拟合)?

%Generate random data
 xVec = 0:0.001:1;
 Distr = makedist('Normal','mu',10,'sigma',1);
 for i=1:length(xVec)
    yVec(i) = sqrt(xVec(i))*random(Distr);
 end

%Create fit and prediction interval
 FitVec = fit(xVec',yVec','poly4');
 pRvecConf = predint(FitVec,xVec,0.95,'observation','off');

%Plot
 plot(FitVec,xVec,yVec) 
 hold on
 plot(xVec,pRvecConf,'m--')
 legend('Data','Fitted curve','Confidence','Location','se')
 xlabel('Length')
 ylabel('Strength')

请参阅以下示例图:

由于 yVec 是通过使用 sqrt(xVec) 对随机分布进行加权生成的,因此您实际上将每个 x 值的随机变量的方差更改为 xVec(sqrt(xVec) 的平方)。您可以做的是通过使用 xVec 对原始置信区间进行加权来重新计算置信区间。以下是一些基于您的代码,

   %Generate random data
 xVec = 0:0.001:1;
 Distr = makedist('Normal','mu',10,'sigma',1);
 for i=1:length(xVec)
    yVec(i) = sqrt(xVec(i))*random(Distr);
 end

%Create fit and confidence interval
 FitVec = fit(xVec',yVec','poly4')
 pRvecConf = predint(FitVec,xVec,0.95,'observation','off');

 %get the fitting values
 fitY=feval(FitVec,xVec);
 %multiply the confidence interval with sqrt(xVec(i)).^2 
 ci=(fitY-pRvecConf(:,1)).*xVec';
 %get the weighted confidence interval
 Conf_new=[fitY-ci,fitY+ci];

 %Plot
 plot(FitVec,xVec,yVec) 
 hold on
 plot(xVec,Conf_new,'m--')
 legend('Data','Fitted curve','Confidence','Location','se')
 xlabel('Length')
 ylabel('Strength')

结果应该是这样的: