1

I'm in the second week of Professor Andrew Ng's Machine Learning course through Coursera. We're working on linear regression and right now I'm dealing with coding the cost function.

The code I've written solves the problem correctly when i excecute each line one by one in the octave command line but returns 0 when i run it from the file computeCost.m file in Octave.

The code I've got so far.

function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression 
%   J = COMPUTECOST(X, y, theta) computes the cost of using theta as the 
%   parameter for linear regression to fit the data points in X and y

% Initialize some useful values
%m = length(y) % number of training examples

% You need to return the following variables correctly 


% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
%               You should set J to the cost.


m = size(X,1);
predictions= X*theta;
sqrErrors= (predictions-y).^2;
J= 1/(2*m) * sum(sqrErrors);


=========================================================================

end

I've set

X=[1 1; 1 2; 1 3]     y=[1;2;3]      and theta=[0,1]

When I execute the above lines one by one in command prompt i get J= 0 and J=2.333 when theta=[0;0].But when i run the same code from the file computeCost.m from octave command line i always get J=0 no matter what value i set for theta..Please help

2
  • 1
    I saved your computeCost.m in a file. And then computeCost(X,y,[0;1]) = 0, and computeCost(X,y,[0;0]) ans = 2.3333. Both are correct. Try to print out the intermediate result to debug. Commented Oct 19, 2014 at 22:55
  • 2
    ensure you are executing the right code by checking you are in the right directory using pwd. Commented Oct 20, 2014 at 5:37

2 Answers 2

0

Is it possible that you make an error when calling computeCost? You mention you are running the script from computeCost.m. (I think the best is you describe better which code pasrt is in which file and how you call the functions)

The rule is: If your function name is "computeCost", the function should be implemented (function until endfunction) in a file called "computeCost.m". (There are some exceptions which I leave out)

It's also possible that you call computeCost erroneously. Consider this script

C = 0;
function C = addthem (A, B)
  C = A+B;
endfunction

addthem (2, 3); # This WON'T update C

C = addthem (4, 5); # This will update C

You could also set a breakpoint with "keyboard" in computeCost and dbstep from there. Have you had a look at the debugging functions? It's really worth to use them because this makes debugging really easy: Debugging in Octave

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is in the architecture. You are not allowed to input or define your variables separately in computeCost.m or manually. The only way to test it is via ex1.m

So, do not input any variables to the initial computeCost.m: just edit the formula.

Don't worry if 'y' or 'x' are undefined under ex1.m: these problems will disappear.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.