any ideas for making code faster?
Show older comments
clc;
clear all;
R = [0.004 0.0057 0.005 0.005 0.0045 0.0044 0.005];
X = [0.05 0.0714 0.0563 0.045 0.0409 0.05 0.05];
L = [100 70 80 100 110 90 100]; %KM
for a = 1:7
Z1(1,a) = ((R(1,a) + i*(X(1,a))))*L(1,a);
end
z1 = abs(Z1); %begining of line
z2 = abs(Z1); %end of line
z = [z1 z2]; %Z tottal
Z2 = Z1;
Z = [Z1 Z2];
ctr1 = [240 240 160 240 240 240 160];
ctr2 = [240 160 240 240 240 240 160];
ctr = [ctr1 ctr2];
ptr = (150*1000)/110;
tetta1 = angle(Z1);%begining of line
tetta2 = angle(Z1);%end of line
tetta = [tetta1 tetta2];
z_final = z * 0.8;
for i= 1:14
ZZ1(1,i) = ((z_final(1,i))/(cos(tetta(1,i) - (pi/4)))*(ctr(1,i)/ptr));
end
L2 = [14 2 50 35; 3 0 40 0; 4 0 50 0;5 0 55 0; 7 6 50 45; 1 0 50 0; 2 8 35 50; 13 0 45 0; 14 8 50 50; 9 0 35 0; 10 0 40 0; 11 0 50 0; 7 12 50 55; 6 12 45 55]; %
% backup1 backup2 distance1 distance2
A = zeros (14,14);
for i = 1:14
A(i,L2(i,1)) = Z(1,L2(i,1))/2;
if L2(i,2) ~= 0
A(i,L2(i,2)) = Z(1,L2(i,1))/2;
else
end
end
for i = 1:14
if L2(i,2) == 0
z2new(1,i) = A(i,L2(i,1));
elseif L2(i,3) <= L2(i,4);
z2new(1,i) = A(i,L2(i,1));
else
L2(i,3) >= L2(i,4);z2new(1,i) = A(i,L2(i,2));
end
end
ZZ2_2 = Z + z2new;
z2_final = abs(ZZ2_2);
tetta3 = angle(ZZ2_2);
for i= 1:14
ZZ2(1,i) = ((z2_final(1,i))/(cos(tetta3(1,i) - (pi/4)))*(ctr(1,i)/ptr));
end
Z22new = z2new * 2;
L3 = [3 80; 4 100; 5 110; 6 90; 1 100; 2 70; 2 70; 7 100; 13 90; 8 100; 9 70; 10 80; 2 70; 1 100];
for i = 1:14
z3new(1,i) = (Z(1,L3(i,1)))*(2/5);
end
ZZ_3 = (Z + Z22new) + z3new;
Z3_final = abs(ZZ_3);
tetta4 = angle(ZZ_3);
for i= 1:14
ZZ3(1,i) = ((Z3_final(1,i))/(cos(tetta4(1,i) - (pi/4)))*(ctr(1,i)/ptr));
end
for i=1:14
cti2(1,i) = 0.3;
cti3(1,i) = 0.6;
end
backup = [6 1; 13 7; 7 2; 14 6; 8 13; 5 6; 9 14; 4 5;
10 9; 3 4; 2 3; 11 10; 1 2; 12 11]; % column2==0>>one relay backub
backup1 = [13 12; 7 8; 14 12; 5 7; 9 8; 1 14];
for i = 1:14
if ZZ2(1,backup(i,1)) > ZZ2(1,backup(i,2))
cti2(1,backup(i,1)) = cti2(1,backup(i,1)) + 0.3;
end
if ZZ3(1,backup(i,1)) > ZZ3(1,backup(i,2))
cti3(1,backup(i,1)) = cti3(1,backup(i,1)) + 0.3;
end
end
for i = 1:6
if cti2(1,backup1(i,1)) == 0.3
if ZZ2(1,backup1(i,1)) > ZZ2(1,backup1(i,2))
cti2(1,backup1(i,1)) = cti2(1,backup1(i,1)) + 0.3;
end
end
if cti3(1,backup1(i,1)) == 0.6
if ZZ3(1,backup1(i,1)) > ZZ3(1,backup1(i,2))
cti3(1,backup1(i,1)) = cti3(1,backup1(i,1)) + 0.3;
end
end
end
How Can i make my code faster and shorter?any ideas ?time of execution is really important to me
5 Comments
Walter Roberson
on 24 Jun 2022
Pre-allocate your output variables for one thing.
Use logical indexing instead of loops.
Walter Roberson
on 24 Jun 2022
Please format your code to be readable.
Your previous version was more readable.
arian hoseini
on 25 Jun 2022
Dyuman Joshi
on 25 Jun 2022
Example -
%Replace this
for a = 1:7
Z1(1,a) = ((R(1,a) + i*(X(1,a))))*L(1,a);
end
%with
Z1=(R+i*X).*L
DGM
on 25 Jun 2022
This looks like a bug.
for i = 1:14
if L2(i,2) == 0
z2new(1,i) = A(i,L2(i,1));
elseif L2(i,3) <= L2(i,4);
z2new(1,i) = A(i,L2(i,1));
else % is this supposed to be an elseif statement?
L2(i,3) >= L2(i,4); % this test does nothing
z2new(1,i) = A(i,L2(i,2));
end
end
What's the intended behavior?
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing 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!