Pregunta

I want to write in Maple Taylor series for cosinus function. Here's my code:

better_cos := proc (x) options operator, arrow; sum((-1)^n*x^(2*n)/factorial(2*n), n = 0 .. 20) end proc;

better_cos(0) returns 0 instead of 1 (cos(0) == 1). It's probably because x^(2*n) return always 0 instead of 1. For example:

fun_sum := proc (x) options operator, arrow; sum(x^(2*n), n = 0 .. 0) end proc

return 0 for x == 1.

It's weird because 0^0 returns 1. Do you have any idea how can I correctly implement taylor series for cosinus?

¿Fue útil?

Solución

You should be able to get what you want by using add instead of sum in your better_cos operator.

Using add is often more appropriate for adding up a finite number of terms of a numeric sequence, and also note that add has Maple's so-called special evaluation rules.

If you intend to take the sum of a fixed number of terms (ie. n from 0 to 20) then you should not write a procedure that computes the factorials for each input argument (ie. for each value of x). Instead, produce the truncated series just once, and then use unapply to produce an operator. This approach also happens to deal with your original problem, since the x^0 term becomes 1 because the symbol x is used.

You could also rearrange the polynomial (truncated series) so that it is in Horner form, to try and minimize arithmetic steps when evaluating subsequently at various numeric values of x.

For example, using 5 terms for brevity instead of 20 as you had it,

convert(add((-1)^n*x^(2*n)/factorial(2*n), n = 0 .. 5),horner);

     /  1   /1    /   1    /  1        1     2\  2\  2\  2\  2
 1 + |- - + |-- + |- --- + |----- - ------- x | x | x | x | x 
     \  2   \24   \  720   \40320   3628800   /   /   /   /   

bc := unapply(%,x):

You can now apply the procedure bc as you wish, either with symbolic or numeric arguments.

expand(bc(x));

       1  2   1   4    1   6     1    8      1     10
   1 - - x  + -- x  - --- x  + ----- x  - ------- x  
       2      24      720      40320      3628800    

bc(0);
                           1

bc(1.2);
                      0.3623577360

If you prefer to have your procedure better_cos take a pair of arguments, so that the number of terms is variable, then you could still consider using add to deal with your original problem. eg,

bc := (x,N)->add((-1)^n*x^(2*n)/(2*n)!, n = 0 .. N):

I suppose that this is a homework assignment, and that you realize that you could also use the existing system commands taylor or series to get the same results, ie.

convert(series(cos(x),x=0,10),polynom);

              1  2   1   4    1   6     1    8
          1 - - x  + -- x  - --- x  + ----- x 
              2      24      720      40320   

convert(taylor(cos(x),x=0,10),polynom);

              1  2   1   4    1   6     1    8
          1 - - x  + -- x  - --- x  + ----- x 
              2      24      720      40320   

Otros consejos

Here's the Taylor series definition:

enter image description here

Don't start the loop with zero; initialize with one and start at two.

Factorial is inefficient, too.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top