create a loop within another loop as the parameter changes
1 次查看(过去 30 天)
显示 更早的评论
hello everyone, i need help. i have an array m*n. for each value of n i need to perform a series of operations where a parameter (Y changes from 0 to 100). the following code needs to be repeated for each value of n.
C=[B,T0]; % matrix
Y=(0:0.1:100); % parameter that must change
Etat=zeros(1001,6681);
for ii=(1:1000);
ws=((1+rhob)/(1+rhob*Y(1,1+ii)))*C(1,1);
wsf=abs(ws);
wr=(((1+rhob)*Y(1,1+ii))/(1+rhob*Y(1,1+ii)))*C(1,1);
wrf=abs(wr);
h=((wsf-C(1,1))*(wrf-C(1,1)))/(C(1,1)*(wsf-wrf));
H=abs(h);
eta=1-(1-eta0)*H;
etar=0.965;
etas=0.97;
Ts= -((C(1,2)*C(1,1))/(0.28*wsf))*(1+(0.64/eta));
Tr=-((C(1,2)*C(1,1))/(wrf*eta))+(wsf/wrf)*((C(1,2)*C(1,1))/(0.28*wsf))*(1+(0.64/eta)));
etat=eta*((Tr*wrf+Ts*wsf)/((Tr*wrf/etar)+(Ts*wsf/etas)));
end
this series of operations must be repeated for all valuesi of the vector column B
thanks
回答(2 个)
Vaibhav
2024-2-6
编辑:Vaibhav
2024-2-6
Hi Alessandro
I understand that you would like to perform series of operations for each value of "n".
The existing code can be wrapped inside another loop that iterates over the values of "n". Below is a modification of your code to achieve this:
% Define necessary parameters and arrays
% rhob, eta0 etc
% Initialize your array m*n
% Assuming you have B and T0 defined
% Loop over each value of n
for n = 1:size(C, 2)
C = [B, T0]; % matrix
Y = (0:0.1:100); % parameter that must change
Etat = zeros(1001, 6681);
for ii = 1:1000
ws = ((1 + rhob) / (1 + rhob * Y(1, 1 + ii))) * C(1, n);
wsf = abs(ws);
wr = (((1 + rhob) * Y(1, 1 + ii)) / (1 + rhob * Y(1, 1 + ii))) * C(1, n);
wrf = abs(wr);
h = ((wsf - C(1, n)) * (wrf - C(1, n))) / (C(1, n) * (wsf - wrf));
H = abs(h);
eta = 1 - (1 - eta0) * H;
etar = 0.965;
etas = 0.97;
Ts = -((C(1, 2) * C(1, n)) / (0.28 * wsf)) * (1 + (0.64 / eta));
Tr = -((C(1, 2) * C(1, n)) / (wrf * eta)) + (wsf / wrf) * ((C(1, 2) * C(1, n)) / (0.28 * wsf)) * (1 + (0.64 / eta));
etat = eta * ((Tr * wrf + Ts * wsf) / ((Tr * wrf / etar) + (Ts * wsf / etas)));
end
end
Hope this helps!
George Abrahams
2024-2-6
编辑:George Abrahams
2024-2-6
Unfortunately your code contains mismatched brackets in the calculation of Tr, we don't know the sizes of rhob and eta0, and 2 elements of C are accessed in each loop which appears to contradict the question. For these reasons I can't offer you an exact answer.
It also seems that there's some confusion between columns and rows. Note that in MATLAB the 1st dimension is the row and the 2nd is the column, so A(2,4) references the element in row 2 column 4. The colon operator can be used to select entire rows or columns, for example, A(2,:) references the whole of row 2. I recommend you to read up on Matrix Indexing.
MATLAB is specialized for Array Operations, which I think is probably the answer to your question. With array operations, you can calculate the same equation for every element of the column at once, without needing to loop. For example, C(:,1)*Y(1) multiplies each element in the first column of C by the first element of Y. The alternative would be nested loops, as suggested by @Vaibhav.
3 个评论
George Abrahams
2024-2-7
Yep, there's an unmatched closing parenthesis, see below.
% )
% ( ) ( ) ( ) ( ) ¦
% ( ¦ ¦ ¦ ¦) ( ) ( ¦ ¦ ¦ ¦) ( ) ( ) ¦
% (¦ ¦ ¦ ¦ ¦¦ ¦ ¦) ( ) (¦ ¦ ¦ ¦ ¦¦ ¦ ¦) ( ¦ ¦)¦
% ¦¦ ¦ ¦ ¦ ¦¦ ¦ ¦¦ ¦ ¦ ¦¦ ¦ ¦ ¦ ¦¦ ¦ ¦¦ ¦ ¦ ¦¦¦
Tr=-((C(1,2)*C(1,1))/(wrf*eta))+(wsf/wrf)*((C(1,2)*C(1,1))/(0.28*wsf))*(1+(0.64/eta)));
Given the following test data, could you please provide some example code to manually calculate all of the values that you want? I think that this would be much clearer and help us to help you.
C = [1 2; 3 4]
Y = [1 2]
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!