Message Boards Message Boards

0
|
8183 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

[?] Avoid error with NSum : it returns NSum::nsnum: Summand f[n]?

Posted 5 years ago

Consider the following example (I had a lot of trouble to find a minimal working example, I think it is compactified enough now).

Omega0 = 1.
nAvg = 10.
t=2.
Omegan[n_] := Omega0*Sqrt[n + 1]
f[n_] := Piecewise[{{Cos[Omegan[n]*t/2]^2*
     Abs[Exp[-nAvg/2]*Sqrt[nAvg]^n/Sqrt[Factorial[n]]], 
    0 <= n <= 20}}, 0]
NSum[f[n], {n, 1, 100}]

If you run this short script, it should return you :

NSum::nsnum: Summand (or its derivative) f[n] is not numerical at point n = 17.

This problem I am facing occurs only with some specific function. It occurs with this complicated looking function I gave you but if you try simpler one the script may just work correctly.

My questions :

First: I would like to understand why I have this error.

Second: How to solve it ?

In short : how to do numeric summation in general with mathematica ? In my specific case I have functions that may be piecewise defined. In all generality my function can be a product/sum of piecewise functions so it is not obvious at first view to know the boundary of the sum without looking more carefully, which I would like to avoid.

POSTED BY: M F
5 Replies

For NSum, you can use a customized method: NSum[f[n], {n, 1, 100}, Method -> "WynnEpsilon"] from this SE

POSTED BY: Shenghui Yang

I'm going to have ask the same question as Rohit. Is there a particular reason why NSum? Total[Table[]] also works. Sometimes if you error one way, finding an alternative route may just be the fastest way to resolve the problem.

In[7]:= Total[Table[f[n], {n, 1, 100}]]
Out[7]= 2.69827
POSTED BY: Isaac Sarver
Posted 5 years ago

Is there a reason for using NSum? Sum works

Sum[f[n], {n, 1, 100}]
(* 2.69827 *)
POSTED BY: Rohit Namjoshi

your problem is that f[17] is not numerical. The error is accurate. Type the following:

In[6]:= f[17]

Out[6]= 0.112978 Cos[2.12132 t]^2

The variable "t" is not defined so the result is an expression and not a number. NSum expects numerical results.

Regards,

Neil

POSTED BY: Neil Singer
Posted 5 years ago

Sorry I forgot to copy paster the line defining t.

Add t=2 before and you will have the same problem.

POSTED BY: M F
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract