% Steepest Descent Method
clc;clear all;close all;
%% lambda = 0.01;
lambda=0.01;

tolerance=0.000000001;
maxsteps=100; %k
x_initial=[0.1, -0.1];
clear x1 x2 CostFun;
count=0;
w=x_initial;
x(count+1)=w(1);
y(count+1)=w(2);
cost_function(count+1)=my_fun(w);

while ((abs(my_fun(w))>tolerance) & (count<=maxsteps))
    count=count+1;
   
    d=[deriv1(w), deriv2(w)];
    w=w-lambda*d;
    x(count+1)=w(1);
    y(count+1)=w(2);
    cost_function(count+1)=my_fun(w);
end;

plot(cost_function,'b-')
hold on
grid on

%% lambda=0.02;
lambda = 0.02;

tolerance=0.000000001;
maxsteps=100;
x_initial=[0.1, -0.1];
clear x1 x2 CostFun;
count=0;
w=x_initial;
x(count+1)=w(1);
y(count+1)=w(2);
cost_function(count+1)=my_fun(w);

while ((abs(my_fun(w))>tolerance) & (count<=maxsteps))
    count=count+1;
   
    d=[deriv1(w), deriv2(w)];
    w=w-lambda*d;
    x(count+1)=w(1);
    y(count+1)=w(2);
    cost_function(count+1)=my_fun(w);
end;

plot(cost_function,'r-')
hold on
%% lambda =0.1;
lambda = 0.1;

tolerance=0.000000001;
maxsteps=100;
x_initial=[0.1, -0.1];
clear x1 x2 CostFun;
count=0;
w=x_initial;
x(count+1)=w(1);
y(count+1)=w(2);
cost_function(count+1)=my_fun(w);

while ((abs(my_fun(w))>tolerance) & (count<=maxsteps))
    count=count+1;
   
    d=[deriv1(w), deriv2(w)];
    w=w-lambda*d;
    x(count+1)=w(1);
    y(count+1)=w(2);
    cost_function(count+1)=my_fun(w);
end;

plot(cost_function,'g-')
hold on
%% lambda =0.15;
lambda = 0.15;

tolerance=0.000000001;
maxsteps=100;
x_initial=[0.1, -0.1];
clear x1 x2 CostFun;
count=0;
w=x_initial;
x(count+1)=w(1);
y(count+1)=w(2);
cost_function(count+1)=my_fun(w);

while ((abs(my_fun(w))>tolerance) & (count<=maxsteps))
    count=count+1;
   
    d=[deriv1(w), deriv2(w)];
    w=w-lambda*d;
    x(count+1)=w(1);
    y(count+1)=w(2);
    cost_function(count+1)=my_fun(w);
end;

plot(cost_function,'c-')
hold on
%%
% legend('lamda=0.01','lamda=0.02','lamda=0.1','lamda=0.15')
% title('Cost Function Vs. Iterations')
% xlabel('Iterations')
% ylabel('Cost Function')

%% lambda = 1
lambda = 1;

tolerance=0.000000001;
maxsteps=100;
x_initial=[0.1, -0.1];
clear x1 x2 CostFun;
count=0;
w=x_initial;
x(count+1)=w(1);
y(count+1)=w(2);
cost_function(count+1)=my_fun(w);

while ((abs(my_fun(w))>tolerance) & (count<=maxsteps))
    count=count+1;
   
    d=[deriv1(w), deriv2(w)];
    w=w-lambda*d;
    x(count+1)=w(1);
    y(count+1)=w(2);
    cost_function(count+1)=my_fun(w);
end;

plot(cost_function,'m-')
hold on
%% lamda =0.5
lambda = 0.5;

tolerance=0.000000001;
maxsteps=100;
x_initial=[0.1, -0.1];
clear x1 x2 CostFun;
count=0;
w=x_initial;
x(count+1)=w(1);
y(count+1)=w(2);
cost_function(count+1)=my_fun(w);

while ((abs(my_fun(w))>tolerance) & (count<=maxsteps))
    count=count+1;
   
    d=[deriv1(w), deriv2(w)];
    w=w-lambda*d;
    x(count+1)=w(1);
    y(count+1)=w(2);
    cost_function(count+1)=my_fun(w);
end;

plot(cost_function,'k-')
hold on
title ('Cost Function - Steps')
xlabel('Steps');
ylabel('Cost Function');

%%
legend('lamda=0.01','lamda=0.02','lamda=0.1','lamda=0.15','lamda=1','lamda=0.5')
title('Cost Function Vs. Iterations')
xlabel('Iterations')
ylabel('Cost Function')
