How to get all positive values in following code

1 view (last 30 days)
MIch
MIch on 21 Sep 2022
Moved: Rik on 22 Sep 2022
Hello.
If possible, I need help with my code.
I wrote the code and I need to get all positive values for a variable named delta but it seems that my code isn't successful enough. Is it possible to save good T values (the ones that result with positive delta) and then when the loop is broken to continue from the place where the loop was broken???
%constants
var_g_T=1.2;
var_d_T=0.05;
C= 0.3;
P= [2.0 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.0 2.5 2.5];
b_r=14;
g_r=[1 2 2 3 4 5 6 6 7 7 8 8 9 10 11 12 12 13 14 14];
r_r=[6 1 7 2 3 4 5 14 5 13 7 9 10 11 12 13 14 8 1 9];
v_gr = size (g_r);
b_gr = v_gr(2);
v_rr = size (r_r);
b_rr = v_rr(2);
kv_g=[3232 5924 5924 3556 3738 2401 6109 6109 5223 5223 6093 6093 2484 3883 3707 5899 5899 2991 5199 5199];
kv_r=[3233 996 1890 3556 2244 2401 1197 1874 1197 987 1890 1165 2484 2344 3707 987 1874 2991 996 1165];
%for loop
for i = 1 : 5
delta_neg=false;
while delta_neg==0
for m = 1 : 14
T(i,m) = (var_g_T-var_d_T)*rand(1)+var_d_T;
end
for n = 1:b_rr
gpp(i,n)=P(1,g_r(1,n));
gvp(i,n)=0.14*T(i,g_r(1,n))/((kv_g(i,n)/gpp(i,n))^0.02 -1);
rpp(i,n)=P(1,r_r(1,n));
rvp(i,n)=0.14*T(i,r_r(1,n))/((kv_r(i,n)/rpp(i,n))^0.02 -1);
% is delta positive???
delta(i,n)=rvp(i,n)-gvp(i,n)-C;
disp(delta)
if delta(i,n)>0
delta_neg=true;
else
delta_neg=false;
break
end
end
end
end

Answers (1)

Karim
Karim on 21 Sep 2022
Im modified the code a bit, see below for the adjustments and some coments in the code.
I stored the T values along with the i and n index into a table. Is this what you needed?
%constants
var_g_T=1.2;
var_d_T=0.05;
C= 0.3;
P= [2.0 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.0 2.5 2.5];
b_r=14;
g_r=[1 2 2 3 4 5 6 6 7 7 8 8 9 10 11 12 12 13 14 14];
r_r=[6 1 7 2 3 4 5 14 5 13 7 9 10 11 12 13 14 8 1 9];
v_gr = size (g_r);
b_gr = v_gr(2);
v_rr = size (r_r);
b_rr = v_rr(2);
kv_g=[3232 5924 5924 3556 3738 2401 6109 6109 5223 5223 6093 6093 2484 3883 3707 5899 5899 2991 5199 5199];
kv_r=[3233 996 1890 3556 2244 2401 1197 1874 1197 987 1890 1165 2484 2344 3707 987 1874 2991 996 1165];
numN = b_rr;
numI = 5;
% compute all values of "T"
T = (var_g_T-var_d_T).*rand(numI,numN) + var_d_T
T = 5×20
0.2096 0.8173 1.1229 1.0401 0.3960 1.0598 0.6261 0.7479 0.3235 0.0804 1.1333 1.1929 1.1431 1.1939 1.0399 0.6139 0.1151 1.1760 0.5305 0.3641 0.9752 0.2826 0.8557 0.5500 0.9210 1.1690 1.0053 1.0433 1.1118 0.4117 0.6160 0.7454 1.0043 0.5711 0.2330 1.1377 0.8652 0.7427 1.1052 0.3710 0.3143 0.8098 0.8303 0.2676 1.0149 0.5286 0.1634 1.1807 0.1821 0.1114 0.3258 0.2602 0.0637 0.2262 0.9717 0.6669 0.9608 0.5051 0.5073 0.5251 0.2599 0.6098 0.7954 0.3595 0.2799 0.5029 0.0561 0.3552 0.8500 0.2601 0.1308 0.3171 0.5341 0.7645 1.1256 1.0907 0.6599 0.5355 0.7342 1.1104 0.3396 0.1736 0.4916 0.9848 0.7131 0.6029 0.8343 0.6765 0.4516 0.5311 0.7465 0.4529 0.6683 0.2592 0.4811 0.8691 0.5817 0.4345 0.0748 0.5035
% intialize some matrices to store the temporary results
[gpp,gvp,rpp,rvp,delta] = deal(zeros(numI,numN));
n = 1:numN;
for i = 1:numI
gpp(i,n) = P(1,g_r(n));
gvp(i,n) = 0.14*T(i,g_r(n))./((kv_g(n)./gpp(i,n)).^0.02 -1);
rpp(i,n) = P(1,r_r(n));
rvp(i,n) = 0.14*T(i,r_r(n))./((kv_r(n)./rpp(n)).^0.02 -1);
% evaluate all delta's
delta(i,n) = rvp(i,n) - gvp(i,n) - C;
end
% extract the values from T that have a positive delta
T_positive = T( delta > 0 )
T_positive = 13×1
0.2096 0.2826 0.4916 1.0598 0.1634 0.3552 0.7454 0.3171 1.1377 1.0907
% get the i and n value for the positive T values
[i_value,n_value] = find(delta > 0 );
% store the data in a table
MyResult = table(T_positive,i_value,n_value)
MyResult = 13×3 table
T_positive i_value n_value __________ _______ _______ 0.20958 1 1 0.28256 2 2 0.49162 5 3 1.0598 1 6 0.16338 3 7 0.3552 4 8 0.74545 2 12 0.31709 4 12 1.1377 2 16 1.0907 4 16 0.86914 5 16 0.65992 4 17 0.50514 3 18
  1 Comment
MIch
MIch on 22 Sep 2022
Moved: Rik on 22 Sep 2022
Thanks for your answer but this is not what I was looking for. I need to get positive delta calculated from the gvp and rvp equations. T is the variable in those equation, and in this example there is 14 T variables (the highest value in g_r and r_r variables)

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!