如何将误差线添加到 MatLab cftool 生成的曲线拟合散点图中?
How can error bars be added to curve fitted scatter plots generated by MatLab's cftool?
我正在尝试获取使用 MatLab 的 cftool 创建的图形并将垂直误差线添加到 y 数据。我一直在尝试更改创建图形的自动生成代码。
我试过使用 errorbar 函数,但是当我这样做时它会覆盖给定的图。也就是说,它创建了一个线图(点不应该连接)并且曲线拟合也不存在。我查看了绘图函数的文档,但似乎没有向数据添加误差线的选项。
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' 参数导致 errorbar 函数添加错误条而不在数据点之间画一条线。
我正在尝试获取使用 MatLab 的 cftool 创建的图形并将垂直误差线添加到 y 数据。我一直在尝试更改创建图形的自动生成代码。
我试过使用 errorbar 函数,但是当我这样做时它会覆盖给定的图。也就是说,它创建了一个线图(点不应该连接)并且曲线拟合也不存在。我查看了绘图函数的文档,但似乎没有向数据添加误差线的选项。
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' 参数导致 errorbar 函数添加错误条而不在数据点之间画一条线。