Optimize two variables under two conditions

2 views (last 30 days)
Hi, this is my code below.
I want to maximize h and minimize d while two conditions are met. Condition 1: eps > c/t and condition 2: epz > m/d.
I guess I have to do a for loop for this, I do not really know.
h = 510*10^-3;
b = 170*10^-3;
t = 5*10^-3;
d = 4*10^-3;
R = 18*10^-3;
epsilon = sqrt((235*10^6)/(275*10^6));
m = (h-2*t)-2*sqrt(2)*R;
c = ((b-d)/2)-sqrt(2)*R;
% fläns TK3: 14e > c/t
eps = 14*epsilon;
% liv TK3: 124e > ngt/d
epz = 124*epsilon;

Accepted Answer

John D'Errico
John D'Errico on 2 Dec 2021
Edited: John D'Errico on 2 Dec 2021
Um, you cannot optimize TWO things at once.
There is no reason to assume that they will both achieve their respective optimal values at the same point in your parameter space, and if not, then how will you make the tradeoff?
Oh, and it is a really bad idea to name your variable eps, since that is the name of a really useful function in MATLAB. Do that, and one day soon you will be moaning and asking why the function named eps no longer works for you.
Next, I wonder what you think this does:
Answer: nothing. It dumps the value of c/t in the bit bucket after computing it.
What else? It appears from your code that h and d are fixed constants. So what are you hoping to optimize?
If I had to make a wild guess, I think you want to have two functinos of the variables h and d, where h and d have some unknown value, NOT the fixed value you gave them.
Oh, and learn to use scientific notation. It will make your code easier to write.
But if I look at your relations, they are actually pretty simple. Simple algebra is enough to learn a lot.
syms h d
b = 170e-3;
t = 5e-3;
R = 18e-3;
epsilon = sqrt(235/275);
Now we can look at your expression for c. c is simply linear in d, and has nothing to do with h. Can we solve for the set of values for d, such that the given relationship holds true? We have c as:
c = ((b-d)/2)-sqrt(2)*R
c = 
drelation = solve(14*epsilon >= c/t,d,'returnconditions',true)
drelation = struct with fields:
d: x parameters: x conditions: 214530430576822603/1801439850948198400 - (7*47^(1/2)*55^(1/2))/2750 <= x
And that gives us a lower bound on d. Any smaller than that, and your constraint will fail, but ANY value for d greater than -0.01 is acceptable. (x is the unknown d in this.)
ans = 
Of course, since you have provided only 2 significant digits at most in your numbers, we effectively know that d >= -0.01. Interestingly, d could be zero, and that singularity will be problematic.
Next, we can look at m. m is ALSO a linear function of h. But the constraint applies to m/d. So we have this expression:
124*epsilon - m/d > 0
See that I've written it so that we need this expression to be a positive value.
Where does that relationship hold true? I'll make this into a function so we can plot the positive part.
mfun = @(H) (H-2*t)-2*sqrt(2)*R;
fun = @(D,H) max(124*epsilon - mfun(H)./D,0);
xlabel 'd'
ylabel 'h'
Whereever that plotted surface is greater than zero, you have a valid solution. Since d must be as small as possible, then negative values for d that are greater than -0.01 are good. As long as d is less than zero, then make h = inf, which MAXIMIZES h.
And all of this analysis is probably meaningless, since I doubt that negative values of d even make any sense to you in context. If d must be positive, then make d as close to zero as possible, as long as it is positive. That MINIMIZES d, in which case as long as d is NOT zero but it is positive, the relation for m can be written
124*epsilon*d > m
Since we can make d as small as we wish, as long as it is positive, then this relationship reduces to
m < 0
Reemember that epsilon is positive, as is d, but d approaches zero as close as we can make it. So now we have:
(h-2*t)-2*sqrt(2)*R < 0
And therefore we can trivially solve for h.
h < 2*t + sqrt(2)*R
So the solution that MINIMIZES d as long as d is positive, AND maximizes h is
h = 2*t + sqrt(2)*R
h = 0.0355
Where d is as close to zero as possible as long as it is non-zero.
If d can be negative, I've already solved that problem, but somehow I think this solution may be more useful.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!