How to make while loop faster ?
2 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
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
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.
See Also
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!