Optimization with 1 equation with 3 variables

Chemical Engineer here so my programming is not that good. I am wondering if there is a way to find the minimum of a variable, 'x'. There is only one equation, and I'd like to find the minumum of 'x' by optimizing 'y' and 'z.' I know that I could plug in numbers, but is there a way for Matlab to automatically do this for me? Thank you!

10 comentarios

Walter Roberson
Walter Roberson el 12 de Abr. de 2019
What operations does the equation involve? Is it a simple multinomial, or does it involve exp() ? trig functions? Bessel functions?
Do you have the symbolic toolbox?
Torsten
Torsten el 12 de Abr. de 2019
Editada: Torsten el 12 de Abr. de 2019
If you formulate your problem as
min: x
under the constraint
f(x,y,z) = 0
you can use MATLAB's "fmincon" to solve the problem for you.
Victoria
Victoria el 12 de Abr. de 2019
It is multinomial, and I don't believe I do
Walter Roberson
Walter Roberson el 12 de Abr. de 2019
for multinomial fminsearch is less likely to get caught in a local min than fmincon or fminunc is. However even fminsearch can get caught in local min by sufficiently steep multinomial.
For multinomial the more robust approach is the calculus based approach of solving for zeros of the derivative.
I'm still a little confused, apologies. Would my code look something like this then?
fun=myequation==0; %x is what I want to find as a minimum, y and z are what I want to optimize
x0=fmincon(fun,x,y,z);
Would this give me the actually value of the minimum of x (it can be negative), as well as the values of y and z used to get me that minumum.
Additionally, is there a way to bound the y and z variables? They describe pipe diameter and pressure, so there are values that I would deem too high or too low (negatives). Would it be something like -
y= [0 10];
z=[0 100];
Thank you for all the help!
fmincon needs a function handle, not a logical or symbolic expression.
initial = [1 1 1]; %for example
A = []; b = [];
Aeq = []; beq = [];
lb = [-inf 0 0]; ub = [inf 10 100];
fmincon(@myequation, initial, A, b, Aeq, beq, lb, ub)
I think I've got it running? However, I can't check the number because it returns lw, D2, and P1 (my x, y, and z) in symbolic form. I read sym() and double () will get it into numerical form, but when I tried it, it gave me no additional outputs. I hope it is the last time I need any help!
Code is below.
clear, clc, close all;
syms lw D2 P1
V1=(19/3);
Z=15;
PW=999.7;
hW=1;
L1=26;
WS1=0.149;
g=9.81;
Patm=1;
function R1 = myfun(lw)
R1= ((8*V1^2)/(pi^2*D2^4))+lw+g*Z+((P1-(Patm+(PW)*hW*g))/(PW))+(WS1)/(PW*V1)
%R1 = ((4*(V1^2)/(*pi^2)*(D2^4)))*((1/2)+2*Ff*((L1+40*sqrt(2)*D2))/(sqrt(2)*D2))+g*Z+((P1-(Patm+PW*g*hW))/(PW))+((WS1)/(PW*V1)))==0;
initial = [1 1 1]; %for example
D2 = []; P1 = [];
D2eq = []; P1eq = [];
lb = [-inf 0 0]; ub = [inf 10 100];
fmincon(@myfun, initial, D2, P1, D2eq, P1eq, lb, ub)
symN = sym([lw D2 P2]);
doubleN = double(symN);
end
Thanks again!
Walter Roberson
Walter Roberson el 13 de Abr. de 2019
There are a number of things wrong with that code, but we do not need to run it.
Your R1 is linear in lw, and your lw is not bounded (-inf to +inf). Therefore the minimum of myfun will be when lw is -infinity (as long as P1 and D2 are real valued and D2 is not 0)
Also, your R1 is linear in P1, so the minimum would be when P1 is minimum.
Also, your R1 is inversely proportional to D2, so the minimum would be when D2 is maximum.
Victoria
Victoria el 13 de Abr. de 2019
So there is no way for me to get a lw that is not -infinty? Would it be better to bound it? I'm trying to get minimal lost work, and D2 and P1 are diameter and pressure respectively. If not, I will just result to do it by hand. Both P1 and D2 will not be zero
Walter Roberson
Walter Roberson el 13 de Abr. de 2019
Review your equation. R1 is linear in P1, so minimum would be when P1 is minimum. R1 is inversely proportional to D2, so the minimum would be when D2 is maximum. Together with your bounds, that already tells you D2 = 10 and P1 = 0 . And then your equation is linear in lw, so you can make the result infinitely negative by making lw infinitely negative.
If the goal were to permit lw to be negative but that the overall minima of the function has to be >= 0, then provided you are willing to approximate 0.149 as 149/1000 instead of the actual [0.1485, 0.1495) range that 0.149 actually represents, then there is an exact solution that works out as approximately -137.34 . If you impose a lower bound of 0 for lw, then the minimum of the function is when lw is 0, and the minima would be approximately 137.34, which would decrease if you permitted D2 to become larger.

Iniciar sesión para comentar.

Respuestas (0)

Preguntada:

el 12 de Abr. de 2019

Comentada:

el 13 de Abr. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by