how to optimize a sweep for solving inequalities?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
KSSV
2023-1-16
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 个评论
更多回答(1 个)
Sulaymon Eshkabilov
2023-1-16
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Assumptions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!