clc,clear all, close all
x = [23.80,27.60,31.60,32.40,33.70,34.90,43.20,52.80,63.80,73.40];
y = [41.4,51.8,61.70,67.90,68.70,77.50,95.90,137.40,155.0,175.0];
%绘制原始数据散点图
figure
plot(x,y,'r*')
xlabel('x(职工工资总额)','fontsize', 12)
ylabel('y(商品零售总额)', 'fontsize',12)
set(gca,'linewidth',2);
%第一种方式
Lxx = sum((x-mean(x)).^2);
Lxy = sum((x-mean(x)).*(y-mean(y)));
b1 = Lxy/Lxx;
b0 = mean(y)-b1*mean(x);
y1 = b1*x+b0;
hold on
plot(x,y1,'linewidth',2);
%第二种方式
a1 = (10*sum(x.*y)-sum(x)*sum(y))/(10*sum(x.^2)-sum(x)*sum(x));
a2 = (sum(y)-a1*sum(x))/10;
y2 = a1*x+a2;
hold on
plot(x,y2,'g--o','linewidth',2);
x = [23.80,27.60,31.60,32.40,33.70,34.90,43.20,52.80,63.80,73.40];
y = [41.4,51.8,61.70,67.90,68.70,77.50,95.90,137.40,155.0,175.0];
%绘制原始数据散点图
figure
plot(x,y,'r*')
xlabel('x(职工工资总额)','fontsize', 12)
ylabel('y(商品零售总额)', 'fontsize',12)
set(gca,'linewidth',2);
%第一种方式
Lxx = sum((x-mean(x)).^2);
Lxy = sum((x-mean(x)).*(y-mean(y)));
b1 = Lxy/Lxx;
b0 = mean(y)-b1*mean(x);
y1 = b1*x+b0;
hold on
plot(x,y1,'linewidth',2);
%第二种方式
a1 = (10*sum(x.*y)-sum(x)*sum(y))/(10*sum(x.^2)-sum(x)*sum(x));
a2 = (sum(y)-a1*sum(x))/10;
y2 = a1*x+a2;
hold on
plot(x,y2,'g--o','linewidth',2);