## Friday, August 14, 2015

### 3D Plots of Toroidal Spirals in Octave

Problem

A toroidal spiral is a space curve whose x, y, and z coordinates are computed with the following parametric equations:
x = (C + sin(Kt))cos(t); y = (C + sin(Kt))sin(t); z = cos(20t). They are called toroidal because they lie on a torus, as shown in Figure 1. In this post, we will briefly tackle the problem of 3D plotting toroidal spirals in Octave and investigate the impact of the C and K parameters.

Figure 1. 3D plot of a Toroidal Spiral

Solution Outline

The Octave code below shows how the plot in Figure 1 can be generated in Octave. It also shows a generic approach and allows us to play with the C and K parameters.

t = 0:0.01:2*pi;
K = 20;
C = 2;

x2 = arrayfun(@(x) (C + sin(K*x))*cos(x), t);
y2 = arrayfun(@(x) (C + sin(K*x))*sin(x), t);
z2 = arrayfun(@(x) cos(K*x), t);

figure;
plot3(x2, y2, z2);
xlabel('x=(2+sin(20t))cos(t)');
ylabel('y=(2+sin(20t))sin(t)');
zlabel('z=cos(20t)');
view([30, 40]);

Figure 2 shows another 3D plot  with C=4 and K=20.

Figure 2. 3D Plot of Another Toroidal Curve

Octave Source

t = 0:0.01:2*pi;
K = 20;

C = 0;
x0 = arrayfun(@(x) (C + sin(K*x))*cos(x), t);
y0 = arrayfun(@(x) (C + sin(K*x))*sin(x), t);
z0 = arrayfun(@(x) cos(K*x), t);

figure;
plot3(x0, y0, z0);
xlabel('x=(0+sin(20t))cos(t)');
ylabel('y=(0+sin(20t))sin(t)');
zlabel('z=cos(20t)');
view([30, 40]);

C = 1;
x1 = arrayfun(@(x) (C + sin(K*x))*cos(x), t);
y1 = arrayfun(@(x) (C + sin(K*x))*sin(x), t);
z1 = arrayfun(@(x) cos(K*x), t);

figure;
plot3(x1, y1, z1);
xlabel('x=(1+sin(20t))cos(t)');
ylabel('y=(1+sin(20t))sin(t)');
zlabel('z=cos(20t)');
view([30, 40]);

C = 2;
x2 = arrayfun(@(x) (C + sin(K*x))*cos(x), t);
y2 = arrayfun(@(x) (C + sin(K*x))*sin(x), t);
z2 = arrayfun(@(x) cos(K*x), t);

figure;
plot3(x2, y2, z2);
xlabel('x=(2+sin(20t))cos(t)');
ylabel('y=(2+sin(20t))sin(t)');
zlabel('z=cos(20t)');
view([30, 40]);

C = 3;
x3 = arrayfun(@(x) (C + sin(K*x))*cos(x), t);
y3 = arrayfun(@(x) (C + sin(K*x))*sin(x), t);
z3 = arrayfun(@(x) cos(K*x), t);

figure;
plot3(x3, y3, z3);
xlabel('x=(3+sin(20t))cos(t)');
ylabel('y=(3+sin(20t))sin(t)');
zlabel('z=cos(20t)');
view([30, 40]);

C = 4;
x4 = arrayfun(@(x) (C + sin(K*x))*cos(x), t);
y4 = arrayfun(@(x) (C + sin(K*x))*sin(x), t);
z4 = arrayfun(@(x) cos(K*x), t);

figure;
plot3(x4, y4, z4);
xlabel('x=(4+sin(20t))cos(t)');
ylabel('y=(4+sin(20t))sin(t)');
zlabel('z=cos(20t)');
view([30, 40]);

C = 5;
x5 = arrayfun(@(x) (CONST + sin(K*x))*cos(x), t);
y5 = arrayfun(@(x) (CONST + sin(K*x))*sin(x), t);
z5 = arrayfun(@(x) cos(K*x), t);

figure;
plot3(x5, y5, z5);
xlabel('x=(5+sin(20t))cos(t)');
ylabel('y=(5+sin(20t))sin(t)');
zlabel('z=cos(20t)');
view([30, 40]);