How to make while loop faster ?

4 views (last 30 days)
khaled elbatawy
khaled elbatawy on 18 Sep 2020
Commented: Star Strider on 18 Sep 2020
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

Answers (1)

Star Strider
Star Strider on 18 Sep 2020
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 Comments
khaled elbatawy
khaled elbatawy on 18 Sep 2020
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 on 18 Sep 2020
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!