Schwefel function trying to find global minimum with three variables, but I am receiving a error from function

581 views Asked by At

I am writing a Schwefel function with three variables x1, x2 and x3 with x (-400,400), I am trying to find the global minimum of a Schwefel function. Can anybody tell me what's wrong with the function code.

function output = objective_function(in)
x1 = in(1);
x2 = in(2);
x3 = in(3);
output=(-x1.*sin(sqrt(mod(x1)))+(-x2.*sin(sqrt(mod(x2)))+(-x3.*sin(sqrt(mod(x3)));
ouput=[F1 F2];
1

There are 1 answers

0
MichaelTr7 On

Current Problems:

1: Mismatched delimiters in line:

output=(-x1.*sin(sqrt(mod(x1)))+(-x2.*sin(sqrt(mod(x2)))+(-x3.*sin(sqrt(mod(x3)));

2: Modulus requires a second argument. The second argument inputted into the mod() function needs to be the divisor. The modulus can only return the remainder if it knows the number being divided (dividend) and the number that is dividing (divisor). Calling the mod() function can follow the form:

mod(dividend,divisor);

Aside:

mod() is often used accidentally in replace of abs()

Modulus → mod() : Returns the remainder after division.
Example: mod(10,3) = 1 → 10/3 = 3 with remainder 1

Absolute → abs(): Returns the absolute/magnitude of the number.
Example: abs(-10) = 10 or abs(1 + 1i) = 1.4142


3: Variables F1 and F2 are not defined or initialized before being used. The variable called ouput may be a typo.

ouput=[F1 F2];

Playground Script:

Not sure what the function is supposed to exactly do but here is a script that you can modify to meet your needs. This gets rid of the errors but might need to be reconfigured to suit your exact functionality and output equation.

in = [1 2 3];
[output] = objective_function(in);

function [output] = objective_function(in)
x1 = in(1);
x2 = in(2);
x3 = in(3);

%Splitting into terms will help with debugging bracket balancing issues%
Divisor = 2;
Term_1 = (-x1.*sin(sqrt(mod(x1,Divisor))));
Term_2 = (-x2.*sin(sqrt(mod(x2,Divisor))));
Term_3 = (-x3.*sin(sqrt(mod(x3,Divisor))));

output = Term_1 + Term_2 + Term_3;

end

Ran using MATLAB R2019b