# computation takes more time at each iteration

4 views (last 30 days)
Hello felows,
I have a continiouse simulation. ( discrete state space equation). I tr to compute the next state using the previous one. the problem is each time the loop takes more time compare to the previouse one.
I try to plot the output every 10000 itterations. The first time takes 1 second, the second 2 and it increase
%% main code
close all
clc
clear all
global Ts
Ts=1e-7;
global r_i L_i C_i R_l L_l L_g r_g i_L_i i_C_i i_L_g i_L_l
global Zeq Zeq_2 X_eq R_eq Z_i Z_g Z_L i_Z_i i_Z_g i_Z_L
global omega delta count Wn
global V_g V_i
global I_L_i_o V_C_i_o I_L_l_o I_L_g_o
global t D pi_2_3 Brk
Brk=1;
pi_2_3=(2*pi/3);
Wn=120*pi;
I_L_i_o=[0 0 0]; V_C_i_o=[0 0 0];I_L_l_o=[0 0 0];I_L_g_o=[0 0 0];
[r_i, L_i ,C_i ,R_l, L_l, L_g ,r_g]=initi_va();
i_L_i=1/L_i;
i_C_i=1/C_i;
i_L_g=1/L_g;
i_L_l=1/L_l;
X_eq=120*pi*(L_i+L_g);
R_eq=(r_i+r_g);
Zeq_2=(X_eq)^2+(R_eq)^2;
Zeq=sqrt(Zeq_2);
Z_i=r_i+i*(Wn*L_i);
Z_g=r_g+i*(Wn*L_g);
Z_L=1/((1/R_l)+i*(Wn*C_i));
i_Z_i=Z_i^-1;
i_Z_g=Z_g^-1;
i_Z_L=Z_L^-1;
delta=0;
D.delta=0
T_end=.06;
omega=120*pi;
count=1;
t=0;
tic
while t(count)<T_end
D.E_abs(count)=111;
D.V_abs(count)=110;
if delta<.001
delta=delta+1e-5;
end
D.delta(count)=delta;
three_phase_version();
t(count+1)=t(count)+Ts;
image_show_c()
count=count+1;
end
t(end)=[];
%% Plotting
close all
figure
subplot(2,1,1)
plot(t,D.P2G)
title("P")
subplot(2,1,2)
plot(t,D.Q2G)
title("Q")
figure
subplot(2,1,1)
plot(t,D.V_g(:,1))
title("V_G")
subplot(2,1,2)
plot(t,D.V_i)
title("V_i")
figure
plot(t,D.I_L_i)
title("I_L_I")
figure
plot(t,D.I_L_g)
title("I_L_G")
figure
subplot(2,1,1)
plot(t,D.delta)
title("delta")
subplot(2,1,2)
%plot(t,D.Q2G)
title("--")
%% Functions
function [I_L_i, V_C_i ,I_L_l ,I_L_g ]=...
next_s(V_i, V_g ,Brk ,I_L_i_o, V_C_i_o ,I_L_l_o ,I_L_g_o)
global r_i L_i C_i R_l L_l L_g r_g Ts
global i_L_i i_C_i i_L_g i_L_l
if (C_i~=0 && L_g~=0 )
I_C=I_L_i_o-I_L_l_o-(V_C_i_o/R_l)-I_L_g_o;
V_C_i=(Ts*i_C_i)*I_C+V_C_i_o;
I_L_l=(Ts*i_L_l)*V_C_i_o+I_L_l_o;
I_L_i=(Ts*i_L_i)*(V_i-V_C_i_o-(r_i*I_L_i_o))+I_L_i_o;
if Brk==1
I_L_g=(Ts*i_L_g)*(V_C_i_o-V_g-I_L_g_o*r_g)+I_L_g_o;
else
I_L_g=0;
end
end
if (C_i~=0 && L_g==0)
I_C=I_L_i_o-I_L_l_o-(V_C_i_o/R_l)-Brk*((V_C_i_o-V_g)/r_g);
V_C_i=(Ts/C_i)*I_C+V_C_i_o;
I_L_l=(Ts/L_l)*V_C_i_o+I_L_l_o;
I_L_i=(Ts/L_i)*(V_i-V_C_i_o-(r_i*I_L_i_o))+I_L_i_o;
if Brk==1
I_L_g=(V_C_i_o-V_g)/r_g;
else
I_L_g=0;
end
end
if (C_i==0 && L_g~=0 )
V_C_i=(I_L_i_o-I_L_l_o-I_L_g_o)*R_l;
I_L_l=(Ts/L_l)*V_C_i+I_L_l_o;
I_L_i=(Ts/L_i)*(V_i-V_C_i-(r_i*I_L_i_o))+I_L_i_o;
if Brk==1
I_L_g=(Ts/L_g)*(V_C_i-V_g-I_L_g_o*r_g)+I_L_g_o;
else
I_L_g=0;
end
end
if (C_i==0 && L_g==0 )
V_C_i=V_g+(I_L_i_o-I_L_l_o)*r_g;
I_L_l=(Ts/L_l)*V_C_i+I_L_l_o;
I_L_i=(Ts/L_i)*(V_i-V_C_i-(r_i*I_L_i_o))+I_L_i_o;
if Brk==1
I_L_g=(Ts/L_g)*(V_C_i-V_g-I_L_g_o*r_g)+I_L_g_o;
else
I_L_g=0;
end
end
% V_C_i=(Ts/C_i)*(I_L_i_o-I_L_l_o-(V_C_i_o/R_l))+V_C_i_o;
% I_L_i=(Ts/L_i)*(V_i-V_C_i_o-(r_i*I_L_i_o))+I_L_i_o;
% I_L_l=(Ts/L_l)*V_C_i_o+I_L_l_o;
% I_L_i=vpa(I_L_i,4);
% V_C_i=vpa(V_C_i,4);
% I_L_l=vpa(I_L_l,4);
% I_L_g=vpa(I_L_g,4);
end
function [y , err]=PI(ref,x,y_o,Kp, Ki,Lim_n,Lim_p,ff)
err=(ref-x); %err
err = err*Kp; %kp
temp_P= err;
err = err*Ki; %ki
temp_I=y_o+err; %int
y=temp_P+temp_I;
if y>Lim_p
y=Lim_p;
end
if y<Lim_n
y=Lim_n;
end
end
function [d_w_n, delta]=VSG(d_p,d_w_o,delta_o)
global Ts
Dp=5000; %droop
J=0.1; %Moment of Inertia
d_w_n=d_w_o+((d_p-d_w_o*Dp)/J)*Ts;
delta=delta_o+d_w_o*Ts;
end
function [P,Q] = PQ_comput(Vabc,Iabc)
P=Vabc*Iabc';
Q=(1/sqrt(3))*((Vabc(2)-Vabc(3))*Iabc(1)+(Vabc(3)-Vabc(1))*Iabc(2)+(Vabc(1)-Vabc(2))*Iabc(3));
end
function [r_i, L_i, C_i, R_l, L_l, L_g ,r_g]=initi_va()
r_i=10e-3;
L_i=20e-6;
C_i=1e-6; % Do not define zero with R_l=inf
L_g=20e-6;
r_g=10e-3;
R_l=100; % Do not define inf with C_i=0
L_l=100;
end
function three_phase_version()
global count omega Brk
global I_L_i_o V_C_i_o I_L_l_o I_L_g_o
global V_g V_i
global t delta D pi_2_3
for j=1:3
V_g=110*sqrt(2)*sin((omega*t(count))+((j-2)*pi_2_3));
V_i=110*sqrt(2)*sin(omega*t(count)+((j-2)*pi_2_3)+D.delta(count));
D.V_g(count,j)=V_g;
D.V_i(count,j)=V_i;
D.I_L_i(count,j)=I_L_i_o(j);
D.V_C_i(count,j)=V_C_i_o(j);
D.I_L_l(count,j)=I_L_l_o(j);
D.I_L_g(count,j)=I_L_g_o(j);
[I_L_i_o(j), V_C_i_o(j) ,I_L_l_o(j) ,I_L_g_o(j) ]=...
next_s(V_i, V_g ,Brk ,I_L_i_o(j), V_C_i_o(j) ,I_L_l_o(j) ,I_L_g_o(j));
end
[D.P2G(count),D.Q2G(count)]=PQ_comput(D.V_i(count,:),D.I_L_i(count,:));
end
global count
global X_eq R_eq Zeq_2
global i_Z_i i_Z_g i_Z_L
global delta D
%{
E=D.E_abs(count);
V=D.V_abs(count);
P=3*.5*(((E^2/Zeq_2)-((E*V*cos(delta))/Zeq_2))*R_eq+(E*V*X_eq*sin(delta))/Zeq_2);
Q=3*.5*(((E^2/Zeq_2)-((E*V*cos(delta))/Zeq_2))*X_eq-(E*V*R_eq*sin(delta))/Zeq_2);
%}
E=D.E_abs(count)*(cos(delta)+i*sin(delta));
V=D.V_abs(count);
V_C_jw=(E*i_Z_i+V*i_Z_g)/(i_Z_i+i_Z_g+i_Z_L);
I_i=(E-V_C_jw)*i_Z_i;
I_g=(V_C_jw-V)*i_Z_g;
S_i=3*E*conj(I_i);
%S_g=3*V*conj(I_g);
P=real(S_i);
Q=imag(S_i);
end
function image_show_c()
global count t D
if rem(count,10000)==0
toc
display(t(count));
%close all
subplot(2,1,1)
plot(t(1:count),D.P2G(1:count))
title("P")
subplot(2,1,2)
plot(t(1:count),D.Q2G(1:count))
title("Q")
hold on
pause(1e-5)
tic
end
end
s. I tries to comment the plotting but it didn't help.

Stephen Cobeldick on 2 Jul 2019
Sepehr Saadatmand on 2 Jul 2019
Hi Stephen,
I put lots of time on what you said and it worked well. Now I am sharing my experience with others.
1. The main reason of the slow proceed was because of not preallocating my arrays and matrices and every time I extended the previous array it took lots of time.
2. I have not worked with code profiler and it was awesome
3. As you mentioned another reason was external variables.
Thanks a lot