Having each iteration of nested loop plot on graph

7 views (last 30 days)
I have a test script of a much larger script for one nested loop. The nested loop uses 3 different vectorized variables to find a Radius, then using the same variables and the new radius, determine a value. The value is summed for each possible combination of x and y for a given z. I wish to plot that summed value, the the loop will return to z and pick a new z, where the sum is reset. Once each z has been used, the plot should show the summed value at each z recorded on a single plot. When I use the loop, I am only getting the final value of sigma, and nothing plotted. Can someone figure out why my nested loop is failing?
clear;clc;
gamma = 95;
zwater = 0;
H = 10;
TYP = 1;
x = 8;
v = 0.49;
array = 2;
arraysize = 6;
Q = 250;
Q = (Q/array^2);
nax = array;
nay = array;
sum =0;
xmin = x - arraysize/2;
xmax = x + arraysize/2;
yleft = -(arraysize/2);
yright = (arraysize/2);
z = 0:0.1:H;
xa = linspace(xmin+(1/(2*array)*arraysize),xmax-(arraysize/(2*array)),nax);
ya = linspace(yleft+(1/(2*array)*arraysize),yright-(arraysize/(2*array)),nay);
for zii = z
sum =0;
for xii = xa
for yii = ya
R = sqrt(xii^2+yii^2+zii^2);
sigprimeh = ((Q/(R*pi))*((3*zii*xii^2)/R^3)*((R*(1-2*v))/(R+zii)));
sum = sum +sigprimeh;
figure()
end
end
plot(sigprimeh,z)
title('Stress Due to Surcharge Load')
xlabel('\sigma`h (ksf)');
ylabel('Depth "z"(ft)');
set(gca,'YDir','reverse')
hold on
end

Accepted Answer

Tushar Behera
Tushar Behera on 10 Feb 2023
Edited: Tushar Behera on 10 Feb 2023
Hi Mabin,
I assume you want to know why your code is not performing as you had expected.
Your code has a problem since you calculate the "sigprimeh" value inside the innermost loop, overwriting it after each iteration and only charting the final value. After computing the total for each "zii", you must relocate the assignment statement outside the innermost loop in order to plot the cumulative sum for each "zii" value. Also you should use an array to store the values of "sigmaprimeh" as that will preserve all the calculated values. For example:
clear;
clc;
gamma = 95;
zwater = 0;
H = 10;
TYP = 1;
x = 8;
v = 0.49;
array = 2;
arraysize = 6;
Q = 250;
Q = (Q/array^2);
nax = array;
nay = array;
xmin = x - arraysize/2;
xmax = x + arraysize/2;
yleft = -(arraysize/2);
yright = (arraysize/2);
z = 0:0.1:H;
sigma_h = zeros(1,length(z)); % Preallocate memory for sigma_h
xa = linspace(xmin+(1/(2*array)*arraysize),xmax-(arraysize/(2*array)),nax);
ya = linspace(yleft+(1/(2*array)*arraysize),yright-(arraysize/(2*array)),nay);
for ii = 1:length(z)
sum =0;
for xii = xa
for yii = ya
R = sqrt(xii^2+yii^2+z(ii)^2);
sigprimeh = ((Q/(R*pi))*((3*z(ii)*xii^2)/R^3)*((R*(1-2*v))/(R+z(ii))));
sum = sum +sigprimeh;
end
end
sigma_h(ii) = sum;
end
plot(sigma_h,z)
title('Stress Due to Surcharge Load')
xlabel('\sigma`h (ksf)');
ylabel('Depth "z"(ft)');
set(gca,'YDir','reverse')
This will compute the accumulated sum for each "zii" value and store it in the "sigma_h" array, which can then be plotted against "z".
I hope this answers your question.
Regards,
Tushar

More Answers (0)

Categories

Find more on Visual Exploration 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!