How to plot a rolling window of five points from a matrix using animate in Maple

42 Views Asked by At

This question is about animating a plot of 3d points in Maple.

Consider the parametric function

$$f(t)=(0,\cos{t},\sin{t})$$

Let's create a matrix of twenty points of this function, each row being a point.

pd := Matrix([seq(f(t), t = 0 .. 20)]);

We can plot these points

pointplot3d(pd)

to obtain

enter image description here

Now, imagine we want to have an animation that plots a rolling window of five points. That is, first we plot rows 1-5, then rows 2-6, then 3-7, and so on.

I thought about implementing this as follows

with(plots):
animate(pointplot3d, [pd[t .. t + 5, () .. ()]], t = [1, 2, 3, 4, 5])

However, I get an error

Error, non-integer ranges in Matrix index

The indexing of the matrix is being done using values of $t$ from $1$ to $5$, if I understand correctly.

Here is a similar example that also uses a list for this same parameter to animate

animate(plot3d, [[sin(q^2 + q)*cos(q), sin(q^2 + q)*sin(q), cos(q^2 + q)], q = s - 1 .. s], s = [seq(i, i = 1 .. 5)], frames = 100)

This produces the correct animation.

I would like to attach my worksheet, but I can't see how.

In any case, why does the first use of animate not work?

EDIT: the following workaround seems to work

m1 := proc(pd, t) 
  return pointplot3d(pd[t .. t + 5, () .. ()]); 
end proc

animate(m1, [pd, t], t = [seq(i, i = 1 .. 15)])

What I am doing with this code is passing my own function as the first parameter to pointplot3d. The docs say this first parameter is a

Maple procedure that generates a 2-D or 3-D plot or plot objects

I wasn't sure if this would work because when I check the return value of pointplot3d it is a function. I am not sure how the type system is in Maple. But this did work. And when I print out the value of t in the function m1 they seem to be integers.

Not sure why the original (easier) way isn't working.

1

There are 1 best solutions below

0
On BEST ANSWER

You are making a common mistake and encountering what is sometimes known in Maple as premature evaluation.

The usual mechanism for how Maple handles procedure calls is that the arguments are evaluated up front, and then passed into the procedure.

You are passing the following as the second argument to the animate command, and it emits an error because at this moment (prior to entering the animate procedure itself) there is no value of t. And the Matrix pd cannot be indexed in such an indeterminate way.

pd[t .. t + 25, ..];
Error, non-integer ranges in Matrix index

There are several ways to accomplish what you're after.

One way is to delay the evaluation of pointplot3d by using a so-called operator calling sequence of animate, with another procedure, as in your second example.

Another way is delay the evaluation with single right-ticks (aka uneval quotes in Maple).

For example (using a slightly larger number of points, for fun):

restart;
with(plots):
f := t -> [0,cos(t),sin(t)]:
pd := Matrix([seq(f(t), t=0..50)]):

pointplot3d(pd);

animate(pointplot3d,
        ['pd[t .. t + 25, ..]'],
        t = [$1..25]);

There are other ways, including construction of the individual frames using seq, which has special evaluation rules.