%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Set the random seed
rng(1);   

% Create some noisy data from the model y = -1 + 2x
m = 20;
x = 10*rand(m,1) - 5;
y = 2*x - 1 + randn(m,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create plots of cost function and data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

figure(1);           % Use Figure window 1
clf();               % Clear figure

% Create left subplot and plot contours of cost function
subplot(1,2,1);
hold on;
xlabel('Intercept \theta_0');
ylabel('Slope \theta_1');

% Compute cost function for a grid of points
[THETA0, THETA1] = meshgrid(linspace(-10, 10), linspace(-10,10));
[p,q] = size(THETA0);
J = zeros(p,q);
for i=1:p
    for j=1:q
        J(i,j) = cost_function(x, y, THETA0(i,j), THETA1(i,j));
    end
end

% Plot cost function contours
contour(THETA0, THETA1, J); 

% WARNING: This won't look correct until you implement cost_function()

% Create right subplot and plot the data
subplot(1,2,2);
hold on;
scatter(x, y);
xlim([-10 10]);
ylim([-10 10]);
xlabel('Input (x)');
ylabel('Output (y)');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot an initial hypothesis
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Set parameters to arbitrary values
theta0 = 4;
theta1 = -2;

subplot(1,2,1);                  % switch to left subplot
scatter(theta0, theta1, 'x');    % plot the 'x'

subplot(1,2,2);                                          % switch to right subplot
line_handle = line([-10 10], theta0 + theta1*[-10 10]);  % plot the line

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% GRADIENT DESCENT: TO BE COMPLETED 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% YOUR CODE HERE



% The following code can be uncommented and used inside the loop to update
% the plots and pause so you can see the figures updating.
%
% This assumes you have updated theta0 and theta1 and also that you have 
% saved the old parameter values in theta0_old and theta1_old.
%
% subplot(1,2,1);
% scatter(theta0, theta1, 'x');
% line([theta0_old theta0], [theta1_old, theta1]);
% 
% subplot(1,2,2);
% delete(line_handle); % delete the old line
% line_handle = plot(xx, theta0 + theta1*xx);
% 
% pause(0.01);
