6.32 animate and evaluation of variables (9.7.96)

6.32.1 Mike May
6.32.2 Robert Israel (16.7.96)
6.32.3 Jan-Moritz Franosch (17.7.96)
6.32.4 Dan Dubois (17.7.96)

6.32.1 Mike May

I have started to play with Maple Version V4. I have been having some trouble with the animate command. There are some obvious nice animations for calculus where you string together approximations, either with sums, like rightbox, or with series, like taylor.

My problem is that when I try to use these with animate, I get an error indicating a parameter is a wrong type. (Taylor wants a natural number for degree. The parameters in animate seem to be automatically real.)

*************sample code****************** 
 
> with(plots):convert(taylor(sin(x),x=0, 8), polynom); 
> plot({sin(x), convert(taylor(sin(x),x=0, 8), polynom)}, x=-3..6, 
       y=-1..1); 
> animate({sin(x), convert(taylor(sin(x),x=0, n), polynom)}, n=2..10, 
          frames=9, view=[-3..5, -1..1]); 
Error, wrong number (or type) of parameters in function taylor
 

6.32.2 Robert Israel (16.7.96)

Actually, the main problem is one of premature evaluation. The arguments to "animate", like those of nearly all functions, are evaluated before "animate" is called. Thus "taylor" is called with third argument the symbolic variable n. Unfortunately, you can’t use the usual remedy of quoting the offending expression:

> animate({sin(x), 'convert(taylor(sin(x),x=0, n), polynom)'}, x = -3 .. 5, 
           n = 2 .. 10, frames = 9);
 

because this will have the opposite problem: when "taylor" is called, n will be assigned a numerical value, but so will x. If you’re determined to use "animate", you might do it as follows (note the use of "round" to make the n values into integers):

> animate({sin(x), 'subs(t=x,convert(taylor(sin(t),t=0, round(n)), polynom))'}, 
       x = -3 .. 5, n = 2 .. 10, frames = 9);
 

A simpler approach is to use "display(..., insequence=true)". Note that "seq" assigns values to the index variable before evaluating its first argument.

> display([ seq( plot({sin(x), convert(taylor(sin(x), x = 0, n), polynom)}, 
          x = -3 .. 5), n = 2 .. 10) ], insequence=true);
 

The second approach should also be more efficient, since you only call "taylor" once for each n.

6.32.3 Jan-Moritz Franosch (17.7.96)

I just found a solution but I still don’t know why it must be so complicated:

with(plots); 
p:=proc(x,n) 
  local i,y,expr; 
  i:=trunc(n); 
  expr:=convert( taylor( sin(y),y=0,i ), polynom ); 
  RETURN(expr); 
end; 
animate( 'p(x,n)', x=-3..5, n=2..10, frames=9, view=[-3..5,-1..1] );
 

The quotes ('p(x,n)') are necessary because animate seems to try to evaluate p(x,n) and this leads to the error you described because n is even no number.

Replacing x by y in the procedure is necessary because something like taylor( sin(1.7),1.7=0,5 ) is not possible of course.

I think plot should be improved so it can be used more easy in such situations.

6.32.4 Dan Dubois (17.7.96)

The taylor() routine does expect a non-negative integer as a third argument. You may wish to plot the integer values between 2 and 8 and animate them using the display() routine:

    |\^/|     Maple V Release 4 (WM - Internal Use Only) 
._|\|   |/|_. Copyright (c) 1981-1996 by Waterloo Maple Inc. All rights 
 \  MAPLE  /  reserved. Maple and Maple V are registered trademarks of 
 <____ ____>  Waterloo Maple Inc. 
      |       Type ? for help. 
> with(plots):convert(taylor(sin(x),x=0, 8), polynom); 
                                3          5           7 
                       x - 1/6 x  + 1/120 x  - 1/5040 x 
 
 
> myproc:=proc(n) convert(taylor(sin(x),x=0, n), polynom); end; 
        myproc := proc(n) convert(taylor(sin(x), x = 0, n), polynom) end 
 
 
> for i from 2 to 8 do 
> p[i]:=plot({sin(x),myproc(i)},x=-3..5,y=-1..1): 
> od: 
 
> display([seq(p[j],j=2..8)],insequence=true); 
[...]