Re: fourier transform time

• To: mathgroup at smc.vnet.net
• Subject: [mg36451] Re: [mg36425] fourier transform time
• From: BobHanlon at aol.com
• Date: Sat, 7 Sep 2002 02:54:11 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```In a message dated 9/6/02 5:14:56 AM, sbstory at unity.ncsu.edu writes:

>For my PDE class we have been calculating Fourier transforms. The instructor
>arrived today with a printout of two plots of a certain Fourier transform,
>done with a different CAS. The first plot was to 30 terms, the second was
>to
>120 terms.
>    Curious, I translated the functions into Mathematica (4.0 on Windows2000
>on a PIII 700) to see how much time this required to process. I was
>Staggered at how much time it took. Here's the code:
>
>L = 2;
>f[x_] := UnitStep[x - 1];
>b[n_] := (2/L)*Integrate[f[x]*Sin[n*Pi*x/L], {x, 0, L}];
>
>FS[N_, x_] := Sum[b[n]*Sin[n*Pi*x/L], {n, 1, N}];
>
>Timing[Plot[FS[30, x], {x, 0, 2}]]
>
>Out[23]=
>{419.713 Second, \[SkeletonIndicator]Graphics\[SkeletonIndicator]}
>
>In this case the number of terms is 30.
>
>The time required per number of terms seems to fit the following polynomial:
>
>y = 0.3926x^2 + 2.2379x
>
>This is a large amount of time. I understand that the code is not optimized,
>and was more or less copied from the code in the other CAS, but is this
>a
>reasonable amount of time, or is something going wrong?  I don't use
Mathematica
>because of the speed, but should it be this slow?
>
>Just curious,

Your definition of b recalculates the integral for every call.  To evaluate
the integral once
include Evaluate.

Clear[f, b, FS];

L = 2;

f[x_] := UnitStep[x - 1];

b[n_] := Evaluate[
(2/L)*Integrate[f[x]*Sin[n*Pi*x/L], {x, 0, L}]];

In the case of FS it is best to wait for an integer value of N prior to
performing the Sum.

FS[N_Integer, x_] := Sum[b[n]*Sin[n*Pi*x/L],
{n, 1, N}];

Now Evaluate the argument of the Plot to cause the Sum to be done once.

Timing[Plot[Evaluate[FS[120, x]], {x, 0, 2}]][[1]]

0.366667 Second

While my computer may be faster than yours, this result for N=120 is 1000
times faster than your result for N=30.

Bob Hanlon

```

• Prev by Date: RE: Plot several graphs generated by Table
• Next by Date: Re: fourier transform time
• Previous by thread: fourier transform time
• Next by thread: Re: fourier transform time