4
$\begingroup$

I am a statistician who is very new to Mathematica. Ultimately I would like to be able to graph convolutions of arbitrary user defined functions. But I can't as yet obtain values of such convolutions even at single points, unless the convolution can be expressed in terms of builtin functions.

f[x_] := x^0.5 (1 - x)^0.4 UnitBox[x - 0.5]
g[y_] := Convolve[f[x], f[x], x, y]
g[0.2]

(* Out: Convolve[(1 - x)^0.4 x^0.5 UnitBox[-0.5 + x], (1 - 
    x)^0.4 x^0.5 UnitBox[-0.5 + x], x, 0.2] *)
$\endgroup$
  • $\begingroup$ Sometimes using the convolution theorem and FourierTransform can be successful even when Convolve is not. In this case, though, no such luck. The forward transform proceeds without much of a problem (after substituting HeavisidePi for UnitBox), but it produces a pretty awful-looking expression that cannot be inverse-transformed. So, I think a numerical result is the best you can hope for in this case. $\endgroup$ – Oleksandr R. Jan 10 '14 at 0:01
  • $\begingroup$ You can also define something which spits out a plot of the numerical convolution of two input functions computed using FFT's, using Fourier and InverseFourier. This is slightly less direct than just using NIntegrate, but it executes fast and still handles arbitrary function input. $\endgroup$ – DumpsterDoofus Jan 10 '14 at 1:20
4
$\begingroup$

I think the problem is that it could not do the symbolic integration. Meanwhile, you could always do the convolve directly, since it is just an integration, using NIntegrate. Unless you are looking for a closed form expression of conv(f,g). It would be more efficient to find the conv(f,g) expression, and then evaluate it for different $y$ values than having to do the integration for each increment of $y$, but your function $f(x)$ does not seem to be easy to integrate analytically.

Here is conv(f,g) of your two functions in blue, and the red dashed curve is $f(x)$.

enter image description here

Here is a quick manipulate. You can change $f(x)$ and $g(x)$ definitions below to try different functions.

 Manipulate[
 Grid[{
   {Show[
     Plot[conv[y], {y, -1, to}, ImageSize -> 500, ImagePadding -> 30, Frame -> True],
     Plot[f[x], {x, -1, 3}, PlotStyle -> {Dashed, Red}], PlotRange -> All
     ]}
   }],
 Row[{Control[{{to, 3, "to?"}, 0, 3, .1, ImageSize -> Small}], Dynamic[to]}],
 ContinuousAction -> False,
 SynchronousUpdating -> True,
 Initialization :> 
  (
   (*f[x_?NumericQ]:=Exp[-x]UnitStep[x];*)
   (*g[x_?NumericQ]:=UnitStep[x];*)
   f[x_?NumericQ] := x^0.5 (1 - x)^0.4 UnitBox[x - 0.5];
   g[x_?NumericQ] := f[x];
   conv[y_?NumericQ] := Module[{x}, NIntegrate[f[x]*g[y - x], 
       {x, -Infinity, Infinity}, MaxRecursion -> 6, AccuracyGoal -> 4]
     ];
   )
 ]

Mathematica graphics

$\endgroup$
  • $\begingroup$ I am very grateful for this detailed solution. I'm sure I will learn a lot by studying it. $\endgroup$ – Frank Jan 10 '14 at 0:50
4
$\begingroup$

As Nasser has observed numerical integration works. The following plots (f[x],f[y-x] f[x]f[y-x[ and convolution below) for varying y and thus gives insight into convolution).

enter image description here

where convolution was obtained:

g[y_] := NIntegrate[f[x] f[y - x], {x, -Infinity, Infinity}]
$\endgroup$

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.