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:
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!
No products are associated with this question.
doc polyval doc corrcoef
Welcome to MATLAB Answers!
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
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
% 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);
Play games and win prizes!Learn more