Passing a variable constraint to fmincon
1 view (last 30 days)
Show older comments
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:
- Is there any efficient way to pass the vector input 'y' to fmincon without defining FUN in a for loop?
- How do I pass a vector argument to c(x) in addition to 'x'?
Thank you.
0 Comments
Accepted Answer
Matt J
on 18 May 2023
Edited: Matt J
on 18 May 2023
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 Comments
Walter Roberson
on 18 May 2023
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!