Borrar filtros
Borrar filtros

How do i plot a discontinuous function?

12 visualizaciones (últimos 30 días)
abril
abril el 4 de Mayo de 2020
Comentada: Star Strider el 5 de Oct. de 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

Respuesta aceptada

Star Strider
Star Strider el 4 de Mayo de 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 comentarios
Nathan Shapiro
Nathan Shapiro el 5 de Oct. de 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 el 5 de Oct. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by