Filter löschen
Filter löschen

How to add a less than constraint condition to solve the transcendental equation

1 Ansicht (letzte 30 Tage)
I have tried to solve the transcendental equation by Genetic algorithm. The fitness function used for the equations is given below:
function y = myFitnes(x)
y = ((cos(x(1)) + cos(x(2)) + cos(x(3)) + cos(x(4))-0.9*(pi/2)))^2...
+(cos(3*x(1)) + cos(3*x(2)) + cos(3*x(3)) + cos(3*x(4)))^2...
+(cos(5*x(1)) + cos(5*x(2)) + cos(5*x(3)) + cos(5*x(4)))^2 ...
+ (cos(7*x(1)) + cos(7*x(2)) + cos(7*x(3)) + cos(7*x(4)))^2;
end
The main code written to solve the above equation using Genetic algorithm is given below:
objFcn =@myFitnes;
nvars = 4;
LB = [0 0 0 0];
UB = [pi/2 pi/2 pi/2 pi/2];
[x, fval] = ga(objFcn,nvars,[],[],[],[],LB,UB);
How to add the constraint condition 0<x(1)<x(2)<x(3)<x(4)<pi/2; to solve the above equations.

Akzeptierte Antwort

Matt J
Matt J am 7 Jan. 2018
Bearbeitet: Matt J am 7 Jan. 2018
A=[1,-1,0,0;
0, 1,-1;0;
0, 0, 1,-1];
b=[0;0;0];
objFcn =@myFitnes;
nvars = 4;
LB = [0 0 0 0];
UB = [pi/2 pi/2 pi/2 pi/2];
[x, fval] = ga(objFcn,nvars,A,b,[],[],LB,UB);
  4 Kommentare
Walter Roberson
Walter Roberson am 15 Sep. 2018
[1, -1, 0, 0]*[x1; x2; x3; x4] is x1-x2. Requiring that to be less than b(1)=0 is the condition x1-x2<=0. Add x2 to both sides to get x1<=x2
The A b matrix also encodes x2<=x3 the same way. Transitivity says you can then write x1 <= x2 <= x3 <= x4. The 0 at the beginning is expressed by lb 0. The const at the other end is ub const.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 7 Jan. 2018
objFcn =@myFitnes;
nvars = 4;
A = [1 -1 0 0;
0 1 -1 0;
0 0 1 -1]
b = [0;
0;
0];
LB = [0 0 0 0] + realmin; %disallow 0 exactly
UB = [pi/2 pi/2 pi/2 pi/2] * (1-eps); %disallow pi/2 exactly
[x, fval] = ga(objFcn, nvars, A, b, [], [], LB, UB);
This implements 0 < x(1) <= x(2) <= x(3) <= x(4) < pi/2 which is not exactly what you had asked for. Coding strict inequalities would require adding the nonlinear constraint function, which is possible in this case (since you do not have any integer constraints), but is not as efficient.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by