Raza Mehdi
Raza Mehdi

Reputation: 35

How to plot the variable cross-section of a cylinder?

x = r*cos(t) + ur*cos(t)    
y = r*sin(t) + ur*sin(t)
z = zeros(length(x))   

Cylinder length is along the z direction from 0 to 10.

The variable ur changes with each value of z. It represents a variable thickness of the cylinder.

I tried plot3() by making z a matrix but it creates a circle, not a cylinder.

Upvotes: 3

Views: 242

Answers (2)

Bilal
Bilal

Reputation: 3854

This code may help, instead of using pol2cart instruction, you can use your own equations, the resultant shape depends on the range specified in meshgrid at beginning, also I added Rotation option, of course you can comment that to ignore that option if you do like.

%===========================
% Close and Clear
%===========================
clc
close all
clear all
%===========================
% making a cylinder
%===========================
[theta, r, h] = meshgrid(0:.1:6.28,0:0.1:1, 0:.2:4); 
%====================================
% transforming the coordinate system
%====================================
[x, y, z] = pol2cart(theta, r, h); 
%====================================================
% rotating the data points around x axis by 60 degree
%====================================================
P = (rotx(60) * [x(:), y(:), z(:)]')';
%P = [x(:), y(:), z(:)];
%=======================================
%=======================================
% Drawing The Cloud Points
%=======================================
figure('Name','Cylinder Point Cloud','NumberTitle','off');
scatter3(P(:, 1), P(:, 2), P(:, 3)); % plots a circles around sample points
title('Cylinder Point Cloud');
axis equal
%===============================================

Upvotes: 1

MarkP
MarkP

Reputation: 21

z = ones(1,numel(x))'*(0:10)

would do the trick to get the values to make a cylinder with athe z from 0 to 10.

Upvotes: 1

Related Questions