Asked by Alan Mason
on 16 Jun 2011

Hi,

As stated in the title, I am trying to calculate a line-of-best-fit equation (y=mx+b) from a simple x-y dataset, and then to use this equation to calculate r-square.

At the moment I have the following syntax defining the x & y variables:

x1=dat(:,8); y1=dat(:,14);

But I am unsure of where to go from here. I have been searching these forums & MATLAB Help but I have been unable to find a workable solution.

Therefore my 2 questions are: 1. How do I use MATLAB to get a line-of-best-fit equation for this x-y dataset? 2. How do I use this equation (in conjuction with the x-y dataset) to calculate r-square?

Also, I am new to MATLAB so please go easy on me!

Thanks,

Alan

*No products are associated with this question.*

Answer by Sean de Wolski
on 16 Jun 2011

Accepted answer

doc polyfit

and then

doc polyval doc corrcoef

like magic!

Welcome to MATLAB Answers!

Matt Tearle
on 16 Jun 2011

that's polyfit (not polytfit)

Sean de Wolski
on 16 Jun 2011

Thanks, my keyboard has it in for me today!

Answer by Matt Tearle
on 16 Jun 2011

Approach 1: what Sean said. (Note `corrcoef` gives the correlation coefficient r, not the coefficient of determination r^2)

Approach 2: use `regress`, if you have Statistics Toolbox. This allows all sorts of fancy stuff beyond just a fit, as well as post-fit diagnostics.

Approach 3: DIY:

F = [x1.^0 x1]; % make design matrix [1,x] c = F\y1 % get least-squares fit res = y1 - F*c; % calculate residuals r2 = 1 - var(res)/var(y) % calculate R^2

Alan Mason
on 17 Jun 2011

A actually used this answer - the DIY seemed the most logical choice.

Answer by Alan Mason
on 17 Jun 2011

Thank you both for replying. I actually went with Matt's DIY approach (as this showed the logical steps) and it worked great. The rest of my code I'm not so sure about, but that's another story.....

Here's what I ended up with (practically a copy of Matt's DIY code):

%curve fitting model #1 vpd&LE

x1=dat(:,8);

y1=dat(:,14);

% rsquare_vpd

% make design matrix [1,x]

F1 = [x1.^0 x1];

% get least-squares fit

c1 = F1\y1;

% calculate residuals

res1 = y1 - F1*c1;

% calculate R^2

rsquare_vpd = 1 - var(res1)/var(y1);

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments