make loop if three variable (phi ,G and n) present calculate value for (G and n) and store separate name for same formula ?

1 view (last 30 days)
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)))));

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 10 Apr 2023
"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.
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
B = 2×2 cell array
{[1.4680 1.5065 1.5482 1.5936 1.6433 1.6979 1.7583 1.8257 1.9015]} {[1.4621 1.4697 1.4777 1.4863 1.4954 1.5052 1.5158 1.5273 1.5397]} {[1.5360 1.5763 1.6200 1.6675 1.7194 1.7766 1.8398 1.9103 1.9896]} {[1.5298 1.5378 1.5462 1.5552 1.5647 1.5750 1.5860 1.5980 1.6111]}
You can access corresponding values directly. Say you want to get the values for (n2,G1) -
B{2,1}
ans = 1×9
1.5360 1.5763 1.6200 1.6675 1.7194 1.7766 1.8398 1.9103 1.9896

More Answers (1)

Bhanu Prakash
Bhanu Prakash on 10 Apr 2023
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 Comments
Arvind
Arvind on 10 Apr 2023
Thanks,
Bhanu Prakash;
Can we store each row separately with different name like B1 for (n1,G1) , B2 for (n1,G2) and so on?
Steven Lord
Steven Lord on 10 Apr 2023
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!