sue
sue

Reputation: 29

Graphing a parameterized 3D circle in MATLAB

I'm new to MATLAB and have a fundamental problem that I'm having issues finding online resources for. I have an equation for a circle: C = center + (radius*cos(theta)) + (radius*sin(theta)). I want to graph this circle IN ADDITION to other things I'm graphing beforehand...all in 3D. I've tried using theta = linspace(0,2*pi) but then in constructing my equation for the circle it says matrix sizes don't agree! If you could offer any help that would be great!

Upvotes: 2

Views: 4580

Answers (3)

eat
eat

Reputation: 7530

Since circle is fundamentally a 2D entity, you'll need to generate the points representing it to some known 2D plane embedded in 3D space. Then you'll be able to rotate, scale and translate it as you wish.

A simple demonstration code hopefully makes it clear:

n= 78; theta= linspace(0, 2* pi, n); c= [1 2 3]'; r= sqrt(2);

points= r*[cos(theta); sin(theta); zeros(1, n)]+ repmat(c, 1, n);
plot3(points(1,:), points(2,:), points(3,:), 's-')
axis equal, hold on

R= rref([rand(3) eye(3)]); R= R(:, 4: 6);
points= R* points;
plot3(points(1,:), points(2,:), points(3,:), 'ro')

Upvotes: 1

gnovice
gnovice

Reputation: 125854

Here's an example of plotting a circle with a given radius and center (and assuming the circle lies in the plane z = 0):

radius = 2;      %# Define your radius
center = [1 2];  %# Define your circle center [Cx Cy]
theta = linspace(0,2*pi);          %# Create an array of theta values
X = center(1)+radius.*cos(theta);  %# Create the X values for the circle
Y = center(2)+radius.*sin(theta);  %# Create the Y values for the circle
Z = zeros(size(X));                %# Create the Z values for the circle (needed
                                   %#   for 3D plotting)
hold on;       %# Add to the current existing plot
plot3(X,Y,Z);  %# Plot your circle in 3D

And here are some links to online resources that should be a good starting point for learning the basics of plotting in MATLAB:

Upvotes: 1

Amro
Amro

Reputation: 124563

I am not sure what you are trying to do, but consider this example:

theta = linspace(0,2*pi,100);
C = [0;0];
r =  1;

p = bsxfun(@plus, r.*[cos(theta);sin(theta)], C);
plot(p(1,:), p(2,:), '.-')
axis equal

enter image description here

Upvotes: 0

Related Questions