Do what Excel does in curve fitting in Matlab
조회 수: 6 (최근 30일)
이전 댓글 표시
Hello all, thanks in advance for your help!
I know that Matlab is loaded with all sorts of curve fitting software that can utilize a whole bunch of different functions in order to give the best fitting possible.
However, I just have some simple data that I wish to fit a linear standard line to- make the line go through 0 and show the r squared value, and show the equation. I also wish to show this for my plot that has more than one "function" plotted (this is because I noticed that using the tools method in the plot interface only allows one curve to be fit, and doesn't show the rsquared value).
If anybody can list out what I can add to my code to implement these simple aspects, that will be great.
Thanks again. sam
댓글 수: 0
답변 (3개)
Teja Muppirala
2012년 4월 25일
Wayne gives a good way to do it, but in the latest version of the Statistics Toolbox, there is some great functionality that makes this stuff even easier::
x = 1:100;
y = 2*x+ 4*randn(size(x));
F = LinearModel.fit(x,y,'linear','intercept',false)
plot(F)
F.Rsquared
See "help LinearModel" for more information
댓글 수: 2
Teja Muppirala
2012년 4월 26일
Hello Samuel,
The LinearModel class was just introduced recently in the R2012a release.
You may have to use the other methods mentioned instead.
Wayne King
2012년 4월 25일
Are you really sure you want to force the line to go through zero?
Do you have the Statistics Toolbox? If so, use regress()
x = 1:100;
y = 2*x+ 4*randn(size(x));
X = ones(length(y),2);
X(:,2) = x';
[beta,bint,r,rint,stats] = regress(y',X);
Fitted line is yhat = beta(1)+beta(2)*x. Rsquared is
stats(1)
plot(x,y,'k*'); hold on;
yhat = beta(1)+beta(2)*x;
plot(x,yhat,'r','linewidth',2);
Daniel Shub
2012년 4월 25일
While it doesn't do everything you want I would suggest looking at the source for lsline
type lsline
It takes care of plotting multiple lines. It makes use of polyfit
doc polyfit
which provides the r^2 value you want
Adding the equation for a line that goes through the intercept seems a bit odd to me ...
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!