function least_square_linear(X, Y)
% 用最小二乘法进行直线拟合
plot(X, Y, 'o')
xlabel('x')
ylabel('y')
hold on
% a = (X.' * X).^(-1) * (X.') * Y
x = mean(X); % X的均值
y = mean(Y); % Y的均值
b_0 = (X-x) * ((Y-y).') / ((X-x) * ((X-x).')) % 斜率
b_1 = y - b_0 .* x % 截距
y_pred = b_0 .* X + b_1 % 预测的y值
plot(X, y_pred)
end
x = [0.1, 0.4, 0.5, 0.6 ,0.7, 0.9];
y = [0.61, 0.92, 0.99, 1.52, 1.47, 2.03];
least_square_linear(x, y)