How do I save the output array (y2) for each loop?

조회 수: 2(최근 30일)
Lauren Michaud
Lauren Michaud 2021년 10월 17일
답변: Lauren Michaud 2021년 10월 17일
How do I save the output array (y2) for each loop?
At the end, I would like a 3x10 array stored in the variable ysum. Where each row is the unique calculation based on the "c" count.
I've been going at this for 6 hours and can't seem to get it.
Thanks in advanced.
Fmultiple = input('Enter multiple concentrated forces [N] in array format: ');
amultiple = input(sprintf('Enter multiple locations (0 - %0.0f) [m]: ', L));
z = length(amultiple); % this is the length of the amultiple user vector input
% a new blank vector must be created to which outputs in the loops will be
% appended
ysum = [];% this will store all the values of the deflection calculations
c = 0;
for i = 1:z; % this loop from spot 1 of the array inputed all the way to the end
a = amultiple(i) % indexes i for amultiple array
F = Fmultiple(i) % indexes i for Fmultiple array
c = c + 1
y = [] % reset y each round
I = (w .* (h .^3)) ./ 12;
R = (F ./ L) .* (L - a);
theta = ((F .* a) ./ (6 .* E .* I .* L)) .* ((2 .* L) - a) .* (L - a);
if c > 0
y1 = [];
y2 = [];
x = 0:L;
for n = 1:length(x);
if x(n) <= a
def = (-1 .* theta .* x(n)) + ((R * x(n) .^3) ./ (6 .* E .* I));
y1 = [y1, def];
else
def = (-1 .* theta .* x(n)) + ((R * x(n) .^3) ./ (6 .* E .* I)) - ((F ./ (6 .* E .* I)) .* ((x(n) - a) .^3));
y2 = [y1, def];
ysum %<=== %MARKED
end
end
end
end

채택된 답변

Lauren Michaud
Lauren Michaud 2021년 10월 17일
I was able to get help that lead to a solution.
For completeness, i'm posting it here.
loop = 1;
while loop == 1
% Task 4
% what is the objective of this for loop
% for multiple forces, each element of the array is it's own point
% for for F = [800, 350, 200] a = [3, 5, 8]; 800 is the F, 3 is the a
% the output of this array will be concatenated with the next round which
% will be F = 350, a = 5
% lastly F = 200, a = 8
% this will yield 3 rows
% two for loops will be needed
Fmultiple = input('Enter multiple concentrated forces [N] in array format: ');
amultiple = input(sprintf('Enter multiple locations (0 - %0.0f) [m]: ', L));
z = length(amultiple); % this is the length of the amultiple user vector input
% a new blank vector must be created to which outputs in the loops will be
% appended
%c = 0;
%ysum = zeros(1,L) % create a zero table that is the size of the expected array
Msum = 0;
for i = 1:z; % this loop from spot 1 of the array inputed all the way to the end
a = amultiple(i); % indexes i for amultiple array
F = Fmultiple(i); % indexes i for Fmultiple array
%c = c + 1
M = []; % reset y each round
I = (w .* (h .^3)) ./ 12;
R = (F ./ L) .* (L - a);
theta = ((F .* a) ./ (6 .* E .* I .* L)) .* ((2 .* L) - a) .* (L - a);
%if c > 0
% y = []
x = linspace(0,L,1000);
for n = 1:length(x)
if x(n) <= a
def = (-1 .* theta .* x(n)) + ((R * x(n) .^3) ./ (6 .* E .* I));
M = [M, def];
else
def = (-1 .* theta .* x(n)) + ((R * x(n) .^3) ./ (6 .* E .* I)) - ((F ./ (6 .* E .* I)) .* ((x(n) - a) .^3));
M = [M, def];
end
end
Msum = (Msum + M);
% ysum(c,:) = y
% end
end
MaxMin = abs(min(Msum))*1000; % finds the greatest minimum value and takes the absolute value of it
fprintf('The maximum deflection of the beam is %0.3f [mm].', MaxMin);
figure(2)
plot(x,(Msum.*1000));
x = linspace(0,L);
grid on;
xlabel('Length of the Beam [m]')
ylabel('Deflection [mm]')
title(sprintf('Deflection of %s undermultiple concentrated loads',Material(material_task1)))
loop = menu('Do you wish to repeat multiple loads calculation?', "Yes", "No");
end

