addFitLine

版本 1.34.0.0 (2.3 KB) 作者: Rob Campbell
A convenience function to superimpose a polynomial fit to already plotted data
751.0 次下载
更新时间 2022/7/27

查看许可证

** What is it?
This convenience function allows you to quickly add a fit line to data after they have been plotted. e.g.
>> plot(myData)
>> out=addFitLine;
Add fit line returns the fit parameters and handles to the plot objects.
** Details
The stats toolbox is required for the 'linear' and 'quadratic' options only.
function out=addFitLine(poolFits,modelType,lineprops,ax)
Purpose
Add one or more fit lines to some or all data in axes "ax" or,
without ax specified, to the current axes.
Inputs
*poolFits [optional 0 by default] -
if 1 we fit a single line to all data series on the current
axes.
*modelType ['linear' by default] - If 'linear' it fits a straight line
using the regress function from the stats toolbox. If 'quadratic' it
fits a second-order polynomial using regress. The output from regress is
returned. If modelType is an integer, the function fit a polynomial of
this order using polyfit, which is not part of the stats toolbox. The
output of polyfit is then returned.
*lineprops [optional 'b-' by default]
*ax [optional - gca by default]
Outputs
out - the fit parameters and plot handles
Examples
clf
%Fit two seperate lines
subplot(1,3,1), hold on, x=-5:0.1:5;
y=1+1*x+randn(size(x))*2; plot(x,y,'.k')
y=2-4*x+randn(size(x))*2; plot(x,y,'.r')
H=addFitLine
%Fit one line to two sets of data
subplot(1,3,2), hold on
x=-5:0.2:0; y=0.3*x+randn(size(x))*2; plot(x,y,'.k')
x=0:0.2:5; y=0.3*x+randn(size(x))*2; plot(x,y,'.r')
H=addFitLine(1)
%Fit one quadratic
subplot(1,3,3)
x=-5:0.1:5;
y=2+0.3*x+0.5*x.^2+randn(size(x))*2;
plot(x,y,'.k')
addFitLine([],'quadratic')
OR:
addFitLine([],2)
Rob Campbell - December 2009

引用格式

Rob Campbell (2024). addFitLine (https://www.mathworks.com/matlabcentral/fileexchange/26323-addfitline), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2022a
兼容 R2011b 到 R2022a 的版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Linear and Nonlinear Regression 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.34.0.0

Polynomial fit plot now produces a line that is the same color as the data points.

1.33.0.0

Fix bug in polyfit section: x data were being sorted but not y data.
Tidy code.

1.31.0.0

clarify what this function does

1.3.0.0

Correct indexing bug

1.2.0.0

the polynomial fit was not return plot handles

1.1.0.0

Improved error checking: removes text and patch elements before acquiring plot data.
Added ability to fit higher order polynomials using polyfit and polyval.

1.0.0.0