Clear Filters
Clear Filters

How do i plot a discontinuous function?

12 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 4 May 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 Comments
Nathan Shapiro
Nathan Shapiro on 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 on 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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!