How can I speed up nested loops?

6 views (last 30 days)
Yeray Pabon
Yeray Pabon on 1 May 2020
Answered: Athul Prakash on 7 May 2020
Hey all,
I'm still learning MATLAB and I'm doing a program that does a parameter sweep using nested for loops. As shown in the code, it sweeps Thrust and ThrustAngle. The end result will be a table with all the possible solutions.
I'm currently trying to find ways of speeding up my code because eventually, I'll be adding more inner loops (like 4 more) which will significantly impact the calculation speed. I would appreciate if anyone could give me tips to accelerate these loops.
I had been looking at the parfor function but I'm not sure how do I prepare my code for that. I'm pretty sure it's not just changing the for to parfor.
I'm currently researching vectorization for nested loops but I'm still lost in how to convert it.
clc
clear
%% Constants
r1 = 4;
L1 = 8;
L2 = 8;
w = 5;
a = 7;
b = 3;
c = 11;
d = 11;
%% Loop parameters
ThrustMin = 0;
ThrustStep = 1;
ThrustMax = 5;
ThrustAngleMin = 90;
ThrustAngleStep = 10;
ThrustAngleMax = 180;
%% Preallocation
NumThrust = numel(ThrustMin:ThrustStep:ThrustMax);
NumThrustAngle = numel(ThrustAngleMin:ThrustAngleStep:ThrustAngleMax);
NumIterations = NumThrust*NumThrustAngle;
T = zeros(NumIterations,2);
%% Start loops
i = 0;
tic
for Thrust = ThrustMin:ThrustStep:ThrustMax
for ThrustAngle = ThrustAngleMin:ThrustAngleStep:ThrustAngleMax
i = i + 1;
%% Solve using mathematical model
WDistanceM0 = sqrt(a^2 + b^2);
WAngleM0 = atand(a/b);
FDistanceM0 = sqrt(c^2 + d^2);
FAngleM0 = (atand(c/d) - 90 + (ThrustAngle)) + 180;
WDistanceM2 = sqrt((L1 + r1 - a)^2 + b^2);
WAngleM2 = atand((L1 + r1 - a)/b);
FDistanceM2 = sqrt((L1 + r1 - c)^2 + d^2);
FAngleM2 = (atand((L1 + r1 - c)/d) - 90 - (ThrustAngle));
A = [0 0 1 1;
1 1 0 0;
r1 (r1+L1) 0 0;
L1 0 0 0];
B = [Thrust*cosd(ThrustAngle);
w - Thrust*sind(ThrustAngle);
w*WDistanceM0*sind(WAngleM0) + Thrust*FDistanceM0*sind(FAngleM0);
w*WDistanceM2*sind(WAngleM2) + Thrust*FDistanceM2*sind(FAngleM2)];
X = pinv(A)*B;
Fr1 = X(1,1);
Fr2 = X(2,1);
Ff1 = X(3,1);
Ff2 = X(4,1);
%% Insert solutions to a table
T(i,1) = Thrust;
T(i,2) = ThrustAngle;
T(i,3) = Fr1;
T(i,4) = Fr2;
T(i,5) = Ff1;
T(i,6) = Ff2;
end
end
toc
%% End loop and show solutions
T
Thank you for all your help in advance!

Answers (1)

Athul Prakash
Athul Prakash on 7 May 2020
Hi Yeray,
I have a few suggestions for this vectorization,
In the calculation of 'A' and 'B', the functions sind and cosd can work simulatneously on vectors. So passing all the Thrust values and ThrustAngle values as a vector should help you here.
temp = Thrust_vec .* sind(Thrust_angle)
Notice how all 'Thrust' values that are iterated have been collected in a single vector 'Thrust_vec'. Similarly, you may vectorize all your other variables as well.
Please refer to rehape and permute functions, which you may often need while looking to vectorize your code.
Further, please read this doc on how to modify your code for 'parfor':
Hope it Helps!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!