Tamir
Tamir

Reputation: 23

Cost function for linear regression with multiple variables in Matlab

The multivariate linear regression cost function:

enter image description here

Is the following code in Matlab correct?

function J = computeCostMulti(X, y, theta)
    m = length(y);
    J = 0;
    J=(1/(2*m)*(X*theta-y)'*(X*theta-y);
end

Upvotes: 2

Views: 5979

Answers (2)

Sarah
Sarah

Reputation: 21

Your are missing a ) in the end:

J=(1/(2*m))*(X*theta-y)'*(X*theta-y);
          ^

Upvotes: 2

Kurtis Pykes
Kurtis Pykes

Reputation: 351

There is two ways i tried which is essentially the same code.

    J = (X * theta - y)'*(X * theta - y)/2*m; 

or you can try:

    J = (1/(2*m))*(X * theta - y)'*(X * theta - y)

Upvotes: 2

Related Questions