Search This Blog

Saturday, September 12, 2015

linear regression matlab script

function [p,Rsq] = LinearRegression (Xin,Yin)
% LinearRegression example
% x=0:1:9;
% y=3.0 * x + 7 + rand(1,length(x)) ;
% [p,Rsq] = LinearRegression (x,y) ;
% yfit = p(1)*x + p(2) ;
% plot(x,y,"o",x,yfit) ;
p = polyfit(Xin,Yin,1);
Yfit =  p(1) * Xin + p(2);
Yresid = Yin - Yfit;
SSresid = sum(Yresid.^2);
SStotal = (length(Yin)-1) * var(Yin);
Rsq = 1 - SSresid/SStotal;

No comments:

Labels