Effacer les filtres
Effacer les filtres

How do i plot a discontinuous function?

12 vues (au cours des 30 derniers jours)
abril
abril le 4 Mai 2020
Commenté : Star Strider le 5 Oct 2020
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

Réponse acceptée

Star Strider
Star Strider le 4 Mai 2020
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 commentaires
Nathan Shapiro
Nathan Shapiro le 5 Oct 2020
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 le 5 Oct 2020
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by