How to label the maximum and minimum value on a figure
29 次查看(过去 30 天)
显示 更早的评论
Find the time point that corresponds to the maximum and minimum value. Add a text label to the figure indicating the maximum value. Add a text label to the figure indicating the minimum value.
0 个评论
采纳的回答
Image Analyst
2013-3-14
Have you used the help? Did you look up max, min, and text? You will use each of those functions to solve your homework exercise. Be sure to accept both output arguments of min() and max(). This should be a really really trivial problem, even for a beginner. It's so simple that we can't really even give hints without solving it. It's just 4 lines of code to call min(), max(), and 2 calls to text().
4 个评论
Image Analyst
2021-11-7
He's plotting something versus "time points", so t is a vector of the time points, in other words the x axis, and the x axis represents time.
% Create a sample signal.
t = linspace(0.1, 2*pi, 100); % Time axis
y = sin(t);
plot(t, y, 'b-', 'LineWidth', 2);
grid on;
xlabel('t [Time in seconds]');
ylabel('Voltage [Volts]');
% Draw t axis
yline(0, 'LineWidth', 2);
% Find min y value and it's index.
[minSignal, indexOfMin] = min(y);
% Find time value at that min y value.
tMin = t(indexOfMin)
xline(tMin, 'Color', 'r', 'LineWidth', 2)
textLabel = sprintf(' Min of %.2f at t=%f', minSignal, tMin);
text(tMin, minSignal, textLabel, 'fontSize', 15, 'Color', 'r', 'VerticalAlignment','top')
% Find max y value and it's index.
[maxSignal, indexOfMax] = max(y);
% Find time value at that max y value.
tMax = t(indexOfMax)
xline(tMax, 'Color', 'r', 'LineWidth', 2)
textLabel = sprintf(' Max of %.2f at t=%f', maxSignal, tMax);
text(tMax, maxSignal, textLabel, 'fontSize', 15, 'Color', 'r', 'VerticalAlignment','bottom')
ylim([-1.2, 1.2])

更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!