Created
May 26, 2012 22:56
-
-
Save anuvrat/2795554 to your computer and use it in GitHub Desktop.
Compute cost for linear regression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
% We need to return the following variable | |
J = sum((X * theta - y) .^ 2) / (2*m) | |
end |
First write your code in computeCost.m don't run this code
then run ex.m and submit the assignment
data = load('ex1data1.txt'); % read comma separated data
y = data(:, 2);
m = length(y); % number of training examples
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1);
excellent
code is worked
thanks a lot bros
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is the answer of J as I am getting incorrect answer while submitting