MATLAB: How do i plot a discontinuous function

3d plotsdiscontinuosfunctionsMATLAB Online Serverplotting

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

Best Answer

  • 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.
    .