Message Boards Message Boards

0
|
11663 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

Here are some examples of the type of for-loops I've seen from those coming to Mathematica from imperative programming languages like C/Java, where for-loops dominate. I'll present non-for-loop equivalents, in some cases with timings, which will be seen to be an significant difference.

Iterate a command f[] (here f is a no-op, so the timing reflects the overheads of For and Do):

ClearAll[f, i];
For[i = 1, i <= 10^6, i++, f[i]]; // AbsoluteTiming  (*  i  is a global variable *)
i

Clear[i]
Do[f[i], {i, 10^6}]; // AbsoluteTiming  (*  i  is a local variable *)
i
(* Out[]=
  {1.01164, Null}
  1000001

  {0.226265, Null}
  i
*)

Note that i is not localized in For. The user has to localize i explicitly to get an equivalent behavior of Do:

Block[{i}, For[i = 1, i <= 10^6, i++, f[i]]] (* user-localized i *)

Compute some data:

For[i = 1, i <= 10, i++, Print[Sin[0.1 i]]];

data = Table[Sin[0.1 i], {i, 10}];
data // TableForm

Note that the data is not stored in the For loop above, which is not at all a smart way to compute data, but I've seen it done often. Below is the typical way one would compute data. For the for-loop way, you first declare/allocate an array, and then fill it with values. With Table[], it's all handled for you and very efficiently.

(data = ConstantArray[0., 10^6]; (* pre-allocate array *)
  For[i = 1, i <= 10^6, i++,  data[[i]] = Sin[0.1 i]];) // AbsoluteTiming

data = Table[Sin[0.1 i], {i, 10^6}]; // AbsoluteTiming
(* Out[]=
  {2.61934, Null}
  {0.072018, Null}
*)

The time difference is staggering.

One reason For is less efficient is that it is a very general scheme. Any expression may be an iterator (3rd argument). The most common uses, however, are the ones shown (to iterate over a range of integers or to step through a list). Do and Table are optimized for these tasks. Personally, I've always felt For was mainly a convenience for translating C code (etc.) to Mathematica. Its greater generality does open the possibility that there might be some computations that are more naturally expressed with For. Maybe.

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