Problem: MATLAB Implementation of WOA and Related Functions

The problem requires implementing MATLAB functions (Ufun, F1-F23), Get_Functions_details, initialization, WOA, and func_plot for benchmark function optimization.


Input/Output: The problem takes detailed MATLAB function specifications (names, parameters, algorithm flows, constants, conditional logic) for Ufun, F1-F23, Get_Functions_details, initialization, WOA, and func_plot. It generates corresponding MATLAB function files that strictly implement the specifications, ensuring the functions pass the provided test cases (verifying input-output correctness).

Required dependencies:

Sub-steps:
step_number: 1
step_description_prompt: Please write a MATLAB function named Ufun that implements a piecewise power function. The function takes four input arguments x, a, k, m and returns the output o. Function definition: function o = Ufun(x, a, k, m). Algorithm flow: Compute the output o using element-wise operations. The output is the sum of two terms: 1) k element-wise multiplied by (x - a) raised to the element-wise power m, then element-wise multiplied by the logical array where x > a; 2) k element-wise multiplied by (-x - a) raised to the element-wise power m, then element-wise multiplied by the logical array where x < -a. For x values between -a and a (inclusive), both terms are zero, so o will be zero for those x.
function_header: function o=Ufun(x,a,k,m)

end
test_cases: clear; clc;
rng(42);
correct = 32.0000;
x = [-3, -1, 0, 1, 3];
a = 1;
k = 2;
m = 3;
result = Ufun(x, a, k, m);
val = sum(result);
if val~=correct
    error('Wrong')
end
correct = 500.0000;
x = [-4, -6, 2, 3, 5];
a = 2;
k = 5;
m = 3;
result = Ufun(x, a, k, m);
val = sum(result);
if val~=correct
    error('Wrong')
end
correct = 1302.00000;
x = [-1, -2, 5, 6, 4];
a = 1;
k = 6;
m = 3;
result = Ufun(x, a, k, m);
val = sum(result);
if val~=correct
    error('Wrong')
end
disp('Completely right!')

step_number: 2
step_description_prompt: Please write a MATLAB function named F1 that computes the sum of the squares of the elements in the input vector or matrix x.
Function Definition: function o = F1(x)
Algorithm Flow: Compute the sum of the squares of each element in x using element-wise squaring (x.^2) and then summing all elements with sum().
Function Body: o = sum(x.^2);


Please write a MATLAB function named F2 that computes the sum of the absolute values of elements in x plus the product of the absolute values of elements in x.
Function Definition: function o = F2(x)
Algorithm Flow: Compute the absolute values of each element in x using abs(x), sum these absolute values with sum(), compute the product of these absolute values with prod(), then add the sum and product together to get the result.
Function Body: o = sum(abs(x)) + prod(abs(x));


Please write a MATLAB function named F3 that computes the sum of the squares of the cumulative sums from the first element to the i-th element of x (where i ranges from 1 to the number of columns in x).
Function Definition: function o = F3(x)
Algorithm Flow: 1. Get the number of columns in x using size(x,2) and store it in dim. 2. Initialize the result o to 0. 3. Loop from i=1 to dim: a. Compute the sum of the first i elements of x using sum(x(1:i)). b. Square this sum. c. Add this squared value to o. 4. Return the accumulated o as the result.
Function Body:
dim = size(x,2);
o = 0;
for i=1:dim
    o = o + sum(x(1:i))^2;
end


Please write a MATLAB function named F4 that computes the maximum of the absolute values of elements in the input vector or matrix x.
Function Definition: function o = F4(x)
Algorithm Flow: Compute the absolute values of each element in x using abs(x), then find the maximum value among these absolute values with max().
Function Body: o = max(abs(x));


Please write a MATLAB function named F5 that computes the Rosenbrock function value for the input vector x.
Function Definition: function o = F5(x)
Algorithm Flow: 1. Get the number of columns in x using size(x,2) and store it in dim. 2. Compute the term 100*(x(2:dim) - (x(1:dim-1).^2)).^2 which corresponds to 100 times the square of (x_i - x_{i-1}²) for i from 2 to dim. 3. Compute the term (x(1:dim-1) - 1).^2 which corresponds to the square of (x_{i-1} - 1) for i from 2 to dim. 4. Sum both terms together to get the result.
Function Body:
dim = size(x,2);
o = sum(100*(x(2:dim) - (x(1:dim-1).^2)).^2 + (x(1:dim-1)-1).^2);


