diff --git a/calculate_hypothesis.m b/calculate_hypothesis.m index 9532809..019b68a 100644 --- a/calculate_hypothesis.m +++ b/calculate_hypothesis.m @@ -1,9 +1,9 @@ function hypothesis = calculate_hypothesis(X, theta, training_example) %CALCULATE_HYPOTHESIS This calculates the hypothesis for a given X, %theta and specified training example - x0 = X(training_example, 1); - x1 = X(training_example, 2); + x = X(training_example, 1:length(theta)); - hypothesis = theta(1)*x0+theta(2)*x1; + hypothesis = sum(x.*theta); + %hypothesis = theta(1)*x0+theta(2)*x1+theta(3)*x2; end diff --git a/gradient_descent.m b/gradient_descent.m index a1f3492..9ad9635 100644 --- a/gradient_descent.m +++ b/gradient_descent.m @@ -19,34 +19,23 @@ function theta = gradient_descent(X, y, theta, alpha, iterations, do_plot) % gradient descent %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - theta_0 = theta(1); - theta_1 = theta(2); + % fill temporary array with values of theta + theta_temp = theta; + for ind = 1:length(theta_temp) + %update theta(1) and store in temporary variable theta_0 + sigma = 0.0; - %update theta(1) and store in temporary variable theta_0 - sigma = 0.0; + for i = 1:m + hypothesis = calculate_hypothesis(X, theta, i); + output = y(i); + sigma = sigma + (hypothesis - output) * X(i, ind); + end - for i = 1:m - hypothesis = calculate_hypothesis(X, theta, i); - output = y(i); - sigma = sigma + (hypothesis - output); + theta_temp(ind) = theta_temp(ind) - ((alpha * 1.0) / m) * sigma; end - theta_0 = theta_0 - ((alpha * 1.0) / m) * sigma; - - - %update theta(2) and store in temporary variable theta_1 - sigma = 0.0; - - for i = 1:m - hypothesis = calculate_hypothesis(X, theta, i); - output = y(i); - sigma = sigma + (hypothesis - output) * X(i, 2); - end - - theta_1 = theta_1 - ((alpha * 1.0) / m) * sigma; - %update theta - theta = [theta_0, theta_1]; + theta = theta_temp; %update cost_vector cost_vector = [cost_vector; compute_cost(X, y, theta)]; diff --git a/mllab1.m b/mllab1.m index 41e203c..4b93692 100644 --- a/mllab1.m +++ b/mllab1.m @@ -4,11 +4,11 @@ %% initialize theta = [0.0, 0.0]; %The weights of our model. -alpha = 0.01; %The step size for gradient descent. +alpha = 0.001; %The step size for gradient descent. iterations = 50; %do plotting do_plot = true; %% run gradient descent -t = gradient_descent(X, y, theta, alpha, iterations, do_plot); \ No newline at end of file +t = gradient_descent(X, y, theta, alpha, iterations, do_plot); diff --git a/mllab2.m b/mllab2.m index 27dbf8b..4bdff0b 100644 --- a/mllab2.m +++ b/mllab2.m @@ -15,4 +15,4 @@ iterations = 100; %% t = gradient_descent(X, y, theta, alpha, iterations); disp 'Press enter to exit!'; -pause; \ No newline at end of file +pause;