3 views (last 30 days)

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.

Fabio Freschi
on 3 Mar 2020

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

dev3011
on 4 Mar 2020

Edited: dev3011
on 4 Mar 2020

Fabio Freschi
on 4 Mar 2020

fmincon allows the use of nonlinear constraints

look at the documentation

x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/508755-find-independent-variables-that-minimize-function#comment_804901

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/508755-find-independent-variables-that-minimize-function#comment_804901

## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/508755-find-independent-variables-that-minimize-function#comment_804940

⋮## Direct link to this comment

https://la.mathworks.com/matlabcentral/answers/508755-find-independent-variables-that-minimize-function#comment_804940

Sign in to comment.