Find independent variables that minimize function

18 次查看(过去 30 天)
Hey,
I have a mathematical function with around 5 independent variables. I want to find the values of the independent variables for which the function is minimal. All the independent variables have certain boundaries that need to be satisfied.
Up until now I use the Excel plugin "Solver" for this. But my task becomes a bit too complex for Excel, thus I want to switch to Matlab.
This is my function:
st1_l = ((a-real(0.5*(a-(sqrt(a*(a-4*b))))))*(b^2))/((b^2)-(1.22*(0.0004)*c*(1-((-d/(a - b1)) * e)/d))*c*((a - real(0.5*(a-(sqrt(a*(a-4*b))))))+b))
Boundaries are something like this:
318<a<1000
10<b<100
...
The parameters should now be changed within these boundaries, so that st1_l becomes minimal. I already tried fmincon. However, I can't find a way to pass all the parameters and boundaries to fmincon. Just in case it makes clearer what I want to do, here an screenshot from the Excel solver I am using at the moment:
Thank you for any help.
  2 个评论
Mario Malic
Mario Malic 2020-3-3
编辑:Mario Malic 2020-3-3
fmincon(fun,x0,A,b)
Refer to documentation, with ~~variables A and b, you supply inequalities.~~
Edit: I am sorry, I made a silly mistake suggesting you inequalities. Better option is to use bounds as Fabio suggested.
dev3011
dev3011 2020-3-3
编辑:dev3011 2020-3-3
Thank you for your help. I tried to follow your suggenstion and the documentation and produced this code (the formula is shortened to only 2 IVs):
fun = @(x) ((x(1)-real(0.5*(x(1)-(sqrt(x(1)*(x(1)-4*x(2)))))))*(x(2)^2))/((x(2)^2)-(1.22*(0.0004)*(1-(10)/(x(1)))));
A = [1,0;-1,0;0,1;0,-1];
b = [1000;-318;100;-10];
I assume that with this I included the boundaries
318<a<1000
10<b<100.
However, this way seems to be overly complicated and also, when I try to introduce more than 2 independent variables by writing
fun = @(x) ((x(1)-real(0.5*(x(1)-(sqrt(x(1)*(x(1)-4*x(2)))))))*(x(2)^2))/((x(2)^2)-(1.22*(wellenl)*(1-(x(3))/(x(1)))));
A = [1,0,0;-1,0,0;0,1,0;0,-1,0];
b = [1000;-318;100;-10];
I get the error "A must have 2 column(s)". Any suggestions on how to use more than 2 IVs? Also, do I really have to use x(1), x(2), etc. or is there a way to use my own variable names in the formula?

请先登录,再进行评论。

采纳的回答

Fabio Freschi
Fabio Freschi 2020-3-3
If you have only lower and upper bounds, use them directly in fmincon
% your function
fun = @(x) ((x(1)-real(0.5*(x(1)-(sqrt(x(1)*(x(1)-4*x(2)))))))*(x(2)^2))/((x(2)^2)-(1.22*(0.0004)*(1-(x(3))/(x(1)))));
% inequality constraints (none);
A = [];
b = [];
% equality constraints (none)
Aeq = [];
beq = [];
% lower and upper bounds (I assume x(3) unbounded
lb = [318; 10; -Inf];
ub = [1000; 100; Inf];
% inital guess (here: mid point)
x0 = (ub-lb)/2;
% remove Inf
x0(x0 == Inf) = 0;
% run minimization
[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Regarding the name of the variables, if you use an anonymous function you should stick with x(1), x(2), etc. If you write your function in a file, you can rename teh variables inside the function
function f = fun(x)
a = x(1);
b = x(2);
c = x(3);
f = ((a-real(0.5*(a-(sqrt(a*(a-4*b))))))*(b^2))/((b^2)-(1.22*(0.0004)*(1-(c)/(a))));
end
  1 个评论
dev3011
dev3011 2020-3-4
Awesome, thanks a lot for this extensive answer. It works very well.
While implementing it, however, I found that I oversimplified my problem. I will accept your answer as it solves the question I asked perfectly and extend my question below.

请先登录,再进行评论。

更多回答(1 个)

dev3011
dev3011 2020-3-4
编辑:dev3011 2020-3-4
Fabios answer solves the problem I posted very well. However, I realized that I oversimplified my problem. Actually, some of my boundary conditions are functions rather than independent variables.
Let's assume I have the two following functions:
f = ((a-real(0.5*(a-(sqrt(a*(a-4*b))))))*(b^2))/((b^2)-(1.22*(0.0004)*(1-(c)/(a))));
d = ((a * b)^2)*g % In reality this function is more complicated
and I now want the boundary
10<d<100
to be satisfied. Thus, my boundary must be satisfied for a function that is not directly included in my initial function to be minimized but depends on independent variables of this function.
I guess I could solve every one of these boundary functions for the independent variable and then pass the result to fmincon() as a boundary condition. However, as the boundary functions are quite complex this would not be feasible. Is there any way I can pass a linear boundary function like above to fmincon()?
  1 个评论
Fabio Freschi
Fabio Freschi 2020-3-4
fmincon allows the use of nonlinear constraints
look at the documentation
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by