Consider Preallocating for Speed

1 Ansicht (letzte 30 Tage)
Tony Rankin
Tony Rankin am 21 Mai 2021
Kommentiert: Stephen23 am 22 Mai 2021
I have the following code and I am being informed that I can "preallocate for speed". When attempting to set up an array which I think is the solution to this issue, I am told "Index exceeds the number of array elements (1)".
What is the correct array to use for the code?
A= 2; %area (m^2)
T = 0.1; %tau, integral time constant (min)
h=0.001; %stepsize
t_final= 100; %the total time (min)
N=t_final/h;
Kc1 = 1; %p
roportional gain, m^2/min
t1(1)=0; %Kc=1 at 0 minutes
x1(1)=0; %deviation of liquid height (difference between actual height and desired height)
v1(1)=2; %derivative of change in height with respect to time
for i=1:N
t1(i+1)=t1(i)+h;
x1(i+1)=x1(i)+h*(v1(i));
v1(i+1)=v1(i)+h*((-Kc1*v1(i)/A)-(Kc1/(T*A))*x1(i));
end
Kc2 = 2;
t2(1)=0;
v2(1)=2;
x2(1)=0;
for i=1:N
t2(i+1)=t2(i)+h;
x2(i+1)=x2(i)+h*(v2(i));
v2(i+1)=v2(i)+h*((-Kc2.*v2(i)/A)-(Kc2./(T*A))*x2(i));
end
Kc3 = 3;
t3(1)=0;
v3(1)=2;
x3(1)=0;
for i=1:N
t3(i+1)=t3(i)+h;
x3(i+1)=x3(i)+h*(v3(i));
v3(i+1)=v3(i)+h*((-Kc3.*v3(i)/A)-(Kc3./(T*A))*x3(i));
end
Kc4 = 4;
t4(1)=0;
v4(1)=2;
x4(1)=0;
for i=1:N
t4(i+1)=t4(i)+h;
x4(i+1)=x4(i)+h*(v4(i));
v4(i+1)=v4(i)+h*((-Kc4.*v4(i)/A)-(Kc4./(T*A))*x4(i));
end
Kc5 = 5;
t5(1)=0;
v5(1)=2;
x5(1)=0;
for i=1:N
t5(i+1)=t5(i)+h;
x5(i+1)=x5(i)+h*(v5(i));
v5(i+1)=v5(i)+h*((-Kc5.*v5(i)/A)-(Kc5./(T*A))*x5(i));
end
Kc75 = 7.5;
t6(1)=0;
v6(1)=2;
x6(1)=0;
for i=1:N
t6(i+1)=t6(i)+h;
x6(i+1)=x6(i)+h*(v6(i));
v6(i+1)=v6(i)+h*((-Kc75.*v6(i)/A)-(Kc75./(T*A))*x6(i));
end
Kc10 = 10;
t7(1)=0;
v7(1)=2;
x7(1)=0;
for i=1:N
t7(i+1)=t7(i)+h;
x7(i+1)=x7(i)+h*(v7(i));
v7(i+1)=v7(i)+h*((-Kc10.*v7(i)/A)-(Kc10./(T*A))*x7(i));
end
Kc20 = 20;
t8(1)=0;
v8(1)=2;
x8(1)=0;
for i=1:N
t8(i+1)=t8(i)+h;
x8(i+1)=x8(i)+h*(v8(i));
v8(i+1)=v8(i)+h*((-Kc20.*v8(i)/A)-(Kc20./(T*A))*x8(i));
end
  1 Kommentar
Stephen23
Stephen23 am 22 Mai 2021
Duplicating code like that is a very strong code smell
and seems to be caused by forcing meta-data into the variable names (Kc1, Kc2, Kc5, Kc10, Kc20).
To simplify your code keep the meta-data in a separate array and use indexing rather than separate variables. Then you can trivially avoid duplicating code like that.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Daniel Bengtson
Daniel Bengtson am 21 Mai 2021
Bearbeitet: Daniel Bengtson am 21 Mai 2021
You just need to initialize your vectors before entering the for loops. Something like the below sample would satisfy the preallocation for the kc1 case. You'd have to do the same for the other loops as well.
A= 2; %area (m^2)
T = 0.1; %tau, integral time constant (min)
h=0.001; %stepsize
t_final= 100; %the total time (min)
N=t_final/h;
Kc1 = 1; %proportional gain, m^2/min
%% preallocate vectors of zeros
t1 = zeros(N+1,1);
x1 = zeros(N+1,1);
v1 = zeros(N+1,1);
t1(1)=0; %Kc=1 at 0 minutes
x1(1)=0; %deviation of liquid height (difference between actual height and desired height)
v1(1)=2; %derivative of change in height with respect to time
for i=1:N
t1(i+1)=t1(i)+h;
x1(i+1)=x1(i)+h*(v1(i));
v1(i+1)=v1(i)+h*((-Kc1*v1(i)/A)-(Kc1/(T*A))*x1(i));
end

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by