How to make while loop faster ?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!