Please write a MATLAB function named F6 that computes the sum of the squares of the absolute values of (x + 0.5) for each element in x.
Function Definition: function o = F6(x)
Algorithm Flow: Compute (x + 0.5) for each element in x, take the absolute value of each result, square each absolute value, then sum all these squared values with sum().
Function Body: o = sum(abs((x + .5)).^2);


Please write a MATLAB function named F7 that computes the sum of (index * (element^4)) for each element in x (where index is from 1 to the number of columns in x) plus a random number between 0 and 1.
Function Definition: function o = F7(x)
Algorithm Flow: 1. Get the number of columns in x using size(x,2) and store it in dim. 2. Create a vector of indices from 1 to dim using [1:dim]. 3. Compute element-wise multiplication of the index vector with the fourth power of each element in x (x.^4). 4. Sum the result of this multiplication with sum(). 5. Add a random number generated by rand() (uniformly distributed between 0 and 1) to the sum to get the final result.
Function Body:
dim = size(x,2);
o = sum([1:dim].*(x.^4)) + rand;


Please write a MATLAB function named F8 that computes the sum of (-x .* sin(sqrt(abs(x)))) for each element in x.
Function Definition: function o = F8(x)
Algorithm Flow: For each element in x, compute the absolute value with abs(x), take the square root with sqrt(), compute the sine of that square root with sin(), multiply by -x (element-wise), then sum all these products with sum().
Function Body: o = sum(-x .* sin(sqrt(abs(x))));
test_cases: clear; clc;
rng(42);
correct = 6659.399639;
x = [1.2, -0.5, 2.3, -1.8, 0.7];
val = F1(x) + F2(x) + F3(x) + F4(x) + F5(x) + F6(x) + F7(x) + F8(x);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong')
end
correct = 116785.710695;
x = [1.6, -0.3, 5.6, -1.7, 0.9];
val = F1(x) + F2(x) + F3(x) + F4(x) + F5(x) + F6(x) + F7(x) + F8(x);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong')
end
correct = 24863.690416;
x = [2.5, -1.6, 2.6, -3.2, 1.3];
val = F1(x) + F2(x) + F3(x) + F4(x) + F5(x) + F6(x) + F7(x) + F8(x);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong')
end
disp('Completely right!')

step_number: 3
step_description_prompt: Please use MATLAB code to generate functions with the same names as provided to implement the corresponding mathematical functions. The functions must strictly replicate the input-output behavior, logical details, and dependencies of the original code. Assume any called functions (e.g., Ufun) are already defined.

function o = F9(x)
dim = size(x,2);
o = sum(x.^2 - 10*cos(2*pi.*x)) + 10*dim;
end

function o = F10(x)
dim = size(x,2);
o = -20*exp(-.2*sqrt(sum(x.^2)/dim)) - exp(sum(cos(2*pi.*x))/dim) + 20 + exp(1);
end

function o = F11(x)
dim = size(x,2);
o = sum(x.^2)/4000 - prod(cos(x./sqrt([1:dim]))) + 1;
end

function o = F12(x)
dim = size(x,2);
o = (pi/dim)*(10*((sin(pi*(1+(x(1)+1)/4)))^2) + sum((((x(1:dim-1)+1)./4).^2).*(1+10.*((sin(pi.*(1+(x(2:dim)+1)/4)))).^2)) + ((x(dim)+1)/4)^2) + sum(Ufun(x,10,100,4));
end

function o = F13(x)
dim = size(x,2);
o = .1*((sin(3*pi*x(1)))^2 + sum((x(1:dim-1)-1).^2.*(1+(sin(3.*pi.*x(2:dim))).^2)) + ((x(dim)-1)^2)*(1+(sin(2*pi*x(dim)))^2)) + sum(Ufun(x,5,100,4));
end

