How to reduce run time of for loop
5 次查看(过去 30 天)
显示 更早的评论
%Material_data_kevlar;
K =4;
best_m = 0;
best_n = 0;
best_l = 0;
max_layers = K;
num_of_layup_req = [];
flag = false; % Initialize flag variable
while numel(num_of_layup_req) == 0
for m = 1:K
for n = 1:K
for l = 1:K
if rem(m,2) == 0 && rem(n,2) == 0 && rem(l,2) == 0
if (m + n +l) >= K
break;
end
end
if flag
break;
end
% end
end
end
end
max_stress_criteria = max(max(Max_stress_value))
%extract stress components from X
sigma_x = X(1, :);
sigma_y = X(2, :);
tau_12 = X(3, :);
figure('Name', 'Stress Distribution', 'NumberTitle', 'off');
% Plot sigma_x versus z
plot(sigma_x, z, 'DisplayName', '\sigma_x','LineStyle','-','LineWidth',2);
hold on;
% Plot sigma_y versus z
plot(sigma_y, z, 'DisplayName', '\sigma_y','LineStyle','-','LineWidth',2);
% Plot tau_12 versus z
plot(tau_12, z, 'DisplayName', '\tau_{12}','LineStyle','-','LineWidth',2);
xlabel('Stress (MPa)');
ylabel('z (m)');
%xlim([-5e8 5e8]);
% Add legend
legend('Location', 'best','FontSize',12);
% Set limits for y-axis
ylim([min(z) max(z)]);
title("[0_{m}/45_{n}/90_{l}]_{s}")
% Display the minimal stress and the corresponding angle
%disp(['Minimal Calculated Stress: ', num2str(min_stress)]);
disp(['Number of layers:',num2str(numel(L))]);
disp(['Orientation:',num2str(L)]);
disp(['Tsai-wu value:',num2str(Tsai_wu_value)]);
disp(['Thickness:',num2str(t*numel(L)*1000)]);
save("0+45+90")
0 个评论
采纳的回答
Jacob Mathew
2023-6-27
Hi dharmin,
Looking at the code in the for loop, you can try and precalculate the values of sind(L(i)) and cosd(L(i)) which will have some minor time savings than calculating it multiple times in the single row.
However, the first part of the code has 3 nested for loops i.e time complexity of O() which might be the reason why the code is taking so long to run. Consider some optimisation to the algorithm itself to improve the time.
0 个评论
更多回答(1 个)
Walter Roberson
2023-8-31
num_of_layup_req = [];
flag = false; % Initialize flag variable
while numel(num_of_layup_req) == 0
for m = 1:K
for n = 1:K
for l = 1:K
if rem(m,2) == 0 && rem(n,2) == 0 && rem(l,2) == 0
if (m + n +l) >= K
break;
end
end
if flag
break;
end
% end
end
end
end
Inside of the while loop you never change num_of_layup_req so the numel() of it cannot change. Therefore, if the while loop body starts running the first time, it will run infinitely after that -- unless, that is, there is a break at the level of the while loop.
You do have break inside the while loop, but the break statements are all inside for loops, and break only refers to the immediately enclosing for or while . You sometimes terminate the for l early, but you never terminate the for n or for m early and never terminate the while early. Therefore you have an infinite while loop.
You set the flag to false before the loop and you test whether the flag is true inside the loop, but the flag is never changed -- so it never changes from false to true. It is therefore useless to test it.
You can optimize the other break test by looping 2:2:K instead of 1:K . With 2:2:K then mod 2 will always be 0 and so the first if will always be true. You can do this because you never take any action other than testing flag (that cannot change) when any of the mod 2 come out as non-zero
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!