Passing a variable constraint to fmincon

조회 수: 9 (최근 30일)
Rifat
Rifat 2023년 5월 18일
댓글: Rifat 2023년 5월 18일
Hi!,
I am using the 'fmincon' solver for optimizing a function that takes array of inputs. The optimization function is FUN = @(x)((sqrt((x(1) - a)^2 + (x(2) - b)^2) + sqrt((x(1) - c)^2 + (x(2) - d)^2))^2 - (y(nn))^2)^2. Note that y(nn) changes with 'nn'.
Currently, I am defining 'FUN' in a for loop so I can pass a scalar 'y'. Similary, the constraint function for 'FUN', i.e. c = sqrt((x(1)- h).^2 + (x(2) - g).^2)- y(nn) also change with 'nn'. However, I cannot call c(x,y(nn)) inside fimincon and neither can I define c(x) inside the for loop with a fixed value of y(nn). So, I would like to ask:
  1. Is there any efficient way to pass the vector input 'y' to fmincon without defining FUN in a for loop?
  2. How do I pass a vector argument to c(x) in addition to 'x'?
Thank you.

채택된 답변

Matt J
Matt J 2023년 5월 18일
편집: Matt J 2023년 5월 18일
I think this is what you mean,
for nn=1:N
ynn=y(nn);
FUN = @(x)((sqrt((x(1) - a)^2 + (x(2) - b)^2) + sqrt((x(1) - c)^2 + (x(2) - d)^2))^2 - (ynn)^2)^2
CON=@(x) nonlcon(x,ynn,h,g);
xopt=fmincon(FUN,x0,A,b,Aeq,beq,lb,ub,con);
end
function [c,ceq]=nonlcon(x,y,h,g)
c = sqrt((x(1)- h).^2 + (x(2) - g).^2)- y;
ceq=[];
end
  댓글 수: 7
Walter Roberson
Walter Roberson 2023년 5월 18일
arrayfun() is slower then a for loop, as you have to call through an anonymous function for each iteration.
It is, however, often more convenient to write, and in easier cases can produce code that is much easier to understand than a loop. In more complicated cases, writing for arrayfun can easily produce code that is more difficut to understand than a loop would be.
Rifat
Rifat 2023년 5월 18일
Thanks guys for the insights!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by