5
$\begingroup$

I have this function

$$f(x)=\sum_{k=1}^{\lfloor x\rfloor}\tan(k),\quad x\ge 1$$

Then I tried to plot several graphs with this code

f[x_]:=Sum[Tan[k],{k,1,Floor[x]}]
Table[Plot[f[x],{x,1,10^n}],{n,2,10}]

but after half hour or so the computer hang on (memory issues) and I restarted it. Clearly the code is not the more efficient for this task.

My question is: can you give me some tips to improve the performance of mathematica to draw this function? By example, it is possible to define a recursion, instead of a function, and draw it? It seems more appropriate to use a recursion instead of a sum to draw this function.

$\endgroup$
6
$\begingroup$

One possibility is to use Accumulate and Interpolation to create the function:

if = Interpolation[Accumulate @ Tan[N @ Range[10^8]], InterpolationOrder->0]; //AbsoluteTiming

{79.0832, Null}

Now, we can plot if over various domains:

Plot[if[t], {t, 1, 10^2}]

enter image description here

Plot[if[t], {t, 1, 10^3}]

enter image description here

Plot[if[t], {t, 1, 10^8}]

enter image description here

If you need to go higher, then you will need to create a new InterpolatingFunction starting at 10^8.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.