How to draw a circular 3D plot in matlab
I want to plot this function in matlab : sin(4*x)*cos(4*y) on a disk
This is how i proceeded : syms x y; f=@(x,y) sin(4*x)*cos(4*y); ezmesh(f,'circ')
This method works with f=@(x,y) sin(2*x)*cos(2*y);
but with a more quickly varying function like f=@(x,y) sin(4*x)*cos(4*y); ezmesh mistakes these variations for discontinuities. the problem is i can't use the 'circ' parameter and increase the number of points that ezmesh uses at the same time (ezmesh didn't accept it)
Is there any other way ? Help please !
$\endgroup$1 Answer
$\begingroup$Suppose that you want to display the function $f(x,y)$ over the region $r<R,0\leq\theta\leq2\pi$.
Given a scalar R and a function handle taking $x$ and $y$ variables fun.
% Parameters to control fineness of the mesh
nRadius = 101;
nTheta = 101;
% generate a mesh in polar coordinates
[r,theta] = meshgrid(linspace(0,R,nRadius),linspace(0,2*pi,nTheta));
% transform from polar to Cartesian coordinates
X = r.*cos(theta);
Y = r.*sin(theta);
% allocate memory for the result
Z = zeros(nTheta,nRadius);
% calculate the function at the points
for idx = 1:numel(X) Z(i) = fun(X,Y);
end
% display the graph
surf(X,Y,Z)If fun can take vector arguments, then lines $12-18$ can be rewritten as Z=fun(X,Y);. In your case, this would mean rewriting the function to be
fun = @(x,y) sin(4*x).*cos(4*y);An alternate method to generate X and Y matrices for the coordinates is to generate the entire square around the circle and disregard those outside of the circle. This would replace lines $1-10$ above. The downside to this approach is that the boundary will be a series of jagged edges and not a smooth circle.
% choose appropriately to get desired fineness
nX = 101;
nY = 101;
% generate a mesh in Cartesian coordinates
[X,Y] = meshgrid(linspace(-R,R,nX),linspace(-R,R,nY));
% indicate points outside of the domain with NaN
I = X.^2+Y.^2>R;
X(I) = NaN;
Y(I) = NaN; $\endgroup$