linregr.m
来自「several numerical method code」· M 代码 · 共 18 行
M
18 行
function [a, r2] = linregr(x,y)
x=[88.9 108.5 014.1 139.7 127 94 116.8 99.1];
y=[14.6 16.7 15.3 23.2 19.5 16.1 18.1 16.6];
n = length(x);
if length(y)~=n, error('x and y must be same length');
end
x = x(:); y=y(:); % convert to column vectors
sx = sum(x); sy = sum(y);
sx2 = sum(x.*x); sxy = sum(x.*y); sy2 = sum(y.*y);
a(1) = (n*sxy -sx*sy)/(n*sx2-sx^2);
a(2) = sy/n -a(1)*sx/n;
r2 = ((n*sxy -sx*sy)/sqrt(n*sx2-sx^2)/sqrt(n*sy2-sy^2))^2
%create plot of data and best fit line
xp= linspace(min(x),max(x),2);
yp = a(1)*xp + a(2);
plot(x,y,'o',xp,yp)
grid on
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?