MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

0

Plotting a Constant Section for a Piecewise Function

Jacob Rensner さんによって質問されました 2019 年 3 月 30 日
最新アクティビティ Jacob Rensner さんによって コメントされました 2019 年 3 月 30 日
I am trying to plot a piecewise function. The version of the software I have does not allow me to ue the built in function so I have to it a round-a-bout way. Below is the code I have for the figure.
% Figure generation for algorithm #1
figure(1) % Generates plots of heating calibration data with model overlay
subplot(2,1,1) % Positioning of 'clean' heating calibration data
plot(Time(:,1), Temperature(:,1), 'k-') % Plots calibration data
title({'Clean Heating Calibration Data as a Function of Time', 'with Piecewise Model Overlay'})
xlabel('Time (sec)')
ylabel('Temperature (deg C)')
hold on
plot(Time_Top_1a, y1_L(1), 'r--') % Plots constant section of piecewise function
hold on
plot(Time_Bot_1a, HeatingClean_Expected_Temperatures1, 'r--') % Plots piecewise model
legend('Calibration Data', 'Piecewise Model Estimates', 'location', 'best')
grid on
hold off
The first plot is some actual data. The two plots following that plot a model of the actual data. The very bottom plot works fine. I am having trouble with the middle one. It is suppossed to plot a constant value, defined by y1_L(1), over a range of times, defined by Time_Top_1a. When I run the script, this line does not show up but the legend on the graph acknowledges its exisitence. Also, when I change the plotting style from line to data marker, it shows up on the plot but I need it to be a line that matches the style ofthe very bottom plot. Any help would be greatly appreciated.

  0 件のコメント

サインイン to comment.

1 件の回答

回答者: John D'Errico
2019 年 3 月 30 日
編集済み: John D'Errico
2019 年 3 月 30 日
 採用された回答

Your problem lies in your assumptions. Try this example:
t = 0:10
t =
0 1 2 3 4 5 6 7 8 9 10
y = 1;
plot(t,y,'r--')
Nothing appears in the plot, but you assume that it should do something logical. As it turns out, plot does effectively nothing visible when you try to plot a scalar versus a vector like this. (To be honest, I would rather see this return an error than do what appears to be essentially nothing. At least it would be nice if plot returned a warning message that alerts the user to the issue.) In fact though, it does create a plot, or it tries to do so.
h = get(gca,'children')
h =
11×1 Line array:
Line
Line
Line
Line
Line
Line
Line
Line
Line
Line
Line
So, what did it do? When I look at the handles returned here, I see they represent 11 separate, distinct "lines", but each line is only a single point.
h(1)
ans =
Line with properties:
Color: [1 0 0]
LineStyle: '--'
LineWidth: 0.5000
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: 10
YData: 1
ZData: [1×0 double]
Show all properties
So, it plotted the value in y against each of the elements of t, creating 11 distinct "lines", each of length zero. Since no line segments were ever created, then we saw no lines connecting the dots. This also explains why, when you try effectively this:
plot(t,y,'r--o')
it produces a quasi-"line" but with only the points plotted.
I've gone into a bit of depth here to explain what MATLAB did, and why it misunderstood what you tried to do.
Now, how should you fix it? That is far simpler than the explanation I gave.
plot(t,repmat(y,size(t)),'r--')
Essentially, you need to make y the same size as the first argument, so it now understands that you want to create a line as connect the dots, using a '--' linestyle. REPMAT suffices for that purpose, as I did here.

  1 件のコメント

Awesome! Thank you so much for your help.

サインイン to comment.



Translated by