Why Conversion of 2D matrix to 3D matrix is giving wrong results??

1 view (last 30 days)
Rabia Zulfiqar on 21 May 2020
I have developed a code for 1 year which was simple and it was in 2D giving the right answers as well but when I tried to analyze it for more years by converting it into 3D matrix in which each page(layer) represents one year and here I have 9 years which means 9 layers in a 3D matrix.The matlab is not storing right values in 3D matrix.Below you can find my code and explanation.
Max_Cap=1.4;
per=0.7;
Code For 2D matrix
Ebat=zeros(24,365);
Egrid=zeros(24,365);
for a = 1:365
a1=b1(a,1); %size of b1,d1 and g1 is 365x1.
a2=d1(a,1);
a3=g1(a,1);
if a1~=a2
Ebat(a1)= Max_Cap*per;
Ebat(a2) = -Max_Cap*per;
Egrid(a1)=-Max_Cap*per/eta_charge ;
Egrid(a2)=Max_Cap*per*eta_discharge ;
end
if a1~=a2-1
Ebat (a1+1) = Max_Cap*(1-per);
Ebat (a2+1) = -Max_Cap*(1-per);
Egrid(a1+1)=-Max_Cap*(1-per)/eta_charge ;
Egrid(a2+1)=Max_Cap*(1-per)*eta_discharge ;
end
if a1==a2
Ebat(a3) = Max_Cap*per;
Ebat (a2) = -Max_Cap*per;
Egrid(a3)=-Max_Cap*per/eta_charge ;
Egrid(a2)=Max_Cap*per*eta_discharge ;
if a3~= a2-1
Ebat (a3+1) = Max_Cap*(1-per);
Ebat (a2+1) = -Max_Cap*(1-per);
Egrid(a3+1)=-Max_Cap*(1-per)/eta_charge ;
Egrid(a2+1)=Max_Cap*(1-per)*eta_discharge ;
end
if a3==a2-1
Ebat (a2+1) = 0;
Egrid(a2+1)=0 ;
end
end
end
For 3D matrix
here the Max capacity and per is changing for each year and Max_Capyearly and per have a size of 9x1. (because of 9 years) but the problem is I am not getting the right answers for Ebat and Egrid here. The problem is with the calling the indices in formulae to calculate Ebat and Egrid.The matlab is taking the last year value of Max_Capyearly i.e Max_Capyearly(9) insteadof taking the first year value of Max_Capyearly in year 1 of Ebat calculation while its not storing any values in the future years and the whole matrix remains as zero matrix for year 2 to 9.The code is below.
Ebat=zeros(24,365,9)
Egrid=zeros(24,365,9)
Max_Capyearly=[1.4,......,0.85]
for yy=1:9
for a = 1:365
a1=b2(a,1,yy); %sizes of b2,d2 and g2 are 365x1x9
a2=d2(a,1,yy);
a3=g2(a,1,yy);
if a1~=a2
Ebat(a1)= Max_Capyearly(yy)*per(yy) ;
Ebat(a2) = -Max_Capyearly(yy)*per(yy);
Egrid(a1)=-Max_Capyearly(yy).*per(yy)./eta_charge ;
Egrid(a2)=Max_Capyearly(yy)*per(yy)*eta_discharge ;
end
if a1~=a2-1
Ebat (a1+1) = Max_Capyearly(yy)*(1-per(yy));
Ebat (a2+1) = -Max_Capyearly(yy)*(1-per(yy));
Egrid(a1+1)=-Max_Capyearly(yy)*(1-per(yy))/eta_charge ;
Egrid(a2+1)=Max_Capyearly(yy)*(1-per(yy))*eta_discharge ;
end
if a1==a2
Ebat(a3) = Max_Capyearly(yy)*per(yy);
Ebat(a2) = -Max_Capyearly(yy)*per(yy);
Egrid(a3)=-Max_Capyearly(yy)*per(yy)/eta_charge ;
Egrid(a2)=Max_Capyearly(yy)*per(yy)*eta_discharge ;
if a3~= a2-1
Ebat (a3+1) = Max_Capyearly(yy)*(1-per(yy));
Ebat (a2+1) = -Max_Capyearly(yy)*(1-per(yy));
Egrid(a3+1)=-Max_Capyearly(yy)*(1-per(yy))/eta_charge ;
Egrid(a2+1)=Max_Capyearly(yy)*(1-per(yy))*eta_discharge ;
end
if a3==a2-1
Ebat (a2+1) = 0;
Egrid(a2+1)= 0;
end
end
end
end
Result Matrix
Just for your understanding I am getting something like this for 2D matrix and 3D matrix
For 2D
Ebat
[0 0 0 0] %(Actual size of matrix is 24x365 just to explain what I am getting I have given an example matrix)
0 0 0 0 0
1 0 0 0
0.4 0 0 0]
For 3D
Ebat(:,:,1) % for 1st year
[0 0 0 0] %(Actual size of matrix is 24x365x9 just to explain what I am getting I have given an example matrix)
0 0 0 0 0
0.85 0 0 0
0.35 0 0 0]
Ebat(:,:,2)
0 0 0 0]
0 0 0 0
0 0 0 0
0 0 0 0]
The first year values should be 1 and 0.4 instead of 0.85 and 0.35 beacuse these values are for the 9th year which MATLAB is taking it in first year.I should not get all zeros in 2nd year but the matlab should store values.
I don't know whats wrong in my code.How should I correct this mistake beacuse the code for the 2D matrix is good and its giving right results.
As you can see that Ebat is wrong in 3D matrix for first year as well as for other years.
David Goodmanson on 24 May 2020
Hi Rabia,
In the 3d version with yy going from 1 to 9, you are accessing variables using the third dimension yy, but you are not saving any variables using the third dimension yy. So there is no way to fill up the nine layers.

Anshika Chaurasia on 9 Feb 2021
Hi Rabia,
The reason mentioned by David is correct.
I am providing the code snippet for your reference which will give you desired 3D matrix result. You can try this:
Ebat_final = zeros(24,365,9);
Egrid_final = zeros(24,365,9);
Max_Capyearly=[1.4,......,0.85]
for yy = 1:9
Ebat = zeros(24,365);
Egrid = zeros(24,365);
for a = 1:365
...
...
end
Ebat_final(:,:,yy) = Ebat;
Egrid_final(:,:,yy) = Egrid;
end