18
$\begingroup$

Given the following Runge-Kutta ODE solver and the graphical output below, how do I get a 3D line plot instead of a 3D point plot? I see that there is no ListLinePlot3D function, so I thought it might be possible to convert the tables of values T1, T2 and T3 into interpolating functions and then use the ParametricPlot3D function to plot the solution in its line form instead of point form. Currently though I'm having a little trouble with the interpolating function + ParametricPlot3D output, as I just get an empty box.

Remove["Global`*"]
(*dx/dt=*)f[t_, x_, y_, z_] := σ (y - x);
(*dy/dt=*)g[t_, x_, y_, z_] := x (ρ - z) - y;
(*dz/dt=*)p[t_, x_, y_, z_] := x y - β z;
σ = 10;
ρ = 28;
β = 8/3;
t[0] = 0;
x[0] = 1;
y[0] = 1;
z[0] = 1;
tmax = 2000;
h = 0.01;

Do[
 {t[n] = t[0] + h n,

  k1 = h f[t[n], x[n], y[n], z[n]];
  l1 = h g[t[n], x[n], y[n], z[n]];
  m1 = h p[t[n], x[n], y[n], z[n]];

  k2 = h f[t[n] + h/2, x[n] +  k1/2, y[n] + l1/2, z[n] + m1/2];
  l2 = h g[t[n] + h/2, x[n] +  k1/2, y[n] + l1/2, z[n] + m1/2];
  m2 = h p[t[n] + h/2, x[n] + k1/2, y[n] + l1/2, z[n] + m1/2];

  k3 = h f[t[n] + h/2, x[n] + k2/2, y[n] + l2/2, z[n] + m2/2];
  l3 = h g[t[n] + h/2, x[n] + k2/2, y[n] + l2/2, z[n] + m2/2];
  m3 = h p[t[n] + h/2, x[n] + k2/2, y[n] + l2/2, z[n] +  m2/2];

  k4 = h f[t[n] + h, x[n] + k3, y[n] + l3, z[n] + m3];
  l4 = h g[t[n] + h, x[n] + k3, y[n] + l3, z[n] + m3];
  m4 = h p[t[n] + h, x[n] + k3, y[n] + l3, z[n] + m3];

  x[n + 1] = x[n] + 1/6 (k1 + 2 k2 + 2 k3 + k4);
  y[n + 1] = y[n] + 1/6 (l1 + 2 l2 + 2 l3 + l4);
  z[n + 1] = z[n] + 1/6 (m1 + 2 m2 + 2 m3 + m4);
  }, {n, 0, tmax}]

T1 = Table[{t[i], x[i]}, {i, 0, tmax}];
T2 = Table[{t[i], y[i]}, {i, 0, tmax}];
T3 = Table[{t[i], z[i]}, {i, 0, tmax}];

ListLinePlot[T1]
ListLinePlot[T2]
ListLinePlot[T3]

ListPointPlot3D[Table[{x[t], y[t], z[t]}, {t, 0, tmax}]]

I1 = Interpolation[T1]
I2 = Interpolation[T2]
I3 = Interpolation[T3]
ParametricPlot3D[{I1[t], I2[t], I3[t]}, {t, 0, tmax}]

What I'm looking to do is essentially get the following Lorenz Attractor point graph into a line graph form:

enter image description here

Any help would be appreciated, thanks guys.

$\endgroup$
3
  • 2
    $\begingroup$ Graphics3D[Line@Table[{x[t], y[t], z[t]}, {t, 0, tmax}]] $\endgroup$
    – Kuba
    Commented Jun 1, 2014 at 12:51
  • $\begingroup$ @Kuba I didn't see your comment until after I posted my answer. I had gotten interrupted after I started. $\endgroup$
    – Michael E2
    Commented Jun 1, 2014 at 14:55
  • $\begingroup$ @MichaelE2 I don't see any problem :) $\endgroup$
    – Kuba
    Commented Jun 1, 2014 at 17:09

3 Answers 3

22
$\begingroup$

Like so?

 ListPointPlot3D[Table[{x[t], y[t], z[t]}, {t, 0, tmax}], 
      ViewPoint -> {0, -2, 0}] /. Point -> Line

You might be interested in

link

$\endgroup$
16
$\begingroup$

Update: In versions 11+, replace

PlotStyle -> {Orange, Specularity[White, 10], (Tube @@ {##}) &}

with

PlotStyle -> {Orange, Specularity[White, 10], Tube[.5]}

when defining options.

Original answer:

This addresses the ParametricPlot3D part of the question.

intF = Interpolation[Table[{{t}, {x[t], y[t], z[t]}}, {t, 0, tmax}]]; 

options = {PlotStyle -> {Orange, Specularity[White, 10], (Tube @@ {##}) &},
           Background -> Black, Boxed -> False, Axes -> False, 
           PlotRange -> All,  BoxRatios -> 1};

ParametricPlot3D[intF[t], {t, 0, tmax}, Evaluate@options]

enter image description here

$\endgroup$
6
  • $\begingroup$ wow ! I would like to see this thing being rotated by a fakir. $\endgroup$
    – eldo
    Commented Jun 1, 2014 at 14:49
  • $\begingroup$ @eldo, yes.. it is much slower to render and to manipulate :) $\endgroup$
    – kglr
    Commented Jun 1, 2014 at 14:55
  • 1
    $\begingroup$ That the most important things are done through tubes. Evidence: first, the reproductive organs, the pen and our gun (Georg Christoph Lichtenberg, Sudelbücher, 1770) $\endgroup$
    – eldo
    Commented Jun 1, 2014 at 15:14
  • $\begingroup$ Very beautiful! $\endgroup$ Commented Jun 1, 2014 at 16:06
  • $\begingroup$ @kglr Very nice, but Tube has no effect on version 12. My attempts to fix it failed. Any ideas? $\endgroup$ Commented Dec 7, 2019 at 2:38
8
$\begingroup$

Another way to get a Line:

Graphics3D @ Line @ Table[{x[t], y[t], z[t]}, {t, 0, tmax}]

or, with style,

Graphics3D[
 {ColorData[1][1], Thickness[Medium], 
  Line[Table[{x[t], y[t], z[t]}, {t, 0, tmax}]]},
 Axes -> True]

Mathematica graphics

For fun, a variation on @eldo's that handles both a colored plot and a regular one:

ListPointPlot3D[Table[{x[t], y[t], z[t]}, {t, 0, tmax}], ColorFunction -> "Rainbow"] /.
  {l : {{_RGBColor, _Point} ..} :>
    ({Thickness[Medium],
      Transpose[l] /. {c_, p_} :> Line[First /@ p, VertexColors -> c]}),
  Point[p_] :> {Thickness[Medium], Line[p]}}

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ very nice ! If the underlying formula would be simpler you could even color it by curvature, speed or acceleration. Don't know how to do this with this beast. $\endgroup$
    – eldo
    Commented Jun 1, 2014 at 14:37
  • $\begingroup$ @eldo Thanks! One could use the differential equations to write the derivatives (of any order) in terms of the coordinates. $\endgroup$
    – Michael E2
    Commented Jun 1, 2014 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.