mjr205
mjr205

Reputation: 11

Mathematica running very slowly with just 4 integrals. Memoization?

While playing with Fourier Integrals, I've noticed my computations taking an excessively long time to calculate. I just have two sets of a Piecewise function, two functions and two integrals. I wouldn't think Mathematica would need to take so long to compute these. What, in particular, is slowly my computations down so much? How can I improve computation time? Will using some combination of memoization help ( :=, for which I don't quite understand)?

Decreasing the Limit on the integrals helps slightly, but not as much as I'd think.

My code is the following:

    MM = 50;
    qf = 10;

    (*First Set*)
    f[t_] = Piecewise[{{1, t <= 0.5}, {0, t > 0.5}}];
    A[w_] = qf Sin[w/2]/(Pi w);
    B[w_] = qf (1 - Cos[w/2])/(Pi w);
    f[x_] = Integrate[A[w] Cos[w x], {x, 0, MM}];
    g[x_] = Integrate[B[w] Sin[w x], {x, 0, MM}];
    Plot[f[x], {x, 0, 1}]
    Plot[g[x], {x, 0, 1}]
    Plot[f[x] + g[x], {x, 0, 1}]

    (*Second Set*)
    ff[t_] = Piecewise[{{1, t <= 0.5}, {0, t > 0.5}}];
    AA[w_] = qf (Sin[w] - Sin[w/2])/(Pi w);
    BB[w_] = -qf (Cos[w] - Cos[w/2])/(Pi w);
    ff[x_] = Integrate[AA[w] Cos[w x], {x, 0, MM}];
    gg[x_] = Integrate[BB[w] Sin[w x], {x, 0, MM}];
    Plot[ff[x], {x, 0, 1}]
    Plot[gg[x], {x, 0, 1}]
    Plot[ff[x] + gg[x], {x, 0, 1}]

Upvotes: 1

Views: 455

Answers (1)

Avi Ginsburg
Avi Ginsburg

Reputation: 10596

You have several problems with your code as shown:

  1. You haven't defined w so e.g. f[x_] isn't fully evaluated
  2. you define f[t_] then f[x_] which overwrites the first definition
  3. You're misusing Piecewise. It should be something like pw = Piecewise[{{Sin[x]/x, x < 0}, {1, x == 0}}, -x^2/100 + 1]

Upvotes: 1

Related Questions