Alex Rivera | Logout

How to use plot3 restrict to a certain region in Matlab?

Asked 2012-11-14T04:19:49.233
8

For example, I want to plot the function

f(x,y) =sin(x^2+y^2)/(x^2+y^2),       x^2+y^2 <=4π

In Mathematica, I can do it as following:

Plot3D[Sin[x^2 + y^2]/(x^2 + y^2), {x, -4, 4}, {y, -4, 4}, 
 RegionFunction -> (#1^2 + #2^2 <= 4 Pi &)]

Where the RegionFunction specified the region of x,y to plot. enter image description here

Edit
Report

1 Answer

3

A slight variation to 3lectrologos's solution, with emphasis on keeping what you want:

x = -4*pi:0.01:4*pi;
y = -4*pi:0.01:4*pi;
[X,Y] = meshgrid(x,y);
Clean = (X.^2 + Y.^2)<=4*pi;
Y = Y.*Clean;
X = X.*Clean;

X(~any(X,2),:) = [];
X(:, ~any(X,1)) = [];
Y(~any(Y,2),:) = [];
Y(:, ~any(Y,1)) = [];

F = sin(X.^2+Y.^2)./(X.^2+Y.^2);
mesh(X,Y,F)

Note that in this case, you need to make sure that (0,0) is in your solution profile.

Edit: compressing matrices for easier plotting

answered 2012-11-14T05:07:30.373

Your Answer