make loop if three variable (phi ,G and n) present calculate value for (G and n) and store separate name for same formula ?
1 回表示 (過去 30 日間)
古いコメントを表示
make loop for different combination of n and G calculate different B and store it by B1(n1,G1) , B2(n1,G2), B3(n2,G1) and B4(n2,G2) for plot
phi = 0.01:0.05:0.45;
n=[0.5 0.1];
V_clay = [0.0 0.3];
G = 0.9552 + 0.0448*exp(-V_clay/0.06714);
B= sqrt(4/3 + (36./(45.*(G.^2.)*((1-phi).^(2*n)))) + ((4*(1-(G.^2).*((1-phi).^(2*n))))./(3*(G.^2).*((1-phi).^(2*n)))));
2 件のコメント
Askic V
2023 年 4 月 10 日
Hi Arvind,
this could be done using nested loops-
Please check nested for loops in Matlab.
for i = 1:numel(n)
for j = 1:numel(G)
% calculate B....
end
end
採用された回答
Dyuman Joshi
2023 年 4 月 10 日
"Can we store each row separately with different name like B1 for (n1,G1) , B2 for (n1,G2) and so on?"
Any particular reason why you want to store the output like that?
What you want to do (i.e. Dynamically naming variables) is not a good idea. It is often slow, difficult to work with and hard to debug or analyse via Code Helper tools of MATLAB.
Read more about it here - "Why Variables Should Not Be Named Dynamically"
The better approach would be to use Indexing -
phi = 0.01:0.05:0.45;
n = [0.5 0.1];
V_clay = [0.0 0.3];
G = 0.9552 + 0.0448*exp(-V_clay/0.06714);
nl = numel(n);
nG = numel(G);
%Pre-allocation
B=cell(nl,nG);
for ix = 1:nl
for jx = 1:nG
B{ix,jx} = sqrt(4/3 + (36./(45.*(G(ix).^2.)*((1-phi).^(2*n(jx)))) + ((4*(1-(G(ix).^2).*((1-phi).^(2*n(jx))))))./(3*(G(ix).^2).*((1-phi).^(2*n(jx))))));
end
end
B
You can access corresponding values directly. Say you want to get the values for (n2,G1) -
B{2,1}
その他の回答 (1 件)
Bhanu Prakash
2023 年 4 月 10 日
Hi Arvind,
As per my understanding, you want to calculate values of "B", for different combinations of "n" & "G" and store those values.
Consider the code below:
phi = 0.01:0.05:0.45;
n=[0.5 0.1];
V_clay = [0.0 0.3];
G = 0.9552 + 0.0448*exp(-V_clay/0.06714);
B=zeros(4,9);
k=1;
for i=1:length(G)
for j=1:length(n)
B(k,:)= sqrt(4/3 + (36./(45.*(G(i).^2.)*((1-phi).^(2*n(j)))) + ((4*(1-(G(i).^2).*((1-phi).^(2*n(j))))))./(3*(G(i).^2).*((1-phi).^(2*n(j))))));
k=k+1;
end
end
To solve this question, nested "for" loops are required, with the values of indices "i" & "j" ranging from 1 to "length(G)" & "length(n)" respectively.
The matrix "B" is predefined as a zero matrix of size 4x9 (as we get 4 sets of values for "B", with the length of each set as 9). After the completion of execution, the four sets of values generated are stored in the four rows of the matrix "B".
You can refer to the documentation of "for" loop, for more info:
Hope this answer helps you.
Thanks,
Bhanu Prakash.
2 件のコメント
Steven Lord
2023 年 4 月 10 日
Can you dynamically create variables with numbered names like B1, B2, B3, etc.? Yes.
Should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.
参考
カテゴリ
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!