추가 답변(1개)

Walter Roberson
Walter Roberson 2021년 10월 17일
Under what circumstance could c be <= 0 such that the if would fail ?
amultiple = input(sprintf('Enter multiple locations (0 - %0.0f) [m]: ', L));
That tells us that L is a length in meters.
x = 0:L;
That tells us that L is an integer count, not a fractional length.
for n = 1:length(x);
The way you constructed x, x is always going to be integers, and n is always going to be x+1
if x(n) <= a
Why bother with x there when you could just use n? If n<=a+1 ?
ysum %<=== %MARKED
could you confirm that should only be assigned into if the if fails?
I would like a 3x10 array stored in the variable ysum
Where does the 3 come from? Where does the 10 come from?
Your a values are obviously expected to be different. Why would you expect the same number of columns of output for the different a values ?
  댓글 수: 1
Lauren Michaud
Lauren Michaud 2021년 10월 17일
Thanks for your quick response.
I used
c > 0
just as a trigger to make sure that the if statement runs. It's not meant to fail - but always run
"L" is indeed a length in meters. This length can change and it's not hard coded, so this code permits "L" to vary.
for n = 1:length(x)
performs the loop for
x = 0:L
this was simplified from the original
x = 0:0.1:L
So it evaluates the equations in the loop at 0.1 increments the entire distance of L.
I chose a 3x10 array because the loop will output a 1x10 array, but it will do it a total of 3 times as it moves along user inputed array which was 1x3. I made it simple so I can debug a 1x10 array vs a 1x1000 array
After some thought, I have somewhat figure out a solution
ysum(c,:) = y2
The problem is the above code runs when y2 is a 1x5 array and not yet a 1x10 array, and so i'm getting an error
Error: Unable to perform assignment because the size of the left side is 1-by-10 and the size of the right side is 1-by-5.
The goal is for the output in the for loop to be saved each time and not overwrite the previous loop.
There should be 3 unique arrays of 1 x 10 dimension.
Here is the full code.
loop = 1;
while loop == 1
% Task 4
% what is the objective of this for loop
% for multiple forces, each element of the array is it's own point
% for for F = [800, 350, 200] a = [3, 5, 8]; 800 is the F, 3 is the a
% the output of this array will be concatenated with the next round which
% will be F = 350, a = 5
% lastly F = 200, a = 8
% this will yield 3 rows
% two for loops will be needed
Fmultiple = input('Enter multiple concentrated forces [N] in array format: ');
amultiple = input(sprintf('Enter multiple locations (0 - %0.0f) [m]: ', L));
z = length(amultiple); % this is the length of the amultiple user vector input
% a new blank vector must be created to which outputs in the loops will be
% appended
c = 0;
ysum = zeros(z,L) % create a zero table that is the size of the expected array
for i = 1:z; % this loop from spot 1 of the array inputed all the way to the end
a = amultiple(i) % indexes i for amultiple array
F = Fmultiple(i) % indexes i for Fmultiple array
c = c + 1
y = [] % reset y each round
I = (w .* (h .^3)) ./ 12;
R = (F ./ L) .* (L - a);
theta = ((F .* a) ./ (6 .* E .* I .* L)) .* ((2 .* L) - a) .* (L - a);
if c > 0
y1 = [];
y2 = [];
x = 0:L;
for n = 1:length(x);
if x(n) <= a
def = (-1 .* theta .* x(n)) + ((R * x(n) .^3) ./ (6 .* E .* I));
y1 = [y1, def];
else
def = (-1 .* theta .* x(n)) + ((R * x(n) .^3) ./ (6 .* E .* I)) - ((F ./ (6 .* E .* I)) .* ((x(n) - a) .^3));
y2 = [y1, def];
end
end
end
ysum(c,:) = y2
end
loop = menu('Do you wish to repeat multiple loads calculation?', "Yes", "No");
end
Thanks again for your help. Hope I made this clear.
I've been working on this little piece of script for 12 hours now. 1 hour per character. lol
ysum(c,:) = y2

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by