フィルターのクリア

How do i plot a discontinuous function?

12 ビュー (過去 30 日間)
abril
abril 2020 年 5 月 4 日
コメント済み: Star Strider 2020 年 10 月 5 日
Hi i need to plot in 3d the following function in matlab:
f(x,y)= (x^2)/(x^2 - y^2) when |x| ≠ |y|
0 when |x| = |y|
how should i do it? thx

採用された回答

Star Strider
Star Strider 2020 年 5 月 4 日
Try this:
f = @(x,y) ((x.^2)./(x.^2 - y.^2)) .* (abs(x) ~= abs(y))
[X,Y] = ndgrid(linspace(-1,1,150));
figure
surf(X, Y, f(X,Y))
grid on
shading('interp')
It will automatically be 0 when the logical condition is not met, so no specific test need be added for the equality condition.
.
  2 件のコメント
Nathan Shapiro
Nathan Shapiro 2020 年 10 月 5 日
I'm having a similar problem and tried your approach, but for some reason it is giving me an error message ("Matrix is singular to working precision"). Do you know what is causing the problem?
clear
clc
r=100;
x1 = linspace(-10,10,r);
y2 = linspace(-10,10,r);
[x,y] = meshgrid(x1,y2);
z=(sin(x)+sin(y))/(x.*y) .* (x~=0 & y~=0); %% the only discontinuity is when x or y equals 0
figure
surf(x,y,z)
xlabel('x');
ylabel('y');
zlabel('z');
grid on
shading interp
colorbar
Star Strider
Star Strider 2020 年 10 月 5 日
Do you know what is causing the problem?
Yes!
Use element-wise (dot-operator) division:
z=(sin(x)+sin(y))./(x.*y) .* (x~=0 & y~=0); %% the only discontinuity is when x or y equals 0
↑ ← HERE
and the problem no longer exists.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by