由于在blog里面编辑公式比较麻烦,我就现在word里面把文档编辑好,然后截的图。
数据下载 点这
源代码如下:
%function liner_regression()
clc;
clear;
load('shuju.mat');
%datashuju = shuju;
data_x = shuju(:,1);
data_y = shuju(:,2);
syms theta0 theta1;
%h = theta0 + theta1 * data_x;
[r,c] = size(data_x);
J = 0;
for i = 1 : r
J = J + (theta0 + theta1 * data_x(i) - data_y(i))^2;
end
J = J / (2 * r);
theta = [theta0; theta1];
diffJtheta0 = diff(J,theta0);
diffJtheta1 = diff(J,theta1);
diffJ = [diffJtheta0;diffJtheta1];
theta_0 = [rand(1);rand(1)];
theta_1 = theta_0;
diffJtheta00 = diff(diffJtheta0,theta0);
diffJtheta01 = diff(diffJtheta0,theta1);
diffJtheta10 = diff(diffJtheta1,theta0);
diffJtheta11 = diff(diffJtheta1,theta1);
A = [diffJtheta00,diffJtheta01;diffJtheta10,diffJtheta11];
bestalpha = diffJ'* diffJ /(diffJ' * A * diffJ);
plot(data_x,data_y,'x');
axis([0 6 0 12]);
xlabel('x');
ylabel('y');
hold on
refline(theta_0(2),theta_0(1));
cnt1 = 0;
while (norm(subs(diffJ,theta,theta_0)) > epsilon)
tmp = subs(diffJ,theta,theta_0);
% use the best step lenth
%theta_0 = theta_0 - subs(bestalpha,theta,theta_0) * tmp;
%use the given step lenth
theta_0 = theta_0 - 0.05 * tmp;
if(mod(cnt1 , 100) == 0)
refline(theta_0(2),theta_0(1));
end
cnt1 = cnt1 + 1;
%norm(subs(diffJ,theta,theta_0))
end
cnt1
cnt2 = 0
theta_0 = theta_1;
figure(2);
plot(data_x,data_y,'x');
axis([0 6 0 12]);
xlabel('x');
ylabel('y');
hold on
refline(theta_0(2),theta_0(1));
while (norm(subs(diffJ,theta,theta_0)) > epsilon)
tmp = subs(diffJ,theta,theta_0);
% use the best step lenth
theta_0 = theta_0 - subs(bestalpha,theta,theta_0) * tmp;
%use the given step lenth
% theta_0 = theta_0 - 0.05 * tmp;
%if(mod(cnt , 100) == 0)
refline(theta_0(2),theta_0(1));
%end
cnt2 = cnt2 + 1;
%norm(subs(diffJ,theta,theta_0))
end
cnt2