In the following gradient Descent code, theta values are not changing at all( but the for loop is running cz J_history is getting updated). And also even though i have given fprintf no value is being displayed:
1 view (last 30 days)
Show older comments
function [theta,J_history]=gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y);
for iter = 1:num_iters,
H=X*theta;
J1=H-y;
J2=(H-y).*X(:,2);
K=[sum(J1);sum(J2)];
theta=theta-(K*(alpha/m));
J_history(iter,1) = computeCost(X, y, theta);
fprintf('%f\n',J_history(iter));
end
end
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!