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

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 Comments

Then generate random values for dt and calculate x from them. Or are there also restrictions on x ?
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);
Okay, thanks for the help. I rewrote my code, added some if statements and now it works well.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2016a

Asked:

on 23 Mar 2022

Commented:

on 24 Mar 2022

Community Treasure Hunt

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

Start Hunting!