首页 > 解决方案 > 如何将误差线添加到 MatLab 的 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 很陌生(我必须自学这个任务,)所以任何帮助或提示将不胜感激。谢谢。

标签: matlabmatlab-figure

解决方案


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

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

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


推荐阅读