% Problem # 18 (or Timonov's Function # 4)
% Range of initial points: 0 <= x <= 6
% Global maxima: x=2
% f(x)=0
% Coded by: Ali R. Alroomi | Last Update: 25 Feb. 2015 | www.al-roomi.org
 
clear
clc
warning off
 
xmin=0;
xmax=6;
R=100000; % steps resolution
x=xmin:(xmax-xmin)/R:xmax;
 
for i=1:length(x)
    
    if x(i)<=3
        f(i)=-(x(i)-2)^2;
    else
        f(i)=-2*log(x(i)-2)-1;
    end
    
end
 
plot(x,f,'r','LineWidth',2);grid;set(gca,'FontSize',12);
xlabel('x','FontName','Times','FontSize',20,'FontAngle','italic');
ylabel('f(x)','FontName','Times','FontSize',20,'FontAngle','italic');