如何将误差线添加到 MatLab 的 cftool 生成的曲线拟合散点图中?



我正在尝试获取使用 MatLab 的 cftool 创建的图形,并向 y 数据添加垂直误差线。我一直在尝试更改创建图形的自动生成的代码。

我尝试使用错误栏函数,但是当我这样做时,它会覆盖给定的情节。也就是说,它创建了一个线图(点不应该连接(,并且曲线拟合也不存在。我检查了绘图函数的文档,但似乎没有向数据添加误差线的选项。

function [fitresult, gof] = TungstenFit(Bin,Count,CountError)
[xData, yData] = prepareCurveData( Bin, Count );
% Set up fittype and options.
ft = fittype( 'b+m*x+A1*exp(-(x-u1)^2/(2*s1^2))+A2*exp(-(x-u2)^2/(2*s2^2))+A3*exp(-(x-u3)^2/(2*s3^2))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.Lower = [0 0 0 0 -Inf -Inf -Inf -Inf 100 150 150];
opts.StartPoint = [850 500 50 0 -10 10 10 10 140 160 185];
opts.Upper = [Inf Inf Inf 10 Inf Inf Inf Inf Inf Inf Inf];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'W3LsFit' );
h = plot( fitresult, xData, yData );
legend( h, 'Tungsten Bin Counts', 'W3LsFit', 'Location', 'NorthWest' );
% Label axes
xlabel Bin
ylabel Tungsten Bin Count
grid on

此代码创建一个散点图,其中包含数据并绘制曲线拟合函数。但是,它当前对 CountError 数据没有任何作用。

我是 MatLab 的新手(我不得不在这项作业中教给自己(,所以任何帮助或提示将不胜感激。谢谢。

我有一个可行的解决方案。对于后代,这是我在创建情节和图例函数之间添加的代码:

hold on
e = errorbar(xData,yData,CountError,'LineStyle','none');

第一行导致绘图不被覆盖,"LineStyle","none"参数导致误差线函数添加误差线而不在数据点之间画线。

最新更新