Plot has wrong x-values

1 次查看(过去 30 天)
Noush
Noush 2022-1-19
评论: DGM 2022-1-21
Hi, I want to create a graph with two different plots, but the plots keep overlapping as you can see in the screenshot. I don't understand why, in the table columns that I want to plot (column 4 and 5) do not cross.
The x-values are datetime values.
Here Is the relevant part of my code:
%Ladeprozess
Ladeleistung = PVTabelle{:,1} - Grundlast(:,Zahl_Woche) - AGTabelle.Leistung_Gesamt - Einspeiseanteil; %Ladewert, der sich aus den Leistungsflussgraphen ergibt
%Vektor mit 1, wenn die Ladebedingung erfüllt ist und 0, wenn sie nicht erfüllt wird:
Ladebedingung = (PVTabelle{:,1} - (Grundlast(:,Zahl_Woche) - AGTabelle.Leistung_Gesamt - Einspeiseanteil)) > 0;
idx1 = find(Ladebedingung); %idx1 ist ein Vektor mit den Indexwerten der Ladebedingung
Ladeleistung2 = zeros(168,1);
Ladeleistung2(idx1) = Ladeleistung(Ladebedingung); %Leistungswerte des Graphen an den Stellen,an denen die Ladebedingung 1 ist
Ladeleistung2(Ladeleistung2 > 600) = 600; %Begrenzt die maximale Ladeleistung auf 600kW (Batteriebegrenzung)
Ladeleistung2(Ladeleistung2 <= 0) = 0; %Spezifiziert, dass nur positive y Werte dargestellt werden sollen (Ladeprozess)
%Entladeprozess
Entladeleistung = Grundlast(:,Zahl_Woche) + AGTabelle.Leistung_Gesamt + Einspeiseanteil - PVTabelle{:,1}; %Entladewert, der sich aus dem Leistungsflussgraphen ergibt
%Vektor mit einer 1, wenn die Ladebedingung erfüllt ist und eine null, wenn sie nicht erfüllt wird
Entladebedingung = ((Grundlast(:,Zahl_Woche)+ AGTabelle.Leistung_Gesamt + Einspeiseanteil) - PVTabelle{:,1}) > 0 ;
idx2 = find(Entladebedingung); %idx2 ist ein Vektor mit den Indexwerten der Entladebedingung
Entladeleistung2 = zeros(168,1);
Entladeleistung2(idx2) = Entladeleistung(Entladebedingung); %Negative Leistungswerte des Graphen an den Stellen, an denen die Entladebedingung 1 ist
Entladeleistung2(Entladeleistung2 > 750) = 750; %Begrenzt die maximale Entladeleistung auf -750kW (Batteriebegrenzung)
Entladeleistung2(Entladeleistung2 <= 0) = 0; %Spezifiziert, dass nur negative y Werte dargestellt werden sollen (Entladeprozess)
%Tabelle mit Lade- und Entladeleistungen
varNames = {'Datum_und_Zeit','Ladeleistung_Graph','Entladeleistung_Graph','Ausgelegte_Ladeleistung','Ausgelegte_Entladeleistung'};
BatterieTabelle = table(dt,Ladeleistung2,Entladeleistung2,zeros(168,1),zeros(168,1),'VariableNames',varNames);
%Graph2: Lade- und Entladekurven ohne Kapazitätsbegrenzung
figure(2)%Ladekurve
subplot(3,1,1)
area(BatterieTabelle.Datum_und_Zeit,BatterieTabelle.Ladeleistung_Graph)
ylim([-100 1000]);
title("Ladeprozess",'FontSize',14)
subplot(3,1,2)%Entladekurve
area(BatterieTabelle.Datum_und_Zeit,BatterieTabelle.Entladeleistung_Graph)
ylim([-1000 1000]);
title("Entladeprozess",'FontSize',14)
%Zeitvariablen umgerechnet zu einem "double" Wert
dn1 = datenum(BatterieTabelle{1:24,1});
dn2 = datenum(BatterieTabelle{25:48,1});
dn3 = datenum(BatterieTabelle{49:72,1});
dn4 = datenum(BatterieTabelle{73:96,1});
dn5 = datenum(BatterieTabelle{97:120,1});
dn6 = datenum(BatterieTabelle{121:144,1});
dn7 = datenum(BatterieTabelle{145:168,1});
dn8 = datenum(BatterieTabelle{:,1}) - datenum(BatterieTabelle{1,1});
%Integrale der 7 Tage für den Ladeprozess
A1 = cumtrapz(dn1,BatterieTabelle{1:24,2});
A2 = cumtrapz(dn2,BatterieTabelle{25:48,2});
A3 = cumtrapz(dn3,BatterieTabelle{49:72,2});
A4 = cumtrapz(dn4,BatterieTabelle{73:96,2});
A5 = cumtrapz(dn5,BatterieTabelle{97:120,2});
A6 = cumtrapz(dn6,BatterieTabelle{121:144,2});
A7 = cumtrapz(dn7,BatterieTabelle{145:168,2});
%Integrale der 7 Tage für den Entladeprozess
A8 = - cumtrapz(dn1,BatterieTabelle{1:24,3});
A9 = - cumtrapz(dn2,BatterieTabelle{25:48,3});
A10 = - cumtrapz(dn3,BatterieTabelle{49:72,3});
A11 = - cumtrapz(dn4,BatterieTabelle{73:96,3});
A12 = - cumtrapz(dn5,BatterieTabelle{97:120,3});
A13 = - cumtrapz(dn6,BatterieTabelle{121:144,3});
A14 = - cumtrapz(dn7,BatterieTabelle{145:168,3});
KLaden1 = [A1 A2 A3 A4 A5 A6 A7];
KEntladen1 = [A8 A9 A10 A11 A12 A13 A14];
KLaden2 = min(KLaden1, 3040); %Begrenzt die Kapazität der Ladung auf 3040 kWh
KEntladen2 = min(KEntladen1, KLaden1); %Spezifiziert, dass nur soviel entladen werden kann, wie geladen wurde
%Ladebedingung (nach der Auslegung der notwendigen Kapazität)
idx3 = BatterieTabelle.Ladeleistung_Graph > 0;
idx4 = sum(idx3(1:24) == 1); %Summe der Male, an denen idx3 gleich 1 ist (also geladen werden soll) am ersten Tag
idx5 = sum(idx3(25:48) == 1);
idx6 = sum(idx3(49:72) == 1);
idx7 = sum(idx3(73:96) == 1);
idx8 = sum(idx3(97:120) == 1);
idx9 = sum(idx3(121:144) == 1);
idx10 = sum(idx3(145:168) == 1);
KBatterie = max(max(KLaden2));
%Ladeprozesse
%Montag
LBatterieTag1 = zeros(24,1);
LL = KBatterie/idx4;
LBatterieTag1(idx3(1:24) == 1) = cumsum(repmat(LL, idx4, 1));
BatterieTabelle{1:24,4} = LBatterieTag1;
%Dienstag
LBatterieTag2 = zeros(24,1);
LL = KBatterie/idx5;
LBatterieTag2(idx3(25:48) == 1) = cumsum(repmat(LL, idx5, 1));
BatterieTabelle{25:48,4} = LBatterieTag2;
%Mittwoch
LBatterieTag3 = zeros(24,1);
LL = KBatterie/idx6;
LBatterieTag3(idx3(49:72) == 1) = cumsum(repmat(LL, idx6, 1));
BatterieTabelle{49:72,4} = LBatterieTag3;
%Donnerstag
LBatterieTag4 = zeros(24,1);
LL = KBatterie/idx7;
LBatterieTag4(idx3(73:96) == 1) = cumsum(repmat(LL, idx7, 1));
BatterieTabelle{73:96,4} = LBatterieTag4;
%Freitag
LBatterieTag5 = zeros(24,1);
LL = KBatterie/idx8;
LBatterieTag5(idx3(97:120) == 1) = cumsum(repmat(LL, idx8, 1));
BatterieTabelle{97:120,4} = LBatterieTag5;
%Samstag
LBatterieTag6 = zeros(24,1);
LL = KBatterie/idx9;
LBatterieTag6(idx3(121:144) == 1) = cumsum(repmat(LL, idx9, 1));
BatterieTabelle{121:144,4} = LBatterieTag6;
%Sonntag
LBatterieTag7 = zeros(24,1);
LL = KBatterie/idx10;
LBatterieTag7(idx3(145:168) == 1) = cumsum(repmat(LL, idx10, 1));
BatterieTabelle{145:168,4} = LBatterieTag7;
%Entladebedingung(nach der Auslegung der notwendigen Kapazität)
idx11 = BatterieTabelle.Entladeleistung_Graph > 0;
idx12 = sum(idx11(1:24) == 1);
idx13 = sum(idx11(25:48) == 1);
idx14 = sum(idx11(49:72) == 1);
idx15 = sum(idx11(73:96) == 1);
idx16 = sum(idx11(97:120) == 1);
idx17 = sum(idx11(121:144) == 1);
idx18 = sum(idx11(145:168) == 1);
%Entladeprozesse
%Montag
ELBatterieTag1 = zeros(24,1);
ELL = KBatterie/idx12;
ELBatterieTag1(idx11(1:24) == 1) = cumsum(repmat(ELL, idx12, 1));
BatterieTabelle{1:24,5} = ELBatterieTag1;
%Dienstag
ELBatterieTag2 = zeros(24,1);
ELL = KBatterie/idx13;
ELBatterieTag2(idx11(25:48) == 1) = cumsum(repmat(ELL, idx13, 1));
BatterieTabelle{25:48,5} = ELBatterieTag2;
%Mittwoch
ELBatterieTag3 = zeros(24,1);
ELL = KBatterie/idx14;
ELBatterieTag3(idx11(49:72) == 1) = cumsum(repmat(ELL, idx14, 1));
BatterieTabelle{49:72,5} = ELBatterieTag3;
%Donnerstag
ELBatterieTag4 = zeros(24,1);
ELL = KBatterie/idx15;
ELBatterieTag4(idx11(73:96) == 1) = cumsum(repmat(ELL, idx15, 1));
BatterieTabelle{73:96,5} = ELBatterieTag4;
%Freitag
ELBatterieTag5 = zeros(24,1);
ELL = KBatterie/idx16;
ELBatterieTag5(idx11(97:120) == 1) = cumsum(repmat(ELL, idx16, 1));
BatterieTabelle{97:120,5} = ELBatterieTag5;
%Samstag
ELBatterieTag6 = zeros(24,1);
ELL = KBatterie/idx17;
ELBatterieTag6(idx11(121:144) == 1) = cumsum(repmat(ELL, idx17, 1));
BatterieTabelle{121:144,5} = ELBatterieTag6;
%Sonntag
ELBatterieTag7 = zeros(24,1);
ELL = KBatterie/idx18;
ELBatterieTag7(idx11(145:168) == 1) = cumsum(repmat(ELL, idx18, 1));
BatterieTabelle{145:168,5} = ELBatterieTag7;
%Graphen
subplot(3,1,3)%Lade- und Entladekurven mit der ausgerechneten Batteriegröße
area(dt,BatterieTabelle.Ausgelegte_Ladeleistung)
hold on
area(dt,-BatterieTabelle.Ausgelegte_Entladeleistung)
ylim([-1000 1000]);
title("Ausgelegte Batterie", 'FontSize',14)
figure(1)
hold on
area(dt,BatterieTabelle.Ausgelegte_Ladeleistung)
area(dt,-BatterieTabelle.Ausgelegte_Entladeleistung)
legend("PV Leistung","Aggregatslast","Grundlast","Einspeiseanteil","Laden","Entladen",'FontSize',14)
hold off

