# How to remove singularit​ies/discon​tinuities on 3D plots?

7 次查看（过去 30 天）
student_md2022-1-5

I want to plot some functions f(x,y) including singularities.
For example;
f(x,y)=tan(x-y)
• In Matlab, when I run the following code
syms x y;
func=@(x,y) tan(x-y);
fsurf(@(x,y) func(x,y), [-4 2 -1 1])
I get
• In Mathematica, when I plot same function, I can remove the discontinouties using Exclusions -> "Singularities"
In MATLAB, how to remove vertical asymptote lines in the Matlab's figure like Mathematica?
-How to find the asymptote lines in Matlab? After finding the asymptote, maybe we make it invisible in the plot..

### 回答（1 个）

John D'Errico 2022-1-5
There is no simple direct command to remove singularities. However, nothing stops you from setting the z axis limits.
syms x y;
func=@(x,y) tan(x-y);
fsurf(@(x,y) func(x,y), [-4 2 -1 1])
zlim([-5,5])
##### 2 个评论显示隐藏 1更早的评论
John D'Errico 2022-1-7
By the way, there was absolutely no need to use syms there. Defining the function as a function of the variables x and y is sufficient. That is, I needed do only as below.
The vertical lines will still be there, connecting each branch of the tangent function, from inf to -inf. Can you simply exclude them? Not that i can think of.
H = fsurf(@(x,y) tan(x-y), [-4 2 -1 1])
H =
FunctionSurface with properties: Function: @(x,y)tan(x-y) XRange: [-4 2] YRange: [-1 1] EdgeColor: [0 0 0] LineStyle: '-' FaceColor: 'interp' Show all properties
zlim([-5,5])
There is no assurance that you can always trivially do what another package does, down to the last bit. Could you do this? Well, I suppose, if you used meshgrid to create the surface, then replaced any elements larger than a max or smaller then a min with NaNs. But that would take a few more lines to write than the simple call to fsurf.
So without fsurf, where we cannot eliminate the unwanted vertical lines, we could have done this.
[x,y] = meshgrid(linspace(-4,2),linspace(-1,1));
func = @(x,y) tan(x-y);
z = func(x,y);
z((z > 5) | (z < -5)) = NaN;
surf(x,y,z)
xlabel x
ylabel y
zlabel z
grid on
box on
colormap summer

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by