Since you are looking to plot the two variables using the ‘surf’ command, I suggest you mask the values first, then plot using ‘surf’. This is a common approach when working with constrained domains like a triangular region, where you only want to visualize values that fall inside a specific area.
To help illustrate the idea, here's a minimal example I tried on my end. The goal is to plot a function only within a triangle defined by the condition ‘X + Y ≤ 1’ (basically a right-angled triangle in the first quadrant).
- I started off by creating a simple grid that covers the full area, including and around the triangle, using ‘meshgrid’:
[x, y] = meshgrid(linspace(0, 1, 100), linspace(0, 1, 100));
- Then I defined the function and created the mask. After that, I applied the mask.
z = sin(pi*x) .* cos(pi*y);
- Apply the mask by setting values outside the triangle to 'NaN':
The key idea here is that ‘NaN’ values are automatically ignored in the plot, so anything outside the region of interest simply will not appear in the plot.
I ran this workaround on MATLAB R2024b, and below is the result:
You can find additional information about ‘meshgrid’ and ‘surf’ in the MATLAB’s official documentation:
I Hope this helps you.