Message Boards Message Boards

[?] Force Manipulate[] outputs to be reals instead of complex numbers?

Posted 5 years ago

When evaluating the following, each even integer value of n produces a complex number in which the real component is the proper value:

Manipulate[(7 Sin[105 n/\[Pi]]*(-1)^n)/n + 10, {n, 1, 13, 1}] // N

When not using Manipulate[], the list contains only real numbers:

Table[(7 Sin[105 n/\[Pi]]*(-1)^n)/n + 10, {n, 1, 13, 1}] // N

What does Manipulate do that produces imaginary components for even values of n?

POSTED BY: Dave Markham
3 Replies

Thanks for the very understandable explanations, guys--I appreciate it! 8^)

POSTED BY: Dave Markham
Posted 5 years ago
Manipulate[N[(7 Sin[105 n/\[Pi]]*(-1)^n)/n + 10], {n, 1, 13, 1}]

With the ...//N after Manipulate also the value of the n's become machinenumbers, so that they are not exactly integers anymore and the (-1)^n becoms complex if n is not integer.

POSTED BY: Juerg Baertsch

It's because applying N to the whole Manipulate also causes the boundaries and the step for n to turn from integers into reals. You can see the same complex results if you evaluate the following:

Table[(7 Sin[105 n/\[Pi]]*(-1)^n)/n + 10, {n, 1, 13, 1.0}]

The only thing "special" about Manipulate (compared to Table) is that it doesn't resolve the step specification immediately, and that's why it's affected by the outer N.

If you apply N only inside the Manipulate body, you get only real values:

Manipulate[(7 Sin[105 n/\[Pi]]*(-1)^n)/n + 10 // N, {n, 1, 13, 1}]

BTW, note that the imaginary parts you see are not much more than rounding errors. You could also get rid of them using Chop.

POSTED BY: Jan Poeschko
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