Azzera filtri
Azzera filtri

How do i plot a discontinuous function?

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

Risposta accettata

Star Strider
Star Strider il 4 Mag 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 Commenti
Nathan Shapiro
Nathan Shapiro il 5 Ott 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 il 5 Ott 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.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by