How to plot the difference between y components for all x?

2 visualizaciones (últimos 30 días)
SK
SK el 11 de Dic. de 2017
Editada: SK el 12 de Dic. de 2017
I have plotted the energy consumption of two model house on one plot using roughly the following code (I do not have the exact code in-front of me):
t = 0:24; %hour of day
iEc = [measured values for model A]; %length =25
Ec = [measured values for model B;] %length = 25
figure
plot(t, iEc, '-or', t, Ec, '-ob'); %after this, there are graph labels
I am satisfied with how everything turned out but just as an extra feature, I'd like to add a line between the y component of the two curves at the same t value on the same graph. For example, at t = 1 , i'd like to plot a line from (y of iEc) to (y of Ec) and list the difference next to it. This is similar to what I'd like to do but I'd like to add the difference between the curves: i.stack.imgur.com/G2Azd.png
I'm not 100% on how to do this, but I was thinking a for loop may be the best way to do this for all t. Any help or suggestions are appreciated. Thank you.

Respuesta aceptada

Joel Miller
Joel Miller el 12 de Dic. de 2017
Hi, I think this does what you are looking for:
plot(t, iEc, '-or', t, Ec, '-ob', [t; t], [iEc; Ec], '-k')
str = num2cell(abs(iEc-Ec));
Xoffset = t+.1; %X offset from vertical lines
Yoffset = min([iEc; Ec])+abs(iEc-Ec)/2; %midpoint of vertical lines
text(Xoffset, Yoffset, str)
  2 comentarios
SK
SK el 12 de Dic. de 2017
Thank you that was exactly what I was looking for. However I'd also like to add the following cases:
(1) I'd like to change the offset for the range (10<t<15) when the values where the curve difference is ~0.1? I can change X offset but it changes for all (which is ok) but I am unable to offset the y component properly. Please see picture.
(2) How can I change it if I wanted the lines for a specific set of numbers (ex:t = 3, 8, 18, 23)?
SK
SK el 12 de Dic. de 2017
Editada: SK el 12 de Dic. de 2017
I have tied it this way:
plot(t, iEc, '-ro', t, Ec, '-bo',[t; t], [iEc; Ec], '-k ');
hold on
for t = 0:24
if t>=10 && t<=15
str = num2cell(abs(iEc-Ec ));
Xoffset = t + .25;
Yoffset = max([iEc; Ec]) + 1;
text(Xoffset, Yoffset, str)
else
str = num2cell(abs(iEc-Ec));
Xoffset = t + .1;
Yoffset = min([iEc; Ec]) + abs(iEc-Ec)/2;
text(Xoffset, Yoffset, str)
end
end
but it give me the following errors:
Error using text: X and Y must be the same length Error in sgcs_final (line 27): text(Xoffset, Yoffset, str)
any suggestion on what to do from here?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Modeling en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by