MATLAB Answers

Pre-allocation issue non existent before

2 ビュー (過去 30 日間)
Manyu Hati
Manyu Hati 2021 年 4 月 15 日
編集済み: Manyu Hati 2021 年 4 月 15 日
Dear Matlabers,
I would like to solve a pre-allocation problem in my matlab code. This code used to run in 2013 without issues. However, I have tried to run it again, and now Matlab points out that the code has a pre-allocation issue. Please, find attached the data files and the code below to run the simulation. I have tried to fix it by pre-allocating Ac_saving=zeros(100); and Avg_saving=zeros(100), without luck, as Matlab says that the index exceeds the number of elements. Could you please help me to repair the code? Many thankss
data_pv_pu=dlmread('pv.txt');
t=data_pv_pu(:,1);
K=1;
P_pv_nominal=1500;
P_pv=K*P_pv_nominal;
data_pv=data_pv_pu(:,2)*P_pv;
data_load_0=dlmread('load_v.txt');
data_load=data_load_0(:,2);
V=400
Sum_without0=0
Sum_FV0=0
Sum_without=Sum_without0
Ac_saving=0;
Avg_saving=0;
K=1
for x=-99:100
for h=2:8760
P=data_load(h);
P_FV=data_pv(h)*K;
I=P/V;
I_FV=P_FV/V;
for r=1:100
Sum_without=Sum_without+(I*r)^2;
end
Sum_FV=Sum_FV0;
if x>=1
for r=1:100
if r<x
Sum_FV=Sum_FV+(I*r)^2;
else
Sum_FV=Sum_FV+((I*r)-I_FV)^2;
end
end
else
for r=1:100
Sum_FV=Sum_FV+((I*r)-I_FV)^2;
end
Sum_FV=Sum_FV-(x-1)*(I_FV)^2;
end
Ahorro=(Sum_without-Sum_FV)/Sum_without;
Ac_saving=Ac_saving+Ahorro; %
Ahorro=0;
Sum_without=0;
Sum_FV=0;
end
Avg_saving (101-x)=Ac_saving/8760;
Ac_saving=0;
end
plot(Avg_saving)
set(gcf,'Visible','on')
hold on

回答 (1 件)

SungJun Cho
SungJun Cho 2021 年 4 月 15 日
First of all, when I ran your code, Avg_saving outputs 1 x 200 double array, so you might want to preallocate the arrays in right size.
Based on your code, it seems like you want to preallocate the arrays. If that is the case, try using
Ac_saving=zeros(1,100);
Avg_saving=zeros(1,100);
instead of just zeros(100), since the latter will give you 100 x 100 matrix instead of 1 x 100 vector array.
Hope this helps.
  1 件のコメント
Manyu Hati
Manyu Hati 2021 年 4 月 15 日
Thank you. I finally pre-allocated the matrices correctly and the indices accordingly. Now, I find no warnings or errors in the console when compiling. However, when I run the code, it takes so much time, and I think it is getting stucked. I try to find the evaluating line but with no success. Any advice on this? When the simulation is finished, I see a warning called: 'rank deficient'.
Below you have the new code and the attached txt files for data.
data_pv_pu=dlmread('pv.txt');
t=data_pv_pu(:,1);
K=1;
P_pv_nominal=1500;
P_pv=K*P_pv_nominal;
data_pv=data_pv_pu(:,2)*P_pv;
data_load_0=dlmread('load_v.txt');
data_load=data_load_0(:,2);
figure(1);
plot(t,data_load,t,data_pv);
legend('load','pv');
hold on;
set(gcf,'Visible','on')
V=400
Sum_sin0=zeros(199,1);
Sum_FV0=zeros(199,1);
Sum_sin=Sum_sin0;
Ac_saving=zeros(199,1);
Avg_saving=zeros(199,1);
K=25;
for x=-99:100
for h=1:8760
P=data_load(h);
P_FV=data_pv(h)*K;
I=P/V;
I_FV=P_FV/V;
for r=1:100
Sum_sin=Sum_sin+(I*r)^2;
end
Sum_FV=Sum_FV0;
if x>=1
for r=1:100
if r<1
Sum_FV=Sum_FV+(I*r)^2;
else
Sum_FV=Sum_FV+((I*r)-I_FV)^2;
end
end
else
for r=1:100 %
Sum_FV=Sum_FV+((I*r)-I_FV)^2;
end
Sum_FV=Sum_FV-(x-1)*(I_FV)^2;
end
Ahorro=(Sum_sin-Sum_FV)/Sum_sin;
Ac_saving=Ac_saving+Ahorro;
Ahorro=zeros(199,1);
Sum_sin=zeros(199,1);
Sum_FV=zeros(199,1);
end
Avg_saving(101-x)=Ac_saving(101-x)/8760;
Ac_saving=zeros(199,1);
end
plot(Avg_saving)
set(gcf,'Visible','on')
hold on

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by