Issues with contour plot of f(x, y) = x/y.
7 ビュー (過去 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.
0 件のコメント
採用された回答
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 件のコメント
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
2021 年 9 月 11 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Contour Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!