Solve set of inequalities

22 views (last 30 days)
Dmitry Medvedev
Dmitry Medvedev on 19 Apr 2020
Commented: KSSV on 21 Jan 2022
Hello!
I want to solve set of inequalities. One of real number solution will be good enough for me.
Earlier I used MathCad for that. I am new to Matlab. I wrote code below, but it didn't work. Ideally I need same solution as I get in MathCad (not exactly but close).
A = -203;
B = 12189;
DAC05 = 206;
DAC45 = 1864;
gamma = 2;
syms C0 C1
eqn1 = 2048*((A+C0)/C1)>=DAC05-gamma;
eqn2 = 2048*((A+C0)/C1)<=DAC05+gamma;
eqn3 = 2048*((B+C0)/C1)>=DAC45-gamma;
eqn4 = 2048*((B+C0)/C1)<=DAC45+gamma;
eqns = [eqn1, eqn2, eqn3, eqn4];
S = vpasolve(eqns,[C0 C1]);
S.C0;
S.C1;
Can anyone help me with that?

Accepted Answer

John D'Errico
John D'Errico on 19 Apr 2020
Edited: John D'Errico on 19 Apr 2020
You cannot use tools like fsolve or vpasolve to solve systems of inequalities. They are not designed to do that. Really, your goal here is to find any feasible solution?
One simple approach is to use fmincon, allow it to find a feasible solution. Specify some simple objective function. For example, just minimize C1+C0, subject to the indicated inequality constraints.
In fact, I see that your inequalities can actually be re-written to make them simple linear inequalities, as long as C1 has the same sign. So do you have bounds on C0 and C1? Is C1 known to be always positive or negative?
In that case, it is even simpler to find a solution, since then you can just use linprog.
(By the way, it is generally a dangerous thing to use the name gamma for a variable, as that will then conflict with the function gamma.)
A = -203;
B = 12189;
DAC05 = 206;
DAC45 = 1864;
gam = 2;
Aineq =[ -2048, DAC05 - 2;...
2048, -DAC05 - 2;...
-2048, DAC45 - 2;...
2048, -DAC45 - 2];
bineq = [2048*A;-2048*A;2048*B;-2048*B];
f = [1 1]; % minimize the sum: C0 + C1
LB = [-inf,0]; % Constrain C1 to be non-negative
C01 = linprog(f,Aineq,bineq,[],[],LB)
Optimal solution found.
C01 =
1724.03971119134
15270.0457280385
I would point out that this is the same solution as found by MATHCAD, so probably it used the same scheme. (A linear probgramming tool really is the classical solution here.)
I could go further yet, and actually plot the feasible set. There is a very pretty code on the file exchange, called plotregion, by Per Bergström roughly 14 years ago. Nice tool, and it should still work.
plotregion(-Aineq,-bineq,LB)
grid on
hold on
plot(C01(1),C01(2),'ro')
Note that plotregion assumes the opposite sense for the inequality constraints, so I had to flip the signs on the matrices. Also note the axis scaling - beware that factor of 10^4 on the y axis. As you can see, the solution found is the one in the bottom left corner of the feasible set, closest to (0,0).
If your goal might be to find some point internal to the feasible set, then be careful with linprog. It will find a point at one of the boundary vertices. This is a characteristic of linear programming. You could more easily find an internal point using fmincon, if that were your goal.
Finally, if you are the lazy type (ok, that often applies to me, and what the heck, if we have this software, I suppose we might as well use it) you could have used the function equationsToMatrix to convert that system of symbolically written equalities into a matrix form. Here they were simple enough to do using pencil and paper. Since I got the same answer as MathCad did, I probably even got them right. ;-)
  5 Comments
John D'Errico
John D'Errico on 19 Apr 2020
Edited: John D'Errico on 19 Apr 2020
:) And I yours.

Sign in to comment.

More Answers (2)

darova
darova on 19 Apr 2020
Edited: darova on 19 Apr 2020
Here is what i invented
clc,clear
cla
A = -203;
B = 12189;
DAC05 = 206;
DAC45 = 1864;
gamma = 2;
c0 = linspace(-1e3,1e3,30)+1700;
c1 = linspace(-1e4,1e4,30)+15270;
[C0,C1] = meshgrid(c0,c1);
FA = 2048*(A+C0)./C1;
FB = 2048*(B+C0)./C1;
% eqn1 = FA >= DAC05-gamma;
% eqn2 = FA <= DAC05+gamma;
% eqn3 = FB >= DAC45-gamma;
% eqn4 = FB <= DAC45+gamma;
% ix = eqn1 & eqn2 & eqn3 & eqn4;
contour3(C0,C1,FA,[-1 1]*gamma+DAC05,'b')
hold on
contour3(C0,C1,FB,[-1 1]*gamma+DAC45,'r')
surface(C0,C1,FA,'facecolor','r','edgecolor','none')
surface(C0,C1,FB,'facecolor','b','edgecolor','none')
hold off
axis vis3d
alpha(0.3)
xlabel('C0')
ylabel('C1')
Results
I belive there is more simple solution. Anybody has other ideas?
  2 Comments
Dmitry Medvedev
Dmitry Medvedev on 19 Apr 2020
Thanks for your answer.
I am try different approach. Instead of set of inequalities, I wrote set of equation with parameter T1> 0.
function F = Untitled(C, T1)
A = -203;
B = 12189;
DAC05 = 206;
DAC45 = 1864;
gamma = 2;
F(1) = 2048*((A+C(1))/C(2))-DAC05+gamma == 0+T1;
F(2) = 2048*((A+C(1))/C(2))-DAC05-gamma == 0-T1;
F(3) = 2048*((B+C(1))/C(2))-DAC45+gamma == 0+T1;
F(4) = 2048*((B+C(1))/C(2))-DAC45-gamma == 0-T1;
end
But how can I solve it respectfully T1 parameter?
I think I need use some sort of code
T1 = 0.001;
x0 = [0,0];
x = fsolve(Untitled,x0,T1);
But I don't know correct sentence for that.
Error Matlab
Not enough input arguments.
Error in Untitled (line 8)
F(1) = 2048*((A+C(1))/C(2))-DAC05+gamma == 0+T1;
darova
darova on 19 Apr 2020
Maybe this
syms C0 C1 FA FB
eq1 = FA - 2048*(A+C0)./C1;
eq2 = FB - 2048*(B+C0)./C1;
res = solve([eq1 eq2],C0,C1);
res.C0
res.C1
wher
FA = DAC05+[-gamma .. +gamma]
FB = DAC45+[-gamma .. +gamma]

Sign in to comment.


Dmitry Medvedev
Dmitry Medvedev on 19 Apr 2020
Thanks everyone for your help.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!