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

6 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Chunru
Chunru il 11 Set 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 Commenti
Jacky Chong
Jacky Chong il 11 Set 2021
This doesn't answer my question. I don't want to remove . I want to remove .
Chunru
Chunru il 11 Set 2021
Modificato: Chunru il 11 Set 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')

Accedi per commentare.

Più risposte (1)

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh il 11 Set 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);

Categorie

Scopri di più su Animation in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by