While trying to write a function for gradient descent in Matlab I got the following error:
Function with duplicate name "gradientDescent" cannot be defined
. The program I'm working on has two functions in it, and when I remove the second one the problem goes away. I don't understand why this is happening given that the two functions have completely different names. Here's the code:
function dJ = computeDerivative(X, y, theta, feature)
m = length(y); % number of training examples
hypothesis = X * theta;
error = ((hypothesis - y) / m) .* X(feature, :)
dJ = sum(error);
end
function theta = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
m = length(y); % number of training examples
for iter = 1:num_iters
for i = 1:length(theta)
theta(i) = theta(i) - alpha * computeDerivative(X, y, theta, i)
end
end
end