Issues with contour plot of f(x, y) = x/y.
6 views (last 30 days)
Show older comments
Jacky Chong
on 11 Sep 2021
Answered: Abolfazl Chaman Motlagh
on 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.
0 Comments
Accepted Answer
Chunru
on 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 Comments
Chunru
on 11 Sep 2021
Edited: Chunru
on 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')
More Answers (1)
Abolfazl Chaman Motlagh
on 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);
0 Comments
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!