Do what Excel does in curve fitting in Matlab

7 views (last 30 days)
Samuel
Samuel on 25 Apr 2012
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

Answers (3)

Teja Muppirala
Teja Muppirala on 25 Apr 2012
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 Comments
Samuel
Samuel on 25 Apr 2012
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 on 26 Apr 2012
Hello Samuel,
The LinearModel class was just introduced recently in the R2012a release.
You may have to use the other methods mentioned instead.

Sign in to comment.


Wayne King
Wayne King on 25 Apr 2012
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 Comment
Samuel
Samuel on 25 Apr 2012
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.

Sign in to comment.


Daniel Shub
Daniel Shub on 25 Apr 2012
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 ...

Community Treasure Hunt

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

Start Hunting!