% Generalized Penalized Function # 2
% Range of initial points: -50 <= xj <= 50 , j=1,2,...,n
% Global minima: (x1,x2,...,xn)=1
% f(X)=0 
% Coded by: Ali R. Alroomi | Last Update: 10 June 2015 | www.al-roomi.org  

clear
clc
warning off

x1min=-5;
x1max=5;
x2min=-5;
x2max=5;
R=1500; % steps resolution
x1=x1min:(x1max-x1min)/R:x1max;
x2=x2min:(x2max-x2min)/R:x2max;

a=5;
k=100;
m=4;

for j=1:length(x1)
    
    if x1(j)>a
        u1=k*(x1(j)-a).^m;
    elseif x1(j)<(-a)
        u1=k*(-x1(j)-a).^m;
    else
        u1=0;
    end
    
    for i=1:length(x2)
        if x2(i)>a
            u2=k*(x2(i)-a).^m;
        elseif x2(i)<(-a)
            u2=k*(-x2(i)-a).^m;
        else
            u2=0;
        end
        g=((x1(j)-1).^2)*(1+(sin(3*pi*x2(i))).^2);
        f(i)=0.1*(((sin(3*pi*x1(j))).^2)+g+((x2(i)-1).^2)*(1+(sin(2*pi*x2(i))).^2))+u1+u2;
    end
    
    f_tot(j,:)=f;

end

% 1-dimensional plot is not applicable with this benchmark function

figure(1)
meshc(x1,x2,f_tot);colorbar;set(gca,'FontSize',12);
xlabel('x_2','FontName','Times','FontSize',20,'FontAngle','italic');
set(get(gca,'xlabel'),'rotation',25,'VerticalAlignment','bottom');
ylabel('x_1','FontName','Times','FontSize',20,'FontAngle','italic');
set(get(gca,'ylabel'),'rotation',-25,'VerticalAlignment','bottom');
zlabel('f(X)','FontName','Times','FontSize',20,'FontAngle','italic');
title('3D View','FontName','Times','FontSize',24,'FontWeight','bold');

figure(2)
mesh(x1,x2,f_tot);view(0,90);colorbar;set(gca,'FontSize',12);
xlabel('x_2','FontName','Times','FontSize',20,'FontAngle','italic');
ylabel('x_1','FontName','Times','FontSize',20,'FontAngle','italic');
zlabel('f(X)','FontName','Times','FontSize',20,'FontAngle','italic');
title('X-Y Plane View','FontName','Times','FontSize',24,'FontWeight','bold');

figure(3)
mesh(x1,x2,f_tot);view(90,0);colorbar;set(gca,'FontSize',12);
xlabel('x_2','FontName','Times','FontSize',20,'FontAngle','italic');
ylabel('x_1','FontName','Times','FontSize',20,'FontAngle','italic');
zlabel('f(X)','FontName','Times','FontSize',20,'FontAngle','italic');
title('X-Z Plane View','FontName','Times','FontSize',24,'FontWeight','bold');

figure(4)
mesh(x1,x2,f_tot);view(0,0);colorbar;set(gca,'FontSize',12);
xlabel('x_2','FontName','Times','FontSize',20,'FontAngle','italic');
ylabel('x_1','FontName','Times','FontSize',20,'FontAngle','italic');
zlabel('f(X)','FontName','Times','FontSize',20,'FontAngle','italic');
title('Y-Z Plane View','FontName','Times','FontSize',24,'FontWeight','bold');