How to get all positive values in following code
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Karim
2022년 9월 21일
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
% 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 )
% 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)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!