make loop if three variable (phi ,G and n) present calculate value for (G and n) and store separate name for same formula ?
    6 views (last 30 days)
  
       Show older comments
    
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 Comments
  Askic V
      
 on 10 Apr 2023
				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
Accepted Answer
  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.
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}
More Answers (1)
  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
  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.
See Also
Categories
				Find more on Desktop in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




