How do I save the exact output of a for loop without changing any values?

3 commentaires

Rik
Rik le 9 Sep 2020
Why did you flag your own question as unclear? If you think it requires clarification you can edit it. If the answer below doesn't help you, you can post a comment.
Stephen23
Stephen23 le 9 Sep 2020
Original question by Liv Watson retrieved from Google Cache:
"Saving Output of for loop in array"
I'm trying to save the outputs of my for loop for the deflection of a beam, doing 98 iterations of the derived equation for a beam of L=1m and sections at x = 0.01:0.01:0.99. When I use the following code, the outputs of the for loop are the correct values (last value is -1.1933e-06).
%%Acting Forces on the beam and position
F1=(-4);
x1=0.4;
L=1;
%Constant EI Assumed 100 kN.m^2 (from Paper)
EI = 100;
for X = 0.01:0.01:0.99
D1 = ((F2*(L-x2)^2)/(2*EI)).*(L-X).*(((L-X)./(L-x2))-1/3.*((L-X)./(L-x2)).^2).*(X>x2) + (((X-x2)./(L-X)) + 2/3.*((L-x2)./(L-X))).*(x2>X);
disp(D1);
end
However, when I try to assign these output values to an array using the below code, it changes the results of my for loop drastically!! (the last value is around 7000 now)
%%Acting Forces on the beam (Fi) and their positions (xi)
F1 = (-4);
x1 = 0.4;
L = 1;
%Constant EI Assumed 100 kN.m^2 (from Paper)
EI = 100;
x = 0.01:0.01:0.99;
n = length(x);
%Assign space for D1
D1 = zeros(n,1);
%for deflection of beam caused by F1
for X = 1:n
D1(X) = ((F2*(L-x2)^2)/(2*EI)).*(L-X).*(((L-X)./(L-x2))-1/3.*((L-X)./(L-x2)).^2).*(X>x2) + (((X-x2)./(L-X)) + 2/3.*((L-x2)./(L-X))).*(x2>X);
end
disp(D1);
Can anyone spot what I'm doing wrong and suggest a way of fixing it.
Rik
Rik le 9 Sep 2020
Regarding the email you sent to me: you won't be kicked out of university for plagiarism if you don't pass off the solution you got here as your own work. Nothing in your question or the answer looks like plagiarism to me, so it is entirely up to you if you make it plagiarism yourself by copying the solution below and handing it in as your own work. So I'm sorry Liv, I'm not going to remove the backup that Stephen posted, nor am I going to edit the answer.
If you disagree, go ahead and post a comment with your explanation. You can also contact Mathworks support if you feel this is actual plagiarism (rising to the level of copyright infrigment) and not just a student panicking that they might be caught cheating on homework.

Connectez-vous pour commenter.

 Réponse acceptée

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam le 6 Sep 2020

0 votes

You can use this code:
%%Acting Forces on the beam and position
F2=(-4);
x2=0.4;
L=1;
%Constant EI Assumed 100 kN.m^2 (from Paper)
EI = 100;
D=[];
for X = 0.01:0.01:0.99
D1 = ((F2*(L-x2)^2)/(2*EI)).*(L-X).*(((L-X)./(L-x2))-1/3.*((L-X)./(L-x2)).^2).*(X>x2) + (((X-x2)./(L-X)) + 2/3.*((L-x2)./(L-X))).*(x2>X);
D=[D, D1];
%disp(D1);
end
disp(D)
or simply
F2=(-4);
x2=0.4;
L=1;
%Constant EI Assumed 100 kN.m^2 (from Paper)
EI = 100;
X = 0.01:0.01:0.99;
D1 = ((F2*(L-x2)^2)/(2*EI)).*(L-X).*(((L-X)./(L-x2))-1/3.*((L-X)./(L-x2)).^2).*(X>x2) + (((X-x2)./(L-X)) + 2/3.*((L-x2)./(L-X))).*(x2>X);
disp(D1);

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by