0

This function returns the error Not enough input arguments.

function numgrad = computeNumericalGradient(J, theta)

SIZE=length(theta);
numgrad = zeros(SIZE,1);
perturb = zeros(SIZE,1);
e = 1e-4;
for p = 1:SIZE
    % Set perturbation vector
    perturb(p) = e;
    loss1 = J(theta - perturb);
    loss2 = J(theta + perturb);
    % Compute Numerical Gradient
    numgrad(p) = (loss2 - loss1) / (2*e);
    perturb(p) = 0;
end

end

When I pass J and theta as follows:

function db = f(X)
db=X'*X;
end
theta=[1; 2; 3];

However this script runs fine.

theta=[1; 2; 3];

SIZE=length(theta);
numgrad = zeros(SIZE,1);
perturb = zeros(SIZE,1);
e = 1e-4;
for p = 1:SIZE
    % Set perturbation vector
    perturb(p) = e;
    loss1 = f(theta - perturb);
    loss2 = f(theta + perturb);
    % Compute Numerical Gradient
    numgrad(p) = (loss2 - loss1) / (2*e);
    perturb(p) = 0;
end

This is how I am calling my function.

theta=[1; 2; 3];
computeNumericalGradien(f,theta)

I do not understand how does the script work given that the only difference in the script and function is that the function and its argument are not arbitrary in the script.

PS: I am using MATLAB 2014

Tom
  • 1
  • 2

0 Answers0