1
$\begingroup$

I'm trying to play with central limit theorem using Mathematica, so I wrote the following code,

f[x_] := UnitBox[x];  
For[i = 1, i < 4, i++,  
   f[x_] := Convolve[UnitBox[z], f[z], z, x]; 
    ]
Plot[f[x],{x,-3,3}]

hoping to produce a 3-fold convolution of the original function, but only to produce the following error notifications:

$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of UnitBox[z].
$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of UnitBox[z].
$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of UnitBox[z].
General::stop: Further output of $RecursionLimit::reclim2 will be suppressed during this calculation.

but I don't know how to comprehend this. What exactly is wrong with the code?

$\endgroup$
  • $\begingroup$ Have you seen this? $\endgroup$ – J. M. is away Apr 8 '17 at 5:32
  • $\begingroup$ @J.M., I saw that, but I'm too green to mathematica to understand the details of that post. $\endgroup$ – Jia Yiyang Apr 10 '17 at 17:52
  • $\begingroup$ I think that because you use 'SetDelayed' rather than 'Set', only the last assignment to 'f (x)' has any effect. Consequently, when you evaluate it, it calls itself recursively, without limit. $\endgroup$ – mikado Apr 10 '17 at 18:33
  • $\begingroup$ At the very least, did you see that you can use B-splines for this? $\endgroup$ – J. M. is away Apr 10 '17 at 23:11
  • $\begingroup$ @J.M., I saw your answer, but I don't understand how B-spline works even after reading the Wiki introduction to B-splines. $\endgroup$ – Jia Yiyang Apr 12 '17 at 2:19
1
$\begingroup$

The following is a more recursive solution

g[0] = UnitBox;
g[n_] := g[n] = Function[{x}, 
   Evaluate[Module[{z}, Convolve[UnitBox[z], g[n - 1][z], z, x]]]]

Plot[g[3][x], {x, -3, 3}]

enter image description here

$\endgroup$
  • $\begingroup$ This works very well, thanks and +1. But exactly what's wrong with my original code? $\endgroup$ – Jia Yiyang Apr 10 '17 at 17:52
  • $\begingroup$ After some testing it seems what made the difference is "Evaluate", i.e. my code would also work if I enclose the "Convolve" with an "Evaluate". But I don't really understand the operating principle here. $\endgroup$ – Jia Yiyang Apr 10 '17 at 17:59
  • $\begingroup$ When learning Mathematica, I found making it evaluate my functions in the order I intended was one of the biggest challenges. I admit that I gave you something that worked rather than trying very hard to understand what you had done. $\endgroup$ – mikado Apr 10 '17 at 18:27
3
$\begingroup$
g[z_] := Nest[Convolve[#, UnitBox[x], x, z] &, UnitBox[x], 3];
Plot[g[z], {z, -2, 2}]

enter image description here

And try this, which is more instructive:

g[z_] := Nest[Convolve[#, UnitTriangle[x], x, z] &, UnitTriangle[x], 3];
Plot[g[z], {z, -2, 2}]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Better to define g with Set than with SetDelayed for this function; particularly so for the UnitTriangle version. $\endgroup$ – Edmund Apr 8 '17 at 2:57
  • $\begingroup$ Better to include option, Exclusions -> None for the plot. It joins the skip zones. $\endgroup$ – Carl Apr 8 '17 at 3:05
  • 1
    $\begingroup$ Thanks for the answer. But does the first code block really produce a 3-fold convolution of UnitBox? I would expect the result of 3-fold convolution to be a (piece-wise) quadratic function. $\endgroup$ – Jia Yiyang Apr 8 '17 at 3:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.