Azzera filtri
Azzera filtri

how to optimize a sweep for solving inequalities?

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

KSSV
KSSV il 16 Gen 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)]
sol = 4851×2
1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12

Più risposte (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 16 Gen 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
  1 Commento
Alex Muniz
Alex Muniz il 17 Gen 2023
In this way the solution is found very quickly.
The problem is that I am using a symbolic equation and when I use the symbolic equation it gives an error.
V_jw_even = (- kd - 7)*w^4 + (ki - 9*kd + 17)*w^2 + 9*ki
This is part of the code. V_jw_even is calculated elsewhere in the code and the variables w, ki and kd are symbolic. Could you use your solution with this symbolic equation?
i0 = -1
i1 = 1
i2 = -1
% Define o passo de busca para x e y
thestep =0.2;
[ki,kd] = meshgrid(x_min:thestep:x_max,y_min:thestep:y_max);
%
% eqn1 = ki_;
% eqn2 = (-kd_ + ki_ + 1) ;
% eqn3 = (-15*kd_ + ki_ -55);
eqn1 = subs(V_jw_even,w,0)*i0
eqn2 = subs(V_jw_even,w,w_roots)*i1
eqn3 = subs(V_jw_even,w,inf)*i2
idx = eqn1 > 0 & eqn2 > 0 & eqn3 > 0;
sol = [ki(idx) kd(idx)]
Error displayed:
Error using symengine
Unable to prove '0 < -9*ki & 0 < 10*ki - 10*kd + 10 & 0 < Inf*sign(kd + 7) - Inf*(ki - 9*kd + 17)' literally. Use
'isAlways' to test the statement mathematically.
Error in sym/subsindex (line 849)
X = find(mupadmex('symobj::logical',A.s,9)) - 1;
Error in Calculo_Ki_Kd_dotM (line 218)
sol = [ki(idx) kd(idx)]

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by