В Matlab онлайн это сошлось для меня за 47 итераций:
data = load('ex1data2.txt');
X = data(:,1:2);
y = data(:,3);
mu = mean(X); % Mean.
s = max(X) - min(X); % Range.
X = X - mu;
X = X ./ s;
m = size(X, 1); % Number of rows.
n = size(X, 2); % Number of columns.
X = [ones(m, 1) X]; % Add columns of ones to add biasing.
theta = zeros(n+1,1); % Initializing theta.
J = costFunction(X,y,theta);
alpha = 2.02;
iterations = 100;
j_hist = zeros(iterations,1); % Initializing j_hist.
m = size(X, 1);
n = size(theta,1);
for i=1 : iterations
hypo = X*theta;
for j = 1 : n
theta(j) = theta(j) - alpha*(1/m)*sum((hypo - y).*X(:,j));
end
j_hist(i) = costFunction(X,y,theta);
end
function J = costFunction(X,y,theta)
prediction = X*theta;
m = size(X, 1);
sqError = (prediction - y).^2; % ignore negative vals.
J = (1/(2*m))*sum(sqError); % derivative of sqroot cancels.
end
%J
%j_hist
%theta
Затем, после запуска, вы можете проверять J, j_hist и theta по отдельности, если хотите, раскомментировав их.