       RE: Re: fourier transform time

• To: mathgroup at smc.vnet.net
• Subject: [mg36467] RE: [mg36439] Re: fourier transform time
• From: "DrBob" <drbob at bigfoot.com>
• Date: Sun, 8 Sep 2002 03:30:44 -0400 (EDT)
• Reply-to: <drbob at bigfoot.com>
• Sender: owner-wri-mathgroup at wolfram.com

```Even better -- MUCH better -- add Bob Hanlon's Evaluate to the other tricks:

ClearAll[f, b, FS]
L = 2;
f[x_] := UnitStep[x - 1];
b[n_] := b[n] = N[(2*(Cos[(n*Pi)/2] - (-1)^n))/(n*Pi)];
FS[N_, x_] := Sum[b[n]*Sin[n*Pi*(x/L)], {n, 1, N}];
Timing[Plot[Evaluate[FS[30, x]], {x, 0, 2}]]

{0.015 Second, â??Graphicsâ??}

ClearAll[f, b, FS]
L = 2;
f[x_] := UnitStep[x - 1];
b[n_] := b[n] = N[(2*(Cos[(n*Pi)/2] - (-1)^n))/(n*Pi)];
FS[N_, x_] := Sum[b[n]*Sin[n*Pi*(x/L)], {n, 1, N}];
Timing[Plot[Evaluate[FS[120, x]], {x, 0, 2}]]

{0.063 Second, â??Graphicsâ??}

Bobby Treat

-----Original Message-----
From: DrBob [mailto:drbob at bigfoot.com]
To: mathgroup at smc.vnet.net
Subject: [mg36467] RE: [mg36439] Re: fourier transform time

With the second method below (mine), the times add up to 40% less than with the first method (Jens-Peer's), for what SEEMS to be exactly the same work.  Go figure!

Can anybody explain that?

(* Jens-Peer *)

ClearAll[f, b, FS]
L = 2;
f[x_] := UnitStep[x - 1];
b[n_] := 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}]]

{0.547 Second, â??Graphicsâ??}

(* Treat #1 *)

Timing[(2/L)*Integrate[f[x]*Sin[n*Pi*(x/L)], {x, 0, L}]]

{0.016000000000000014*Second, (2*(Cos[(n*Pi)/2] - Cos[n*Pi]))/(n*Pi)}

ClearAll[f, b, FS]
L = 2;
f[x_] := UnitStep[x - 1];
b[n_] := b[n] = (2*(Cos[(n*Pi)/2] - Cos[n*Pi]))/(n*Pi);
FS[N_, x_] := Sum[b[n]*Sin[n*Pi*(x/L)], {n, 1, N}];
Timing[Plot[FS[30, x], {x, 0, 2}]]

{0.297 Second, â??Graphicsâ??}

Even stranger, it SLOWS the Plot if we precompute b before Timing starts:

(* Treat #2 *)

ClearAll[f, b, FS]
L = 2;
f[x_] := UnitStep[x - 1];
b[n_] := b[n] = (2*(Cos[(n*Pi)/2] -
(-1)^n))/(n*Pi);
b /@ Range;
FS[N_, x_] := Sum[b[n]*Sin[n*Pi*(x/L)],
{n, 1, N}];
Timing[Plot[FS[30, x], {x, 0, 2}]]

{0.328 Second, â??Graphicsâ??}

But here's a winner:

(* Treat #3 *)

ClearAll[f, b, FS]
L = 2;
f[x_] := UnitStep[x - 1];
b[n_] := b[n] =
N[(2*(Cos[(n*Pi)/2] - (-1)^n))/
(n*Pi)];
FS[N_, x_] := Sum[b[n]*Sin[n*Pi*(x/L)],
{n, 1, N}];
Timing[Plot[FS[30, x], {x, 0, 2}]]

{0.204 Second, â??Graphicsâ??}

Apparently, computing b within the Plot causes machine-precision arithmetic to be used, and that saves time.  Precomputing b and then converting exact expressions to approximate ones within the Plot seems to take longer.  For that to make sense, I think it must be that n is approximate (not Integer) when it is passed to b within Plot.

Bobby Treat

-----Original Message-----
From: Jens-Peer Kuska [mailto:kuska at informatik.uni-leipzig.de]
To: mathgroup at smc.vnet.net
Subject: [mg36467] [mg36439] Re: fourier transform time

Hi,

with your code you compute the expansion coefficents every
time when FS[] is evaluated. Store the values for b[n] with

L = 2;
f[x_] := UnitStep[x - 1];
b[n_] := 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}]]

and you need only a 1-3 seconds (depending on your machine)

Regards
Jens

Steve Story wrote:
>
> 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=
> {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,
>
> Steve Story

```

• Prev by Date: RE: Re: fourier transform time
• Next by Date: RE: Solve of inverse quadratic finite element problem - resend
• Previous by thread: RE: Re: fourier transform time
• Next by thread: Re: Generating Two Unit Orthogonal Vectors