0

I am trying to numerically find the error of a truncated series. To do so I want to first define a function that is the truncated sum, I cannot seem to do that. All I am tried is not working.

f[x_]:=Normal[Series[ArcTan[x], {x, 0, 100}]]

Does not work as f[1] evaluates $x$ inside $\arctan$. I also tried

Normal[Series[ArcTan[x], {x, 0, 100}]]
f[x_]:=%

But that did not work. When I copy and paste the result of

Normal[Series[ArcTan[x], {x, 0, 100}]]

It works fine. What am I doing wrong?

2132123
  • 657
  • 3
  • 9
  • 3
    If you use ".=" the RHS is evaluated later, but you want immediate evaluation with "=": f[x_] = Normal[Series[ArcTan[x], {x, 0, 100}]] – Daniel Huber Nov 30 '20 at 21:56
  • @DanielHuber wow that worked! I thought the difference is "=" also outputs f[x] while ":=" defines f without outputting f. There more you know! Thank you very much. – 2132123 Nov 30 '20 at 21:57
  • 3
    Or use Evaluate: f[x_] := Evaluate[Normal[...]]. Note that x gets evaluated this way as well as @Daniel's way. If x has a value the definition won't work. In that case, try Block[{x}, f[x] = Normal[..]] – Michael E2 Nov 30 '20 at 21:58
  • 1
    Related: https://mathematica.stackexchange.com/questions/8829/what-is-the-difference-between-set-and-setdelayed – Michael E2 Nov 30 '20 at 22:01
  • @2132123 in any case, if you don’t want the output, you would just use a semi-colon delimiter to suppress the output :) – CA Trevillian Dec 01 '20 at 01:11

2 Answers2

1

It is recommend that define Taylor Series by hand since we can also treat multivariable Taylor series at the same time.

taylorMulti[f_, x_, x0_, n_] := 
  Sum[1/i! Dot[
     D[f, {{Sequence @@ x}, i}] /. 
      Thread[{Sequence @@ x} -> {Sequence @@ x0}], 
     Sequence @@ 
      ConstantArray[{Sequence @@ x} - {Sequence @@ x0}, i]], {i, 0, 
    n}];
taylorMulti[Exp[t], t, 0, 5]
taylorMulti[Exp[t], {t}, {0}, 5]
taylorMulti[Exp[t - s], {t, s}, {0, 0}, 3] // Expand

1 + t + t^2/2 + t^3/6 + t^4/24 + t^5/120

1 - s + s^2/2 - s^3/6 + t - s t + (s^2 t)/2 + t^2/2 - (s t^2)/2 + t^3/ 6

cvgmt
  • 72,231
  • 4
  • 75
  • 133
1

Try this:

f[x_] := Normal[Series[ArcTan[y], {y, 0, 20}]] /. y -> x;

Let us check:

f[0.1] // N
ArcTan[0.1]

(* 0.0996687

0.0996687 *)

f[1] // N ArcTan[1.]

(* 0.76046

0.785398 *)

The smaller is x the better is the coincidence.

Have fun!

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96
  • Could you explain what '''/. y -> x''' ? I think my problem is that I am trying to define a function that has inputs $x,n$ where it outputs evaluation of the truncated sum (n terms) at the point $x$. Using your code works ''' f[x_, k_] := Normal[Series[ArcTan[y], {y, 0, k}]] /. y -> x;'''. Is the issue normally that mathematica cannot expand arctan to $k$ terms without knowing what $k$ is? – 2132123 Dec 02 '20 at 20:16
  • The conflict here is in the part {x, 0, 100} of your code. It is since in the left-hand part stays f [x_] which means "the function f of any x". In this case Mma does not understand the iteration constructs when "any x" takes part in it. However, if I replace x by y, the operation Normal[Series[ArcTan[y], {y, 0, 100}]] will return a polynomial of y. And this is already a simple function, no iterators. Then one replaces y by x. This is done by .../.y->x. Have a look at Menu/Help/WolframDocumentation/ReplaceAll. – Alexei Boulbitch Dec 03 '20 at 09:58