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

6 vues (au cours des 30 derniers jours)
Jacky Chong
Jacky Chong le 11 Sep 2021
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.

Réponse acceptée

Chunru
Chunru le 11 Sep 2021
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 commentaires
Jacky Chong
Jacky Chong le 11 Sep 2021
This doesn't answer my question. I don't want to remove . I want to remove .
Chunru
Chunru le 11 Sep 2021
Modifié(e) : Chunru le 11 Sep 2021
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')

Connectez-vous pour commenter.

Plus de réponses (1)

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh le 11 Sep 2021
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);

Catégories

En savoir plus sur Contour Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by