フィルターのクリア

z value is missing during using surf for plotting

1 回表示 (過去 30 日間)
jarvan
jarvan 2014 年 12 月 14 日
コメント済み: Star Strider 2014 年 12 月 14 日
I have a user-program which is download on http://tinyurl.com/p4uqz4r. I have to use surf to plot a right circular cone with base R=1at r1= [0, 0, 0] and apex R=0 at r2=[1, 1, 1]. However, when I put R=0,r1= [0, 0, 0],r2=[1, 1, 1] the surf function cant go through because I dont have a z value. I don't have how can I plot with surf with above requirment, which is R=1at r1= [0, 0, 0] and apex R=0 at r2=[1, 1, 1].
function [X, Y, Z] = cylinder2P(R, N,r1,r2)
% The parametric surface will consist of a series of N-sided
% polygons with successive radii given by the array R. Axial
% distance Z increases in equal sized steps from 0 to 1 along
% the line lying between points r1 and r2.
% Author: Luigi Barone
% Date: 9 September 2001
% Modified: Per Sundqvist July 2004
% Set up an array of angles for the polygon.
theta = linspace(0,2*pi,N);
m = length(R); % Number of radius values
% supplied.
if m == 1 % Only one radius value supplied.
R = [R; R]; % Add a duplicate radius to make
m = 2; % a cylinder.
end
X = zeros(m, N); % Preallocate memory.
Y = zeros(m, N);
Z = zeros(m, N);
v=(r2-r1)/sqrt((r2-r1)*(r2-r1)'); %Normalized vector;
%cylinder axis described by: r(t)=r1+v*t for 0<t<1
R2=rand(1,3); %linear independent vector (of v)
x2=v-R2/(R2*v'); %orthogonal vector to v
x2=x2/sqrt(x2*x2'); %orthonormal vector to v
x3=cross(v,x2); %vector orthonormal to v and x2
x3=x3/sqrt(x3*x3');
r1x=r1(1);r1y=r1(2);r1z=r1(3);
r2x=r2(1);r2y=r2(2);r2z=r2(3);
vx=v(1);vy=v(2);vz=v(3);
x2x=x2(1);x2y=x2(2);x2z=x2(3);
x3x=x3(1);x3y=x3(2);x3z=x3(3);
time=linspace(0,1,m);
for j = 1 : m
t=time(j);
X(j, :) = r1x+(r2x-r1x)*t+R(j)*cos(theta)*x2x+R(j)*sin(theta)*x3x;
Y(j, :) = r1y+(r2y-r1y)*t+R(j)*cos(theta)*x2y+R(j)*sin(theta)*x3y;
Z(j, :) = r1z+(r2z-r1z)*t+R(j)*cos(theta)*x2z+R(j)*sin(theta)*x3z;
end
%surf(X, Y, Z);

採用された回答

Star Strider
Star Strider 2014 年 12 月 14 日
You can do that with the built-in cylinder function, using its ability to create shapes from cylinders:
q = 0:0.1:1;
[X,Y,Z] = cylinder(q);
figure(1)
surf(X,Y,Z)
Experiment with it to get the result you want.
  4 件のコメント
jarvan
jarvan 2014 年 12 月 14 日
thank you!
Star Strider
Star Strider 2014 年 12 月 14 日
My pleasure!
The cylinder2P function looks useful. I suppose it would be possible to do much the same thing with cylinder and rotate, similar to this archive ‘spilled bottle’ code:
f = @(x) x.*exp(-x);
x = linspace(0, 5, 31);
[X, Y, Z] = cylinder(f(x));
figure(1)
h = surf(X, Y, Z)
rotate(h, [0 1 0], 90)
xlabel('X')
ylabel('Y')
zlabel('Z')
axis equal

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLighting, Transparency, and Shading についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by