Do what Excel does in curve fitting in Matlab

조회 수: 6 (최근 30일)
Samuel
Samuel 2012년 4월 25일
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

답변 (3개)

Teja Muppirala
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
Samuel
Samuel 2012년 4월 25일
Thanks for the help. I actually have the statistics toolbox when i check the versions, but strangely when I adapt and run the code as you wrote, it gives me an error saying
Undefined variable "LinearModel" or class "LinearModel.fit".
and typing help LinearModel gives an error
LinearModel not found.
i have matlab r2011b for mac and windows. what might be the problem?
thanks!
Teja Muppirala
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
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);
  댓글 수: 1
Samuel
Samuel 2012년 4월 25일
Hello Wayne, thanks for the help!
I actually tried implementing your method, but when I execute it, I get an error that states matrix dimensions must agree. It seems that the X matrix should be only 1 column in order for the regress function to work? Any clarification will be helpful! Thanks.

댓글을 달려면 로그인하십시오.


Daniel Shub
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 ...

카테고리

Help CenterFile Exchange에서 Multiple Linear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by