在matlab中直观地绘制一个无穷大的值

问题描述 投票:4回答:3

我正在尝试重现Dirac Delta功能:

我的代码:

x = -30:1:30;
y = zeros(1,numel(x)); %sets all values initially to zero
y(x==0)= inf; % the point corresponding to x=0 is set to inf
plot(x,y,'d')
axis([-40 40 0 inf])

我的代码产生:

matlab plot matlab-figure
3个回答
10
投票

您可以使用stem执行此操作,将其'Marker'指定为向上箭头...

% Open figure
figure;
% Blue stem plot at x=0, to y=75. Marker style is up arrow
stem(0, 75,'color','b','linewidth',2,'marker','^')
% Add infinity label at x=0, y = 82 = 75 + fontsize/2, where we plotted up to 75
text(0,82,'∞','FontSize',14)
% Set axes limits
xlim([-40,40])
ylim([0,90])

您可以看到output plot here,但请参阅下面的编辑以获得改进版本。

请注意,当然您应该选择相对于绘图上任何其他数据较大的y值。在这个例子中,我选择了75来粗略匹配你想要的示例图。 MATLAB无法在inf上绘制一个值,因为,无穷大位于y轴的哪个位置?


编辑:您可以在评论中指出由于Marco建议的其他'≈'字符而断开y轴。将xlimylim组合成一个axis调用,并更改y轴刻度以帮助指示轴断裂,我们得到以下结果:

stem(0, 80,'color','b','linewidth',2,'marker','^')
text([-42,0,38], [80,87,80], {'≈','∞','≈'}, 'Fontsize', 14)
axis([-40, 40, 0, 100])
yticks(0:20:60)

plot2


3
投票

要显示无穷大,您不应将y设置为无穷大。为此,您可以将y设置为与轴值成比例的较大值。例如,如果轴类似于[min_x max_x min_y max_y],则可以设置y(x==0) = max_y*10

在您的情况下,您将拥有:

x = -30:1:30; min_x = min(x) - 10; max_x = max(x) + 10;
y = zeros(1,numel(x)); 
% compute values of y here
% ...
min_y = min(y) - 10; max_y = max(y) + 10;
y(x==0)= 10 * max_y; 
plot(x,y,'d');
axis([min_x max_x min_y max_y]);

-2
投票

使用Matlab图中的tick属性,如下所述

screenshot

© www.soinside.com 2019 - 2024. All rights reserved.