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!