# How to get postive dt without changing x(without the commented while loop)

조회 수: 2 (최근 30일)
MIch 2022년 3월 23일
댓글: MIch 2022년 3월 24일
I have written this code
%parameters
Max=1;
Min=0.01;
nvar=8;
I = [414 414 414 414 414 414 414 414];
Ikg = [8532 9342 9342 10435 10435 9031 9031 8532];
pgr = [1 6; 2 4; 3 1; 4 7; 8 3; 7 5; 6 8; 5 2]';
Ikr = [8532 9342 9342 10435 10435 9031 9031 8532];
C = 0.3;
gvp = [];
gp = [];
rvp = [];
rp=[];
gk = [];
rk = [];
dt = [];
vp = size (pgr);
bp = vp(2);
%calculation
a = (Max-Min)*rand(1,nvar)+Min;
x = a;
dim = size(a);
r = dim(1);
s = dim(2);
x = a;
for i = 1:r
for j = 1:bp
gk(i,j)= Ikg(i,pgr(1,j));
gp(i,j)=I(i,pgr(1,j));
gvp(i,j)=0.14*x(i,pgr(1,j))/((gk(i,j)/gp(i,j))^0.02 -1);
rk(i,j)=Ikr(j);
rp(i,j)=I(i,pgr(2,j));
rvp(i,j)=(0.14*(x(i,pgr(2,j))))/((rk(i,j)/rp(i,j))^0.02 -1);
dt(i,j)=rvp(i,j)-gvp(i,j)-C;
% while dt(i,j)<0
% x = (Max-Min)*rand(1,nvar)+Min;
% gvp(i,j)=0.14*x(i,pgr(1,j))/((gk(i,j)/gp(i,j))^0.02 -1);
% rvp(i,j)=(0.14*(x(i,pgr(2,j))))/((rk(i,j)/rp(i,j))^0.02 -1);
% dt(i,j)=rvp(i,j)-gvp(i,j)-C;
% end
end
end
Can someone give me an idea how to get dt positive without using commented section of code written above. If I use that commented section I get all positive values for dt, but x then changed and I need x to be one randomly generated vector [1 nvar] always.
If commmented section is not used, I get correct values since I generated x outside the for loop and it didn't changed in the proces. But then I have negative values of dt and that doesn't fit for my project.
##### 댓글 수: 4이전 댓글 2개 표시이전 댓글 2개 숨기기
Torsten 2022년 3월 23일
Try this:
%parameters
Max=1;
Min=0.01;
nvar=8;
I = [414 414 414 414 414 414 414 414];
Ikg = [8532 9342 9342 10435 10435 9031 9031 8532];
pgr = [1 6; 2 4; 3 1; 4 7; 8 3; 7 5; 6 8; 5 2]';
Ikr = [8532 9342 9342 10435 10435 9031 9031 8532];
C = 0.3;
gvp = [];
gp = [];
rvp = [];
rp=[];
gk = [];
rk = [];
dt = [];
vp = size (pgr);
bp = vp(2);
%calculation
a = (Max-Min)*rand(1,nvar)+Min;
x = a;
dim = size(a);
r = dim(1);
s = dim(2);
%x = a;
X = 0.001:0.001:1;
fail = zeros(size(X));
for k = 1:numel(X)
x = X(k);
for i = 1:r
for j = 1:bp
gk(i,j)= Ikg(i,pgr(1,j));
gp(i,j)=I(i,pgr(1,j));
gvp(i,j)=0.14*x/((gk(i,j)/gp(i,j))^0.02 -1);
rk(i,j)=Ikr(j);
rp(i,j)=I(i,pgr(2,j));
rvp(i,j)=(0.14*x)/((rk(i,j)/rp(i,j))^0.02 -1);
dt(i,j)=rvp(i,j)-gvp(i,j)-C;
end
end
dt = dt(:);
dt = dt(dt<=0);
if ~isempty(dt)
fail(k) = 1;
else
fail(k) = 0;
end
end
plot(X,fail)
As you can see, your dt is <=0 for all values for x in the respective interval.
The initial code should work if you let
dt(i,j)=-(rvp(i,j)-gvp(i,j)-C);
MIch 2022년 3월 24일
Okay, thanks for the help. I rewrote my code, added some if statements and now it works well.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Optimization에 대해 자세히 알아보기

R2016a

### Community Treasure Hunt

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

Start Hunting!

Translated by