EricAm
EricAm

Reputation: 105

Plot a family of circles in 3D

I have the following code for plotting 100 circles in separate height planes where the radius increases from 1 to 100.

for r=1:1:100
t=linspace(0,2*pi);
x=r*cos(t);
y=r*sin(t);
for h=100:100:10000   
    z = 100 * r * ones(1, length(t));
    plot3(x,y,z);
    if r == 1 && h == 100
        hold on;
        set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
    end
    drawnow;
end

end

Like this: enter image description here

Question

Now I want to change the code in order for the radius to decrease from 100 to 1, i.e turn the cone upside down. So the code should probably read like this but i can't get it to work:

for r=100:1:1
t=linspace(0,2*pi);
x=r*cos(t);
y=r*sin(t);
for h=100:100:10000   
    z = 100 * r * ones(1, length(t));
    plot3(x,y,z);
    if r == 100 && h == 100
        hold on;
        set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
    end
    drawnow;
end

end

Upvotes: 0

Views: 834

Answers (1)

el_tenedor
el_tenedor

Reputation: 664

You almost got it right. Try this:

for r=100:-1:1

The syntax is as follows:

for i=istart:istride:iend

For Optimization try this - i think this is as fast as it gets (tried it out with octave and gnuplot). I think now the script does it job just fine ;-):

t=linspace(0,2*pi);             % the array t doesnt get changed during for r=...
z = ones(1, length(t));         % same here: one preallocation should do the job

for r=100:-1:1

x=r*cos(t);
y=r*sin(t);

    z(:) = 10000-r*100;
    plot3(x,y,z);
    if r == 100
        hold on;
    end
    drawnow;

end

set(gcf, 'units','normalized','outerposition',[0 0 1 1]); %I think this is the most expesive operation. 
hold off

The problem was, that you didn't assume the linear relation between z and r.

Upvotes: 1

Related Questions