Issues with contour plot of f(x, y) = x/y.

6 次查看(过去 30 天)
I am encountering some issues with plotting contour curves of using the function. It keeps plotting the line .
Here's my code
x = -5:.1:5;
y = -5:.1:5;
[X,Y] = meshgrid(x,y);
Z = X./Y;
[f, h]=contour(X,Y,Z,'ShowText','on', 'LevelList', [-3, -2, -1, 0, 1, 2, 3]);
h.LineWidth = 2;
grid on
axis equal
xlabel('x')
ylabel('y')
title('Contour curves of f(x, y) = x/y')
I want to get rid of the line along with its labels.

采纳的回答

Chunru
Chunru 2021-9-11
You just need to remove the level=0 which result in x=0 (not y=0) being plotted.
x = -5:.1:5;
y = -5:.1:5;
[X,Y] = meshgrid(x,y);
Z = X./Y;
[f, h]=contour(X,Y,Z,'ShowText','on', 'LevelList', [-3, -2, -1, 1, 2, 3]);
h.LineWidth = 2;
grid on
axis equal
xlabel('x')
ylabel('y')
title('Contour curves of f(x, y) = x/y')
  2 个评论
Jacky Chong
Jacky Chong 2021-9-11
This doesn't answer my question. I don't want to remove . I want to remove .
Chunru
Chunru 2021-9-11
编辑:Chunru 2021-9-11
You have rapid change close to y=0 (as your function is x./y. You can remove the data around y=0.
x = [-5:.1:5];
y = [-5:.1:-.2];
[X,Y] = meshgrid(x,y);
Z = X./Y;
[f, h]=contour(X,Y,Z,'ShowText','on', 'LevelList', [-3, -2, -1, 0, 1, 2, 3]);
h.LineWidth = 2;
hold on
x = [-5:.1:5];
y = -[-5:.1:-.2];
[X,Y] = meshgrid(x,y);
Z = X./Y;
[f, h]=contour(X,Y,Z,'ShowText','on', 'LevelList', [-3, -2, -1, 0, 1, 2, 3]);
h.LineWidth = 2;
grid on
axis equal
xlabel('x')
ylabel('y')
title('Contour curves of f(x, y) = x/y')

请先登录,再进行评论。

更多回答(1 个)

Abolfazl Chaman Motlagh
those lines are not y=0. those are continues of the contour lines around the region for your given values. near y=0, z goes to +- infinity.
for better vision look at this:
x = -1:.1:1;
y = -1:.1:1;
y = setdiff(y,[0]); % exclude y=0
[X,Y] = meshgrid(x,y);
Z = X./Y;
[f, h]=contour(X,Y,Z,'ShowText','on', 'LevelList', [ -3,-2, -1, 0, 1, 2,3],'LineWidth',2);
maybe it's better to look at this:
[f, h]=contourf(X,Y,Z,'ShowText','on', 'LevelList', [ -3,-2, -1, 0, 1, 2,3],'LineWidth',2);

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by