% Loading the dataset
dataSet = load(‘DataSet.txt’);
% Storing the values in seperate matrices
W1 = dataSet(:, 1);
w3 = dataSet(:, 2);
% Do you want feature normalization?
normalization = true;
% Applying mean normalization to our dataset
if (normalization)
maxX = max(W1);
minX = min(W1);
W1= (W1 - maxX) / (maxX - minX);
End
% Running gradient descent on the data
% 'w1' is our input matrix
% 'w3' is our output matrix
% 'parameters' is a matrix containing our initial theta and slope
parameters = [0; 0];
learningRate = 0.1;
iteration = 1500;
[parameters, criterionHistory] = gradient(w1, w3, parameters, learningRate, iteration);
% Plotting our final hypothesis
figure;
plot(min(w1 (:, 2)):max(w1 (:, 2)), parameters(1) + parameters(2) * (min(w1 (:, 2)):max(w1 (:, 2))));
hold on;
% Plotting our criterion function on a different figure to see how we did
figure;
plot(criterionHistory, 1:iteration);
% Finally predicting the output of the provided input
input = 120;
if (normalization)
input = (input - maxX) / (maxX - minX);
end
output = parameters(1) + parameters(2) * input;
disp(output);
gradient.m
function [ parameters, criterionHistory ] = gradient(w1, w3, parameters, learningRate, iteration )
% Getting the length of our dataset
m = length(w3);
% Creating a matrix of zeros for storing our criterion function history
criterionHistory = zeros(iteration, 1);
% Running gradient descent
for i = 1:iteration
% Calculating the transpose of our hypothesis
h = (w1* parameters – w3′;
% Updating the parameters
parameters(1) = parameters(1) – learningRate * (1/m) * h * w1(:, 1);
parameters(2) = parameters(2) – learningRate * (1/m) * h * w1 (:, 2);
% Keeping track of the criterion function
criterionHistory(i) = criterion(w1, w3, parameters);
end
criterion.m
parameters(1) = parameters(1) - learningRate * (1/m) * h * w1(:, 1);
parameters(2) = parameters(2) - learningRate * (1/m) * h * w1(:, 2);
function [ criterion ] = criterion( w1, w3, parameters )
% Calculates the criterion function
criterion = (w1 * parameters – w3)' * (w1 * parameters – w3) / (2 * length(w3));
end