Removing the origin from the 3D meshgrid?

1 次查看(过去 30 天)
Suppose I want to plot level surfaces of a function of 3 variables, and suppose this function has a 1/(r^n) dependence. Where r = sqrt(X.^2 + Y.^2 + Z.^2) is the distance from the origin. Naturally I want to filter the origin from the [X,Y,Z] meshgrid to avoid singularities. How do I do this? Here is my attempt.
I thought about using the nonzero function on vectors x,y, and z and then forming the meshgrid but then that excludes entire coordinate planes.
%1) Create the grid
n = 50;
rmax = 2400;
x = linspace(-rmax,rmax,n+1);
y = linspace(-rmax,rmax,n+1);
z = linspace(-rmax,rmax,n+1);
[X,Y,Z] = meshgrid(x,y,z);
%2) Create R
R = sqrt(X.^2 + Y.^2 + Z.^2);
R1 = R.^(-1); %Create the reciprocal of R
idx = isnan(R1); %Find all the NaNs
R(idx) = []; %Delete all the NaN's in R, do the same for X,Y,Z
X(idx) = [];
Y(idx) = [];
Z(idx) = [];
Despite there being obvious singularities in R1 (because x,y,z each contain zero) I am still getting R,X,Y,and Z to have the same number of elements as they did before.
I would greatly appreciate any help.

采纳的回答

darova
darova 2020-2-19
Make selected values even (2 4 6 .. 98 100)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by