x1 = [0, 10, 20, 30, 40, 50, 60]; y1 = [120, 100, 80, 60, 40, 20, 0]; plot(x1,y1) function yy=Regress(x, y, n, al, a0, syx, r2) sumx=0; sumxy=0;st=0; sumy=0;sumx2=0;sr=0; for i = 1:n sumx = sumx + x(i); sumy = sumy + y(i); sumxy=sumxy+x(i)*y(i); sumx2=sumx2+x(i)*x(i); end xm = sumx/n; ym = sumy/n; a1 = (n*sumxy - sumx*sumy)/(n*sumx2 - sumx*sumx) a0 = ym - a1*xm for i = 1:n st = st + (y(i) - ym)^2; sr = sr + (y(i) - a1*x(i) - a0)^2; end syx = (sr/(n - 2))^(0.5); r2 = (st - sr)/st; yy=a0; yy=a1; end fplot(@(x) 120-2*x,[0 60],'b'); hold on plot(x1,y1,'o') hold off grid on; a00=Regress(x1, y1, 7,2,18,2,4); fprintf("Estos son los valores de a0 y a1.\n",a00)