solve function with two variables by ga toolbox

조회 수: 5 (최근 30일)
nadia nadi
nadia nadi 2023년 5월 21일
댓글: nadia nadi 2023년 5월 21일
hello,
I have a function with two variables ang i want to optimize the function by using ga. I used the toolbox long time ago and i forgot how to use it. Can any one help me please.
my function is
y= 0.25-4.27*x(1)+0.61*x(2)+13.34*x(1)*x(2)-4.69*x(2).^2;
and the range of the variables are
lb = [0.001,0.01];
ub = [0.045,0.1];;
I'm trying to use
numberOfVariables = 2;
[x,fval] = ga(FitnessFunction,numberOfVariables,lb,ub)
Thanks

채택된 답변

Walter Roberson
Walter Roberson 2023년 5월 21일
format long g
FitnessFunction = @(x) 0.25-4.27*x(1)+0.61*x(2)+13.34*x(1)*x(2)-4.69*x(2).^2;
lb = [0.001,0.01];
ub = [0.045,0.1];
numberOfVariables = 2;
A = []; b = [];
Aeq = []; beq = [];
[x,fval] = ga(FitnessFunction, numberOfVariables, A, b, Aeq, beq, lb, ub)
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x = 1×2
0.045 0.01
fval =
0.069484
%cross-check
[xf, fvalf] = fmincon(FitnessFunction, randn(numberOfVariables, 1), A, b, Aeq, beq, lb, ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
xf = 2×1
0.0449999033023298 0.0100003582625156
fvalf =
0.0694847999986172

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 5월 21일
Here is how it can be solved with GA:
lb = [0.001, 0.01];
ub = [0.045, 0.1];
numberOfVariables = 2;
y=@(x) 0.25-4.27*x(1)+0.61*x(2)+13.34*x(1)*x(2)-4.69*x(2).^2;
options = optimoptions('ga','PlotFcn', @gaplotbestf);
[x,fval] = ga(y,numberOfVariables,[],[],[],[],lb,ub, [], options)
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
x = 1×2
0.0450 0.0100
fval = 0.0695
  댓글 수: 1
nadia nadi
nadia nadi 2023년 5월 21일
Thanks it is nice of you to send me this code.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Global or Multiple Starting Point Search에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by