For each fmincon it overwrites the previous values, meaning at the end there is only 1 set of values when there should be 100. The code is running 100 times and displays all sets of values but not storing all sets. How do I resolve this?

조회 수: 2 (최근 30일)
for X=1:1:10
for Y=1:1:10
ObjFcn = @myObjective;
x0 = [10 0.001 7];
LB = [0 0 0];
UB = [50 0.5 7.5];
ConsFcn = @(x)myConstraints(x,X,Y);
[x] = fmincon(ObjFcn,x0,[],[],[],[],LB,UB,ConsFcn);
disp(x)
disp(myObjective(x))
end
end

채택된 답변

Alan Weiss
Alan Weiss 2018년 4월 2일
Your code is inefficient in that you define many things inside the loop that should be assigned outside the loop. Using Walter's suggestion, try this:
ObjFcn = @myObjective;
x0 = [10 0.001 7];
LB = [0 0 0];
UB = [50 0.5 7.5];
x = zeros(10,10); % initialization
for X=1:10
for Y=1:10
ConsFcn = @(x)myConstraints(x,X,Y);
x(X,Y) = fmincon(ObjFcn,x0,[],[],[],[],LB,UB,ConsFcn);
disp(x(X,Y))
disp(myObjective(x(X,Y)))
end
end
  댓글 수: 5
Walter Roberson
Walter Roberson 2018년 4월 2일
ObjFcn = @myObjective;
x0 = [10 0.001 7];
LB = [0 0 0];
UB = [50 0.5 7.5];
x = zeros(10,10,3 ); % initialization
fvals = inf(10,10); % initialization
for X=1:10
for Y=1:10
ConsFcn = @(x)myConstraints(x,X,Y);
[x(X,Y,:), fvals(X,Y)] = fmincon(ObjFcn,x0,[],[],[],[],LB,UB,ConsFcn);
disp(x(X,Y))
disp(myObjective(x(X,Y)))
end
end
Scott Sanders
Scott Sanders 2018년 4월 3일
That seems to have done the trick. The final code that works is:
ObjFcn = @myObjective;
x0 = [10 0.001 7];
LB = [0 0 0];
UB = [100 0.3 7.5];
x=zeros(10,10,3);
fvals=inf(10,10);
for X=1:10
for Y=1:10
ConsFcn = @(x)myConstraints(x,X,Y);
[x(X,Y,:),fvals(X,Y)] = fmincon(ObjFcn,x0,[],[],[],[],LB,UB,ConsFcn);
disp(x(X,Y,:))
disp(myObjective(x(X,Y,:)))
end
end
Thank you all for your help!

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 4월 2일
Instead of assigning to x assign to x(X, Y)

카테고리

Help CenterFile Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by