
data = load("profit.txt");
x = data(:,1);
y = data(:,2);

m = length(x);

plot(x, y, 'x');
xlabel('Revenu en 10K euros');
ylabel('Population de la ville en 10,000');


% hypothesis function
function y = h(a, b, x)
   y = a*x + b;
end

% plot the hypothesis for a given a and b
function plot_h(a, b, x, y)
xh = 5:0.1:25;
yh = zeros(1, length(xh));

for i = 1:length(xh)
    yh(i) = h(a, b, xh(i));
end

plot(x, y, 'x', xh, yh,'-');
end

plot_h(1.8, -2, x, y)

function e = E(a, b, x, y)
   s = 0;
   m = length(x);
   for i = 1:m
      s = s + (h(a, b, x(i)) -y(i))^2;
   end
   e = sqrt(s)/(2*m);
end

E(1.8, 2, x, y)

function present_h(a, b, x, y)

plot_h(a, b, x, y);

printf("Error = %f \n", E(a, b, x, y));
pause;
end

present_h(1.8, 2, x, y)

a_vals = -1500:100:1500;
b_vals = -1500:100:1500;

E_vals = zeros(length(a_vals), length(b_vals));

for i = 1:length(a_vals)
    for j = 1:length(b_vals)
        E_vals(i,j) = E(a_vals(i), b_vals(j), x, y);
    end
end


E_vals = E_vals';
% Surface plot
figure;
surf(a_vals, b_vals, E_vals)
xlabel('a'); ylabel('b');


function [delta_a, delta_b] = gradient(a, b, x, y)
   s_a = 0;
   s_b = 0;
   m = length(x);
   for i = 1:m
      s_a = s_a + (h(a, b, x(i)) - y(i))*x(i);
      s_b = s_b + (h(a, b, x(i)) - y(i))*1;
   end
   delta_a = s_a/m;
   delta_b = s_b/m;
end

function [a,b] = descend(a0, b0, x, y, N)
a = a0;
b = b0;
alpha = 0.01;

present_h(a, b, x, y);
for i=1:N
  [delta_a, delta_b] = gradient(a, b, x, y);
  a = a - alpha*delta_a;
  b = b - alpha*delta_b;
  present_h(a, b, x, y);
end
         
end

descend(0, 0, x, y, 100)
