3

I want to graph different ellipses at different heights (z-coordinates).

My idea was to write the following code:

z=0:1/64:3/8;
t=linspace(-pi,pi,25);
[t,z]=meshgrid(t,z);
x=cos(-t);
y=cos(-t-4*pi*z);

I would like MATLAB to read my code like:

"Find x and y, and plot at the corresponding height (z). By doing so, join the points such that you'll form an ellipse at constant z".

I'm not sure what kind of function I could use here to do this and was hoping for someone to tell me if there exists such a function that will do the job or something similar.

In case you're wondering, I want to graph the polarization of light given two counterpropagating beams.

EDIT: While this is similar to the question draw ellipse and ellipsoid in MATLAB, that question doesn't address plotting 2D ellipses in 3D axes, which is what I am trying to do.

Community
  • 1
  • 1
DLV
  • 200
  • 1
  • 8
  • 2
    Please reconsider re-opening this question, as I think it was not correct to close it as a duplicate of the indicated question. This question is about plotting 2D ellipses on 3D axes, and the linked duplicate only addresses plotting 2D ellipses on 2D axes. – gariepy May 25 '16 at 21:15

1 Answers1

3

This can be solved by removing the meshgrid, and just using a plain old for-loop.

t = linspace(-pi,pi,25);
z = 0:1/64:3/8
f = figure;
hold on;
for i = 1:length(z)
    x=cos(-t); y=cos(-t-4*pi*z(i));
    plot3(x,y,z(i)*ones(length(z),1));
end

The problem in the original code is that you're trying build the ellipses all at once, but each ellipse only depends on a single z value, not the entire array of z values.

When I run this code, it produces the following plot:

polarized ellipses

gariepy
  • 3,576
  • 6
  • 21
  • 34