How to make while loop faster ?

조회 수: 5 (최근 30일)
khaled elbatawy
khaled elbatawy 2020년 9월 18일
댓글: Star Strider 2020년 9월 18일
Hello all ,
I have so simple while loop to sum , and it is taking really so long time , and i do not know if it is normal to take this time or not
D_1 = 1.5e-13;
D = 0;
n = 0;
n_cyc = [0.500000000000000 15.5000000000000 1 1 1 1 1 1 1 1 1 1 1 0.500000000000000 0.500000000000000];
while D < 1
% D factor
D = D +D_1; % total damage
n= n + sum(n_cyc); % sum of cyclic Values for all the Blocks till the Fracture
end
Thank you for any helping

답변 (1개)

Star Strider
Star Strider 2020년 9월 18일
First, the number of iterations is going to be:
n_iter = 1/D_1
evaluating to:
n_iter =
6.666666666666667e+12
That is going to take a while.
I do not understand the reason for the loop anyway.
Unless I am missing something, at the end of the loop:
n = sum(n_cyc) * n_iter
or:
n =
1.866666666666667e+14
So I would simply do that one multiplication and be done with it!
  댓글 수: 2
khaled elbatawy
khaled elbatawy 2020년 9월 18일
Thank you for your answer .
Yes i know that it could be in this way but the loop should have more values and Calculation .
The whole picture of what i am generating force signal as sinus function and calculate of it Stresses and then Calculate from that The Damage factor D and in the loop if it is not reaching 1 yet then add another signal or addition Signal of force and recalculate
I wanted to make it in this way so i was hoping if there is possiblity or another way to succeed that
Star Strider
Star Strider 2020년 9월 18일
My pleasure!
I would not use such a small step (1.5e-13) initially. Use larger steps, determine when the failure occurs, then use those limits in subsequent runs of the while loop with progressively smaller steps over a smaller total interval to determine more precisely when the failure occurs.
It still might be more efficient to use other approaches than a while loop, however since I have no idea what you are doing, I have no idea what to suggest.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by