create a loop within another loop as the parameter changes

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

Answers (2)

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 Comments

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
thank you all i solved it. thank you very much

Sign in to comment.

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 Comments

thanks for the reply, but eta0 and rhob are constants, defined beforehand. what i reported is just a part of the code. I rechecked there are no parenthetical errors in determining Tr.
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
thank you all i solved it. thank you very much

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Asked:

on 6 Feb 2024

Commented:

on 10 Feb 2024

Community Treasure Hunt

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

Start Hunting!