function o = F14(x)
aS = [-32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32 -32 -16 0 16 32; -32 -32 -32 -32 -32 -16 -16 -16 -16 -16 0 0 0 0 0 16 16 16 16 16 32 32 32 32 32];
for j = 1:25
    bS(j) = sum((x' - aS(:,j)).^6);
end
o = (1/500 + sum(1./([1:25] + bS))).^(-1);
end

function o = F15(x)
aK = [.1957 .1947 .1735 .16 .0844 .0627 .0456 .0342 .0323 .0235 .0246];
bK = [.25 .5 1 2 4 6 8 10 12 14 16]; bK = 1./bK;
o = sum((aK - (x(1).*(bK.^2 + x(2).*bK))./(bK.^2 + x(3).*bK + x(4))).^2);
end

function o = F16(x)
o = 4*(x(1)^2) - 2.1*(x(1)^4) + (x(1)^6)/3 + x(1)*x(2) - 4*(x(2)^2) + 4*(x(2)^4);
end

function o = F17(x)
o = (x(2) - (x(1)^2)*5.1/(4*(pi^2)) + 5/pi*x(1) - 6)^2 + 10*(1 - 1/(8*pi))*cos(x(1)) + 10;
end
test_cases: clear; clc;
rng(42);
correct = 143.557619;
x5 = [0.5, 1.2, 0.3, 1.5, 0.8];
x2 = [0.5, 1.2];
x4 = [0.3, 0.7, 1.5, 2.1];
val = F9(x5) + F10(x5) + F11(x5) + F12(x5) + F13(x5) + F14(x2) + F15(x4) + F16(x2) + F17(x2);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
correct = 148.143694;
x5 = [0.6, 2.2, 2.2, 1.6, 0.9];
x2 = [0.1, 1.2];
x4 = [0.3, 0.2, 2.3, 1.1];
val = F9(x5) + F10(x5) + F11(x5) + F12(x5) + F13(x5) + F14(x2) + F15(x4) + F16(x2) + F17(x2);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
correct = 131.306685;
x5 = [0.8, 1.4, 0.3, 1.2, 0.8];
x2 = [0.2, 1.1];
x4 = [0.4, 0.3, 1.1, 2.1];
val = F9(x5) + F10(x5) + F11(x5) + F12(x5) + F13(x5) + F14(x2) + F15(x4) + F16(x2) + F17(x2);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
disp('Completely right!')

step_number: 4
step_description_prompt: Please use MATLAB code to generate a function named F18 that calculates the value of the specified mathematical function based on the input vector x. The function definition is: function o = F18(x). The input x is a 2-element vector, and the output o is a scalar. The calculation formula is o = (1 + (x(1) + x(2) + 1)^2 * (19 - 14*x(1) + 3*(x(1)^2) - 14*x(2) + 6*x(1)*x(2) + 3*(x(2)^2))) * (30 + (2*x(1) - 3*x(2))^2 * (18 - 32*x(1) + 12*(x(1)^2) + 48*x(2) - 36*x(1)*x(2) + 27*(x(2)^2))).

Please use MATLAB code to generate a function named F19 that calculates the value of the specified mathematical function based on the input vector x. The function definition is: function o = F19(x). The input x is a 3-element vector, and the output o is a scalar. First, define the constant matrices and vector: aH = [3 10 30; 0.1 10 35; 3 10 30; 0.1 10 35]; cH = [1 1.2 3 3.2]; pH = [0.3689 0.117 0.2673; 0.4699 0.4387 0.747; 0.1091 0.8732 0.5547; 0.03815 0.5743 0.8828]. Initialize o to 0. Then use a for loop from i = 1 to 4: o = o - cH(i) * exp(-sum(aH(i,:) .* ((x - pH(i,:)) .^ 2))).

Please use MATLAB code to generate a function named F20 that calculates the value of the specified mathematical function based on the input vector x. The function definition is: function o = F20(x). The input x is a 6-element vector, and the output o is a scalar. First, define the constant matrices and vector: aH = [10 3 17 3.5 1.7 8; 0.05 10 17 0.1 8 14; 3 3.5 1.7 10 17 8; 17 8 0.05 10 0.1 14]; cH = [1 1.2 3 3.2]; pH = [0.1312 0.1696 0.5569 0.0124 0.8283 0.5886; 0.2329 0.4135 0.8307 0.3736 0.1004 0.9991; 0.2348 0.1415 0.3522 0.2883 0.3047 0.6650; 0.4047 0.8828 0.8732 0.5743 0.1091 0.0381]. Initialize o to 0. Then use a for loop from i = 1 to 4: o = o - cH(i) * exp(-sum(aH(i,:) .* ((x - pH(i,:)) .^ 2))).

Please use MATLAB code to generate a function named F21 that calculates the value of the specified mathematical function based on the input vector x. The function definition is: function o = F21(x). The input x is a 4-element vector, and the output o is a scalar. First, define the constant matrices and vector: aSH = [4 4 4 4; 1 1 1 1; 8 8 8 8; 6 6 6 6; 3 7 3 7; 2 9 2 9; 5 5 3 3; 8 1 8 1; 6 2 6 2; 7 3.6 7 3.6]; cSH = [0.1 0.2 0.2 0.4 0.4 0.6 0.3 0.7 0.5 0.5]. Initialize o to 0. Then use a for loop from i = 1 to 5: o = o - ((x - aSH(i,:)) * (x - aSH(i,:))' + cSH(i))^(-1).

Please use MATLAB code to generate a function named F22 that calculates the value of the specified mathematical function based on the input vector x. The function definition is: function o = F22(x). The input x is a 4-element vector, and the output o is a scalar. First, define the constant matrices and vector: aSH = [4 4 4 4; 1 1 1 1; 8 8 8 8; 6 6 6 6; 3 7 3 7; 2 9 2 9; 5 5 3 3; 8 1 8 1; 6 2 6 2; 7 3.6 7 3.6]; cSH = [0.1 0.2 0.2 0.4 0.4 0.6 0.3 0.7 0.5 0.5]. Initialize o to 0. Then use a for loop from i = 1 to 7: o = o - ((x - aSH(i,:)) * (x - aSH(i,:))' + cSH(i))^(-1).

Please use MATLAB code to generate a function named F23 that calculates the value of the specified mathematical function based on the input vector x. The function definition is: function o = F23(x). The input x is a 4-element vector, and the output o is a scalar. First, define the constant matrices and vector: aSH = [4 4 4 4; 1 1 1 1; 8 8 8 8; 6 6 6 6; 3 7 3 7; 2 9 2 9; 5 5 3 3; 8 1 8 1; 6 2 6 2; 7 3.6 7 3.6]; cSH = [0.1 0.2 0.2 0.4 0.4 0.6 0.3 0.7 0.5 0.5]. Initialize o to 0. Then use a for loop from i = 1 to 10: o = o - ((x - aSH(i,:)) * (x - aSH(i,:))' + cSH(i))^(-1).
test_cases: clear; clc;
rng(42);
correct = 16649.063712;
x2 = [0.5, 1.2];  
x3 = [0.3, 0.7, 1.5]; 
x4 = [0.3, 0.7, 1.5, 2.1];
x6 = [0.2, 0.4, 0.6, 0.8, 1.0, 1.2]; 
val = F18(x2) + F19(x3) + F20(x6) + F21(x4) + F22(x4) + F23(x4);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end

correct = 17899.312189;
x2 = [0.6, 1.3];  
x3 = [0.5, 1.7, 1.2]; 
x4 = [0.1, 1.5, 1.6, 2.1];
x6 = [0.5, 0.2, 1.6, 0.8, 1.1, 1.2]; 
val = F18(x2) + F19(x3) + F20(x6) + F21(x4) + F22(x4) + F23(x4);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end

correct = 42552.178761;
x2 = [0.2, 1.3];  
x3 = [1.3, 0.7, 1.3]; 
x4 = [2.2, 0.6, 1.2, 0.5];
x6 = [0.2, 0.1, 0.7, 0.2, 1.0, 1.1]; 
val = F18(x2) + F19(x3) + F20(x6) + F21(x4) + F22(x4) + F23(x4);
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
disp('Completely right!')

step_number: 5
step_description_prompt: Please write a MATLAB function named Get_Functions_details that takes an input string F and returns four outputs: lb (lower bound), ub (upper bound), dim (dimension), and fobj (a function handle to the corresponding benchmark function).

The function signature must be: function [lb, ub, dim, fobj] = Get_Functions_details(F)

Implement the following logic using a switch-case structure to handle different values of F:
- For each case of F (from 'F1' to 'F23'), assign the corresponding values to fobj, lb, ub, and dim as follows:
  - Case 'F1': fobj = @F1; lb = -100; ub = 100; dim = 30;
  - Case 'F2': fobj = @F2; lb = -10; ub = 10; dim = 30;
  - Case 'F3': fobj = @F3; lb = -100; ub = 100; dim = 30;
  - Case 'F4': fobj = @F4; lb = -100; ub = 100; dim = 30;
  - Case 'F5': fobj = @F5; lb = -30; ub = 30; dim = 30;
  - Case 'F6': fobj = @F6; lb = -100; ub = 100; dim = 30;
  - Case 'F7': fobj = @F7; lb = -1.28; ub = 1.28; dim = 30;
  - Case 'F8': fobj = @F8; lb = -500; ub = 500; dim = 30;
  - Case 'F9': fobj = @F9; lb = -5.12; ub = 5.12; dim = 30;
  - Case 'F10': fobj = @F10; lb = -32; ub = 32; dim = 30;
  - Case 'F11': fobj = @F11; lb = -600; ub = 600; dim = 30;
  - Case 'F12': fobj = @F12; lb = -50; ub = 50; dim = 30;
  - Case 'F13': fobj = @F13; lb = -50; ub = 50; dim = 30;
  - Case 'F14': fobj = @F14; lb = -65.536; ub = 65.536; dim = 2;
  - Case 'F15': fobj = @F15; lb = -5; ub = 5; dim = 4;
  - Case 'F16': fobj = @F16; lb = -5; ub = 5; dim = 2;
  - Case 'F17': fobj = @F17; lb = [-5, 0]; ub = [10, 15]; dim = 2;
  - Case 'F18': fobj = @F18; lb = -2; ub = 2; dim = 2;
  - Case 'F19': fobj = @F19; lb = 0; ub = 1; dim = 3;
  - Case 'F20': fobj = @F20; lb = 0; ub = 1; dim = 6;
  - Case 'F21': fobj = @F21; lb = 0; ub = 10; dim = 4;
  - Case 'F22': fobj = @F22; lb = 0; ub = 10; dim = 4;
  - Case 'F23': fobj = @F23; lb = 0; ub = 10; dim = 4;

Note that the functions F1 to F23 (referred to by the function handles) are assumed to be pre-defined and do not need to be implemented in this function.
function_header: function [lb,ub,dim,fobj] = Get_Functions_details(F)

end
test_cases: clear; clc;
rng(42);
test_funcs = {'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'F10', ...
              'F11', 'F12', 'F13', 'F14', 'F15', 'F16', 'F17', 'F18', 'F19', ...
              'F20', 'F21', 'F22', 'F23'};
total = 0;
correct = 1150.207115;
for i = 1:length(test_funcs)
    [lb, ub, dim, fobj] = Get_Functions_details(test_funcs{i});
    if isscalar(lb) && isscalar(ub)
        x = (lb + ub) / 2 * ones(1, dim);
    else
        x = (lb + ub) / 2;
    end
    fval = fobj(x);
    total = total + sum(lb(:)) + sum(ub(:)) + dim + fval;
end
tol = abs(total-correct);
if tol>1e-4
    error('Wrong!')
end
disp('Completely right!')

step_number: 6
step_description_prompt: Please write a MATLAB function named initialization that initializes the positions of search agents within given boundaries. The function is defined as function Positions = initialization(SearchAgents_no, dim, ub, lb), where SearchAgents_no is the number of search agents, dim is the dimension of the search space, ub is the upper boundary (a scalar or a 1×dim vector), lb is the lower boundary (a scalar or a 1×dim vector), and Positions is the output matrix of size SearchAgents_no×dim storing initialized positions.

Algorithm steps:
1. Calculate Boundary_no as the number of columns of ub using size(ub, 2).
2. If Boundary_no equals 1 (ub and lb are scalars), compute Positions with Positions = rand(SearchAgents_no, dim) .* (ub - lb) + lb.
3. If Boundary_no is greater than 1 (ub and lb are 1×dim vectors), loop over each dimension i from 1 to dim:
   a. Extract ub_i = ub(i) and lb_i = lb(i).
   b. Assign the i-th column of Positions as Positions(:, i) = rand(SearchAgents_no, 1) .* (ub_i - lb_i) + lb_i.

Ensure the function name is exactly initialization, and strictly follow all logic details (input-output parameters, conditional branches, matrix operations) to produce results identical to the original code. Functions called in the original code are assumed to be pre-defined.
function_header: function Positions=initialization(SearchAgents_no,dim,ub,lb)

end
test_cases: clear; clc;
rng(42);

correct = 85.422048;
X1 = initialization(5, 3, 10, -5);
X2 = initialization(4, 3, [5,10,15], [-2,0,3]);
X3 = initialization(3, 2, 0, 0);
val = sum(X1(:)) + sum(X2(:)) + sum(X3(:));
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end

correct = 94.834255;
X1 = initialization(2, 5, 10, -7);
X2 = initialization(4, 3, [5,8,12], [-1,1,3]);
X3 = initialization(2, 2, 1, 4);
val = sum(X1(:)) + sum(X2(:)) + sum(X3(:));
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end

correct = 59.955864;
X1 = initialization(6, 2, 8, -8);
X2 = initialization(4, 3, [2,10,12], [-2,2,6]);
X3 = initialization(3, 4, 0, 1);
val = sum(X1(:)) + sum(X2(:)) + sum(X3(:));
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
disp('Completely right!')

step_number: 7
step_description_prompt: Please write a MATLAB function named WOA that implements the Whale Optimization Algorithm (WOA) to optimize a given objective function. The function will return the best fitness score found, the corresponding best position, and the convergence curve of the best fitness over iterations.

function [Leader_score,Leader_pos,Convergence_curve]=WOA(SearchAgents_no,Max_iter,lb,ub,dim,fobj)

Initialize Leader_pos as a 1-by-dim zero vector and Leader_score as infinity. Generate initial positions of search agents using the pre-defined initialization() function. Initialize Convergence_curve as a 1-by-Max_iter zero vector. Set iteration counter t to 0.

While t is less than Max_iter:
    For each search agent (from 1 to SearchAgents_no):
        Create Flag4ub (logical vector where Positions(i,:) exceeds ub) and Flag4lb (logical vector where Positions(i,:) is below lb). Adjust the agent's position to stay within bounds using: Positions(i,:) = Positions(i,:).*(~(Flag4ub+Flag4lb)) + ub.*Flag4ub + lb.*Flag4lb.
        Evaluate the agent's fitness using the input objective function fobj(Positions(i,:)).
        If the current fitness is less than Leader_score, update Leader_score to the current fitness and Leader_pos to the current agent's position.
    Calculate parameters: a = 2 - t*(2/Max_iter); a2 = -1 + t*(-1/Max_iter).
    For each search agent (from 1 to SearchAgents_no):
        Generate random numbers r1 = rand(), r2 = rand(). Compute A = 2*a*r1 - a, C = 2*r2. Set b = 1, l = (a2 - 1)*rand() + 1, and p = rand().
        For each dimension j (from 1 to dim) of the current agent:
            If p < 0.5:
                If abs(A) >= 1: Select a random leader index using rand_leader_index = floor(SearchAgents_no*rand() + 1), get X_rand = Positions(rand_leader_index, :). Compute D_X_rand = abs(C*X_rand(j) - Positions(i,j)), then update Positions(i,j) = X_rand(j) - A*D_X_rand.
                Else (abs(A) < 1): Compute D_Leader = abs(C*Leader_pos(j) - Positions(i,j)), update Positions(i,j) = Leader_pos(j) - A*D_Leader.
            Else (p >= 0.5): Compute distance2Leader = abs(Leader_pos(j) - Positions(i,j)), then update Positions(i,j) = distance2Leader*exp(b*l)*cos(l*2*pi) + Leader_pos(j).
    Increment t by 1. Save the current Leader_score to Convergence_curve(t). Display the current iteration t and Leader_score using [t Leader_score].
End the while loop.

Return Leader_score, Leader_pos, and Convergence_curve as outputs. The initialization() function called in the code is assumed to be pre-defined and does not need redefinition.
function_header: function [Leader_score,Leader_pos,Convergence_curve]=WOA(SearchAgents_no,Max_iter,lb,ub,dim,fobj)

end
test_cases: clear; clc;
rng(42);

SearchAgents_no = 30;
Max_iteration = 5;
Function_name = 'F2';
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Best_score,Best_pos,cg_curve]=WOA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = 1323365604057.234375;
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end

SearchAgents_no = 20;
Max_iteration = 10;
Function_name = 'F11';
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Best_score,Best_pos,cg_curve]=WOA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = 2532.501329;
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end

SearchAgents_no = 30;
Max_iteration = 6;
Function_name = 'F20';
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);
[Best_score,Best_pos,cg_curve]=WOA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
val = Best_score + sum(Best_pos) + sum(cg_curve);
correct = -9.194083;
tol = abs(val-correct);
if tol>1e-4
    error('Wrong!')
end
disp('Completely right!')

step_number: 8
step_description_prompt: Please write a MATLAB function named func_plot that takes a function name (func_name) as input to plot the 3D surface of the specified function using surfc. The function first calls Get_Functions_details (assumed to be predefined) to obtain lb, ub, dim, and fobj. Then, it sets the x and y vectors with specific ranges and step sizes based on func_name using a switch-case structure. Next, it initializes an empty matrix f, uses nested loops to compute f(i,j) by calling fobj with different input dimensions depending on func_name: for most functions, the input is [x(i), y(j)]; for 'F15', the input is [x(i), y(j), 0, 0]; for 'F19', the input is [x(i), y(j), 0]; for 'F20', the input is [x(i), y(j), 0, 0, 0, 0]; for 'F21', 'F22', or 'F23', the input is [x(i), y(j), 0, 0]. Finally, it plots the 3D surface using surfc with 'LineStyle' set to 'none'. The function definition and implementation must strictly match the following:

function func_plot(func_name)

[lb,ub,dim,fobj]=Get_Functions_details(func_name);

switch func_name
    case 'F1'
        x=-100:2:100; y=x;
    case 'F2'
        x=-100:2:100; y=x;
    case 'F3'
        x=-100:2:100; y=x;
    case 'F4'
        x=-100:2:100; y=x;
    case 'F5'
        x=-200:2:200; y=x;
    case 'F6'
        x=-100:2:100; y=x;
    case 'F7'
        x=-1:0.03:1;  y=x;
    case 'F8'
        x=-500:10:500; y=x;
    case 'F9'
        x=-5:0.1:5;   y=x;
    case 'F10'
        x=-20:0.5:20; y=x;
    case 'F11'
        x=-500:10:500; y=x;
    case 'F12'
        x=-10:0.1:10; y=x;
    case 'F13'
        x=-5:0.08:5; y=x;
    case 'F14'
        x=-100:2:100; y=x;
    case 'F15'
        x=-5:0.1:5; y=x;
    case 'F16'
        x=-1:0.01:1; y=x;
    case 'F17'
        x=-5:0.1:5; y=x;
    case 'F18'
        x=-5:0.06:5; y=x;
    case 'F19'
        x=-5:0.1:5; y=x;
    case 'F20'
        x=-5:0.1:5; y=x;
    case 'F21'
        x=-5:0.1:5; y=x;
    case 'F22'
        x=-5:0.1:5; y=x;
    case 'F23'
        x=-5:0.1:5; y=x;
end

L=length(x);
f=[];

for i=1:L
    for j=1:L
        if strcmp(func_name,'F15')==0 && strcmp(func_name,'F19')==0 && strcmp(func_name,'F20')==0 && strcmp(func_name,'F21')==0 && strcmp(func_name,'F22')==0 && strcmp(func_name,'F23')==0
            f(i,j)=fobj([x(i),y(j)]);
        end
        if strcmp(func_name,'F15')==1
            f(i,j)=fobj([x(i),y(j),0,0]);
        end
        if strcmp(func_name,'F19')==1
            f(i,j)=fobj([x(i),y(j),0]);
        end
        if strcmp(func_name,'F20')==1
            f(i,j)=fobj([x(i),y(j),0,0,0,0]);
        end
        if strcmp(func_name,'F21')==1 || strcmp(func_name,'F22')==1 || strcmp(func_name,'F23')==1
            f(i,j)=fobj([x(i),y(j),0,0]);
        end
    end
end

surfc(x,y,f,'LineStyle','none');

end
ground_truth_code: function func_plot(func_name)

[lb,ub,dim,fobj]=Get_Functions_details(func_name);

switch func_name
    case 'F1'
        x=-100:2:100; y=x;

    case 'F2'
        x=-100:2:100; y=x;

    case 'F3'
        x=-100:2:100; y=x;

    case 'F4'
        x=-100:2:100; y=x;
    case 'F5'
        x=-200:2:200; y=x;
    case 'F6'
        x=-100:2:100; y=x;
    case 'F7'
        x=-1:0.03:1;  y=x
    case 'F8'
        x=-500:10:500;y=x;
    case 'F9'
        x=-5:0.1:5;   y=x;
    case 'F10'
        x=-20:0.5:20; y=x;
    case 'F11'
        x=-500:10:500; y=x;
    case 'F12'
        x=-10:0.1:10; y=x;
    case 'F13'
        x=-5:0.08:5; y=x;
    case 'F14'
        x=-100:2:100; y=x;
    case 'F15'
        x=-5:0.1:5; y=x;
    case 'F16'
        x=-1:0.01:1; y=x;
    case 'F17'
        x=-5:0.1:5; y=x;
    case 'F18'
        x=-5:0.06:5; y=x;
    case 'F19'
        x=-5:0.1:5; y=x;
    case 'F20'
        x=-5:0.1:5; y=x;
    case 'F21'
        x=-5:0.1:5; y=x;
    case 'F22'
        x=-5:0.1:5; y=x;
    case 'F23'
        x=-5:0.1:5; y=x;
end



L=length(x);
f=[];

for i=1:L
    for j=1:L
        if strcmp(func_name,'F15')==0 && strcmp(func_name,'F19')==0 && strcmp(func_name,'F20')==0 && strcmp(func_name,'F21')==0 && strcmp(func_name,'F22')==0 && strcmp(func_name,'F23')==0
            f(i,j)=fobj([x(i),y(j)]);
        end
        if strcmp(func_name,'F15')==1
            f(i,j)=fobj([x(i),y(j),0,0]);
        end
        if strcmp(func_name,'F19')==1
            f(i,j)=fobj([x(i),y(j),0]);
        end
        if strcmp(func_name,'F20')==1
            f(i,j)=fobj([x(i),y(j),0,0,0,0]);
        end
        if strcmp(func_name,'F21')==1 || strcmp(func_name,'F22')==1 ||strcmp(func_name,'F23')==1
            f(i,j)=fobj([x(i),y(j),0,0]);
        end
    end
end

surfc(x,y,f,'LineStyle','none');

end
function_header: function func_plot(func_name)

end

step_number: 9
step_description_prompt: Please use MATLAB code to create a script that executes the Whale Optimization Algorithm (WOA) for benchmark function optimization, outputting the best solution, best objective value, and two visualization plots (parameter space of the function and convergence curve). The script must follow these steps:
1. Clear the workspace, command window, and set the random seed to 42 using `clear all`, `clc`, and `rng(42)`.
2. Initialize parameters: `SearchAgents_no = 30` (number of search agents), `Function_name = 'F1'` (target benchmark function), `Max_iteration = 500` (maximum iterations).
3. Retrieve benchmark function details (lower bound `lb`, upper bound `ub`, dimension `dim`, objective function handle `fobj`) via `[lb, ub, dim, fobj] = Get_Functions_details(Function_name)`.
4. Run the WOA function with parameters `SearchAgents_no`, `Max_iteration`, `lb`, `ub`, `dim`, `fobj` to get `Best_score` (best objective value), `Best_pos` (best solution), and `WOA_cg_curve` (convergence curve) using `[Best_score, Best_pos, WOA_cg_curve] = WOA(SearchAgents_no, Max_iteration, lb, ub, dim, fobj)`.
5. Create a figure with position `[269 240 660 290]` using `figure('Position', [269 240 660 290])`.
6. For `subplot(1,2,1)`: Plot the parameter space of `Function_name` using `func_plot(Function_name)`, set title to 'Parameter space', x-label to 'x_1', y-label to 'x_2', z-label to `[Function_name, '( x_1 , x_2 )']`.
7. For `subplot(1,2,2)`: Plot the convergence curve with `semilogy(WOA_cg_curve, 'Color', 'r')`, set title to 'Objective space', x-label to 'Iteration', y-label to 'Best score obtained so far'.
8. Configure the second subplot with `axis tight`, `grid on`, `box on`, and add a legend 'WOA' using `legend('WOA')`.
9. Display results using `display(['The best solution obtained by WOA is : ', num2str(Best_pos)])` and `display(['The best optimal value of the objective function found by WOA is : ', num2str(Best_score)])`.

Note: The functions `Get_Functions_details`, `WOA`, and `func_plot` are pre-defined and do not need reimplementation.
test_cases: rng(42)
correct_score = 3.549319e-89;
correct_pos = -1.612618e-44;
val_score = sum(Best_score(:));
val_pos = sum(Best_pos(:));
tol_score = abs((val_score-correct_score)/correct_score);
if tol_score > 1e-4
    error('Wrong')
end
tol_pos = abs((val_pos-correct_pos)/correct_pos);
if tol_pos > 1e-4
    error('Wrong')
end
disp('Completely right!')

General solution:

General tests: