Message Boards Message Boards

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

[?] Get Fourier series coefficients with for loop?

Posted 7 years ago

Hello everyone, I'd like to get the coefficients of the Fourier series with a for loop. I tried to do it in the following way:

For[i = -10, i < 11, i++, FourierCoefficient[Sin[t], t, i]]

but it does not work. Can you help me please?

Thank you very much.

POSTED BY: Gennaro Arguzzi
8 Replies
POSTED BY: Michael Rogers

Something went horribly wrong with this post -- so I will repost.

Gennaro,

The reason relates to the structure of the Wolfram Language. WL is list-based (similar to LISP or SCHEME). The power of these languages is in handling operations over lists. Furthermore, these languages do not distinguish between program and data -- you can combine them -- for example you can have a list of programs or a list with both data (such as equations) and programs (functions).

You can program WL sequentially (using DO and FOR) but you lose much of the power of the language and operations are slower because EL is optimized for lists. It is best to replace FOR with MAP or its related functions. You will also find that the programs become simpler if you avoid looping constructs and move to list-based constructs.

I hope this helps.

Regards

POSTED BY: Neil Singer

Build in functions work faster than program code.

POSTED BY: Ivan Siahlo

Gennaro, a side note. Stephen Wolfram mentions in his book "An Elementary Introduction To The Wolfram Language" on page 237 that the For command is almost always a bad idea, and can almost always be replaced by much cleaner code using constructs such as Table.

POSTED BY: Ed Forrester

Hi @Ed Forrester. Do you know why for command is bad?

POSTED BY: Gennaro Arguzzi

Sorry, I don't know the reason. I was alerting you to Stephen Wolfram's statement since you were using a For command to solve your problem.

Regards

POSTED BY: Ed Forrester

Fourier series coefficients with for loop

POSTED BY: Neil Singer

Gennaro, try this: Table[FourierCoefficient[Sin[t], t, i], {i, -10, 10}] Regards,

POSTED BY: Ed Forrester
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