0

How would I plot the surface:

z=(1+x^2)/(1+y^2) over the region |x|+|y|<=2 ?

I cannot get the surface constrained to the rhombus/square-shaped region.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
student
  • 1
  • 1

2 Answers2

2

Here's a simple example:

% Create a grid in X and Y:
[XX,YY] = meshgrid(-2:0.01:2);

% Evaluate Z according to the equation:
ZZ = (1+XX.^2) ./ (1+YY.^2);

% Introduce constraints using NaN
XX( abs(XX) + abs(YY) > 2 ) = NaN;

% Plot:
figure(); surf(XX,YY,ZZ, 'EdgeColor','interp');

Which produces:

enter image description here

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
0

The NaN trick does not respect the true boundaries of the domain (in the proposed answer the resulting roughness of boundary is hidden by the fine sampling). It would be better the generate the parametric surface itself, like this (I deliberately used a large step to highlight isoparametric curves)

% create a grid in the parameters domain
[U,V] = meshgrid(-1:0.2:1);

% compute actual (X,Y) values in the domain
X = U-V;
Y = U+V;

% Evaluate Z according to the equation:
Z = (1+X.^2) ./ (1+Y.^2);

% Plot:
figure(); surf(X,Y,Z, 'EdgeColor','interp');

enter image description here

Stéphane Mottelet
  • 2,853
  • 1
  • 9
  • 26