# error when evaluate InterpolationFunction

## Problem

I'd like to plot a moving point along zig zag line. And I used interpolation function to describe the zig zag line.

However when plotting the moving point, there is something wrong with evaluating the interpolation function. My codes is as bellow:

list3d = RandomReal[{0, 10}, {10, 3}]
d3tube = Graphics3D[{Red, Tube[list3d, .04]}]
f = Interpolation[Table[{i, list3d[[i]]}, {i, 0, 10}]]
DynamicModule[{t},
DynamicWrapper[
Show[d3tube, Graphics3D[{PointSize[Large], Black, Point[f[t]]}]],t = Clock[{0, 10}]]
]           (*erro occurs!*)


## Diagnosing

I examine the evaluation of interpolation function with bellowed codes:

Table[{i, f[i]}, {i, 0, 10, 0.05}]


which output:

{{0., {1.45643 - 1. (0. - 1. (-1.45643 + List)), 8.55359 - 1. (0. - 1. (-8.55359 + List)), 5.97185 - 1. (0. - 1. (-5.97185 + List))}}, {0.05, {1.45643 - 0.95 (0.05 (-0.975 (0.333333 (-6.98393 - 0.5 (8.81651 - List)) - 1. (0.5 (8.81651 - List) + 1. (-1.45643 + List))) + 1. (0.5 (8.81651 - List) + 1. (-1.45643 + List))) - 1. (-1.45643 + List)), ......

It shows there's List symbol within the output, I think, which mess up the Evaluation of Interpolation.

## Question

Can any one figure out how to evaluate the interpolation correctly to generate the coordinates of moving point?

• Hint: Something's wrong with the interpolating function, i.e. f. The first thing to do is test f separately. Try f[0]. What does it give? You'll see that it's not a number and that it has List in it. Why List? List is just a head of an expression. The head is the element with index 0. Once you realize this, you will look at the Table function and notice that the iterator i, which is also used as an array index with Part, goes from 0, and not from 1! There's your problem. Feb 10, 2017 at 9:09
• You will have to adjust the range in Clock accordingly. And there's another problem: the first argument of DynamicWrapper won't dynamically update unless it is wrapped in Dynamic. It will still see t as a symbol. It will be better to use a single Dynamic. For performance, put the Point in an Epilog, and make only the Epilog Dynamic. You can replace t with Clock[...] directly, and get rid of t. You may also want to use InterpolationOrder -> 1 to move along line segments. Feb 10, 2017 at 9:14
• Sorry, I was wrong about Epilog, it doesn't take 3D graphics. Feb 10, 2017 at 9:18

## 2 Answers

Try like this:

list3d = RandomReal[{0, 10}, {10, 3}];
d3tube = Graphics3D[{Red, Tube[list3d, .04]}];
f = Interpolation[Table[{i, list3d[[i]]}, {i, 1, 10}], InterpolationOrder -> 1];

Show[d3tube,
Graphics3D[{PointSize[Large], Black,
Dynamic@Point[f[Clock[{1, 10}]]]}]]


Changes:

• Mathematica uses 1-based indexing. Start from 1, not from 0.

• Use Dynamic instead of DynamicWrapper. Wrap it only around the bit where it is necessary, to improve performance.

r = {x, y, z} = Transpose[list3d];
{xi, yi, zi} = Interpolation[#, InterpolationOrder -> 1] & /@ r;
if[t_] := {xi[t], yi[t], zi[t]};
Animate[Graphics3D[{Red, Tube[list3d, .04], PointSize[Large], Black,
Point[if[t]]}], {t, 1, 10}]