버전 (2.34 KB) 작성자: Rob Campbell
A convenience function to superimpose a polynomial fit to already plotted data

다운로드 수: 747

업데이트 날짜: 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)
Add one or more fit lines to some or all data in axes "ax" or,
without ax specified, to the current axes.
*poolFits [optional 0 by default] -
if 1 we fit a single line to all data series on the current
*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]
out - the fit parameters and plot handles
%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')
%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')
%Fit one quadratic
Rob Campbell - December 2009

인용 양식

Rob Campbell (2023). 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!
버전 게시됨 릴리스 정보

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

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

clarify what this function does

Correct indexing bug

the polynomial fit was not return plot handles

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