fmincon with 2 decision variables and multiple value of one parameter in the model

Hi, I am using fmincon to do the constrained optimisation problem with 2 decision variables. Furthermore, I have one variable which is not the decision variable in the model. I wish to check how the value of this variable has an impact on the 2 decision variables. See below in detail.
max m*x(1)+2*x(2)+x(1)*x(2)
s.t. x(1)+x(2)<=18
x(1)>=0
x(2)>=0
check m=2:0.2:3, the optimal solution of x(1) and x(2).
how can I write the range of m in the code.
Thanks

 Accepted Answer

You are doing a parameter sweep. Run fmincon in a loop for the specified m
for m=2:0.2:3
fmincon(@(x)obj(x,m),x0,.....)
end
Where obj(x,m) is your objective function. Since you are interested in maximizing make sure that you flip the sign since fmincon always minimizes.

7 Comments

Thanks Shashank for the reply. I use both command window and edit window.
May I kindly ask you to confirm my input
edit window
function f=myfun(x,m) f(1)=m*x(1); f(2)=2*x(2); f(3)=x(1)*x(2); f=f(1)+f(2)+f(3); f=-f;
command window
for m=2:0.2:3 [x,fval]=fmincon('myfun',x0,[1,1],18,[],[],[0;0],[Inf;Inf]) end
but, it is not working ....
Could you help accordingly; Thanks.
Hi Ni, can you use the code formatting button to format your code, its hard to read. It looks like this --> {}Code, and makes code look like this:
function f = mfun
f = @(x,m)(-m*x(1) + 2*x(2) + x(1)*x(2))
for m=2:0.2:3
[x,fval]=fmincon(@(x)f(x,m),x0,[1,1],18,[],[],[0;0],[Inf;Inf])
end
If something doesn't work please provide info on why you think it doesn't work. This includes error messages, results or anything else that makes you believe that it doesn't work.
hi Shashank, unfortunately it is not working.
Error message provided as : error(message('optimlib:fmincon:WrongNumberOfColumnsInA', sizes.nVar))
do you have idea on this? Thanks.
hi Shashank, thanks, I run it again. It works. Thanks so much.
Hi Shashank,a quick question, do you know how I can plot with m and optimal solution of x(1). Thanks.
f = @(x,m)(-m*x(1) + 2*x(2) + x(1)*x(2))
m=2:0.2:3
for ii = 1:length(m)
[x(:,ii),fval]=fmincon(@(x)f(x,m(ii)),x0,[1,1],18,[],[],[0;0],[Inf;Inf])
end
plot(m,x(1,:))

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel Computing Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!