回答(1 个)

DGM
DGM 2022-1-19
The values are not wrong. There are just few values, so the lines connecting the points make the appearance of nonzero data where there is none. Consider a simplified example:
x = 1:14;
y1 = [50:25:275 zeros(1,4)];
y2 = -[zeros(1,10) 148:21:220];
plot(x,y1,'o-'); hold on
plot(x,y2,'o-')
Depending on your needs, this may be able to be solved by replicating a datapoint at the transition to create a sharp step at the last/first nonzero point.
clf
x1 = x([1:10 10 11:14]);
x2 = x([1:10 11 11:14]);
y1 = y1([1:10 11 11:14]);
y2 = y2([1:10 10 11:14]);
plot(x1,y1,'o-'); hold on
plot(x2,y2,'o-')
However, if you do any sort of rate analysis on these modified vectors, you'll obviously have problems due to the step.
  5 个评论
Noush
Noush 2022-1-21
And on that same topic, I also don't really understand what the difference is between graphing with trapz or cumtrapz ?
DGM
DGM 2022-1-21
Well, I kind of meant either trapz() or cumtrapz(), I just saw that you were already using it.
The answers here are better than what I was going to say as far as the differences:
Basically trapz() gives a scalar output, whereas cumtrapz() gives all the intermediate values as the summation progresses - a sum versus a cumulative sum. I assume you're already using what's appropriate for your needs.
As far as why cumtrapz/etc still treats the data the way the plot shows, that's to be expected unless you decide how to interpret what happens in between those points. Trapezoidal integration is going to treat the data between points as a straight line, just like plot() does. If you know exactly where the transition(s) occur, you could possibly insert extra points as I showed, but you'd have to decide if that's appropriate.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by