%   IF  Clause
%   Objective:
%       use "IF" clause to test whether a condition is met.
%			"IF" yes, then do something
%			"ELSE" do something else
%
%   Remarks:
%       (1)This program also demonstrates the usage of "fprintf" which is very useful
% 				command to printf out message with variable value.
%		  (2)This program also servers as a hint to the first homework.
x_k=input('Give an initial guess of x = ');
N=20; % set up how many iterations to stop
for i=1:N
      f_k=x_k^2 + 2*x_k +3;%evaluate function value.
      d_k= -1 ;				%a simple pre-defined moving direction			
      x_k1 = x_k + d_k;		% new x
      f_k1 = x_k1^2 + 2*x_k1 +3;%evaluate new function value.
      if f_k1 > f_k , % do something 
         disp('End of program');
         fprintf('f_k = %f and f_k1=%f \n',f_k,f_k1);
         break, % force the for loop to end 
      else%do something else
         disp('Continue to move on....');
         fprintf('Iteration  %i : new function value is %f ****\n',i,f_k1);
      end
      x_k=x_k1;%prepare to go back to move on the next step.
end