MATLAB Answers

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:

5 views (last 30 days)
Nesara S R
Nesara S R on 26 Jun 2019
Edited: infinity on 3 Jul 2019
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
  3 Comments
infinity
infinity on 3 Jul 2019
It will be better if you can provide more information of the problem, for example, the values of inputs?
Or you may try to find by what is going on with your code by using debug as guided in this link:

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!