make loop if three variable (phi ,G and n) present calculate value for (G and n) and store separate name for same formula ?
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
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
Arvind
on 10 Apr 2023
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.
Categories
Find more on Programming 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!