create a loop within another loop as the parameter changes

조회 수: 1 (최근 30일)
Alessandro
Alessandro 2024년 2월 6일
댓글: Alessandro 2024년 2월 10일
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
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!
  댓글 수: 2
Alessandro
Alessandro 2024년 2월 6일
thanks for your reply, I omitted that in the calculation of Tr and Ts, I have to consider the values of each row and not just one
Alessandro
Alessandro 2024년 2월 10일
thank you all i solved it. thank you very much

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


George Abrahams
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
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)));
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
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]
C = 2×2
1 2 3 4
Y = [1 2]
Y = 1×2
1 2
Alessandro
Alessandro 2024년 2월 10일
thank you all i solved it. thank you very much

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by