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

3 次查看(过去 30 天)
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.

采纳的回答

Joel Miller
Joel Miller 2017-12-12
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 个评论
SK
SK 2017-12-12
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 2017-12-12
编辑:SK 2017-12-12
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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Modeling 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by