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

댓글 수: 3

Rik
Rik 2020년 9월 9일
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.
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 2020년 9월 9일
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.

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

 채택된 답변

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020년 9월 6일

0 개 추천

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);

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

제품

릴리스

R2020a

태그

질문:

2020년 9월 6일

댓글:

Rik
2020년 9월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by