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

问题描述 投票:0回答:1

我正在尝试使用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的新手(我必须自己教这个任务,所以任何帮助或提示将不胜感激。谢谢。

matlab matlab-figure
1个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.