how to optimize a sweep for solving inequalities?
1 view (last 30 days)
Show older comments
Solution of inequalities. For a project I need to solve systems of inequalities. The script below works, however it is very slow, it takes several minutes to resolve. How can I make it more efficient?
% ki < 0 && -kd + ki + 1 > 0 && -15*kd + ki -55 < 0
syms ki kd
i0 = 1
i1 = -1
i2 = -1
eqn1 = ki*i0
eqn2 = (-kd + ki + 1)*i1
eqn3 = (-15*kd + ki -55)*i2
x_min = -100;
x_max = 100;
y_min = -100;
y_max = 100;
% Define o passo de busca para x e y
step = 1;
for ki_ = x_min:step:x_max
for kd_ = y_min:step:y_max
eqn1_subs_ki_kd = subs(eqn1, {ki}, {ki_});
eqn2_subs_ki_kd = subs(eqn2, {ki kd}, {ki_ kd_});
eqn3_subs_ki_kd = subs(eqn3, {ki kd}, {ki_ kd_});
if eqn1_subs_ki_kd > 0 && eqn2_subs_ki_kd > 0 && eqn3_subs_ki_kd > 0
disp("solution found!")
ki_
kd_
end
end
end
0 Comments
Accepted Answer
KSSV
on 16 Jan 2023
You need to use syms. You can straight away substitute the values in the equation and get the lligcal indexing.
% ki < 0 && -kd + ki + 1 > 0 && -15*kd + ki -55 < 0
i0 = 1 ;
i1 = -1 ;
i2 = -1 ;
x_min = -100;
x_max = 100;
y_min = -100;
y_max = 100;
% Define o passo de busca para x e y
thestep = 1 ;
[ki_,kd_] = meshgrid(x_min:thestep:x_max,y_min:thestep:y_max) ;
eqn1 = ki_*i0 ;
eqn2 = (-kd_ + ki_ + 1)*i1 ;
eqn3 = (-15*kd_ + ki_ -55)*i2 ;
idx = eqn1 > 0 & eqn2 > 0 & eqn3 > 0 ;
sol = [ki_(idx) kd_(idx)]
0 Comments
More Answers (1)
Sulaymon Eshkabilov
on 16 Jan 2023
One of the possible ways to speed up your code is to get rid of display of resuts. Instead storing them, e.g.:
syms ki kd
i0 = 1;
i1 = -1;
i2 = -1;
eqn1 = ki*i0;
eqn2 = (-kd + ki + 1)*i1 ;
eqn3 = (-15*kd + ki -55)*i2;
x_min = -100;
x_max = 100;
y_min = -100;
y_max = 100;
% Define o passo de busca para x e y
step = 1;
ii=1;
for ki_ = x_min:step:x_max
for kd_ = y_min:step:y_max
eqn1_subs_ki_kd = subs(eqn1, {ki}, {ki_});
eqn2_subs_ki_kd = subs(eqn2, {ki kd}, {ki_ kd_});
eqn3_subs_ki_kd = subs(eqn3, {ki kd}, {ki_ kd_});
if eqn1_subs_ki_kd > 0 && eqn2_subs_ki_kd > 0 && eqn3_subs_ki_kd > 0
%disp("solution found!")
KI(ii) = ki_;
KD(ii)=kd_;
ii=ii+1;
end
end
end
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!