0
$\begingroup$

I want to solve a convolution integral numerically:

Dx=1;tau=5;
Ex[x_] := (1/Sqrt[4*Pi*Dx*x])*Exp[-((x - tau)^2/(4*Dx*x))];
Fnum[x_]:=NIntegrate[A[xprime]*Ex[x-xprime], {xprime, 0, x},MaxRecursion->20] 

As a test, I am using a step function for the function A[x] by setting

A[x_]:=If[x0<=x<=x0+dx, A0, 0]

where, for instance, x0=0; dx=1; A0=1; In this specific case, the integral can also be solved analytically and the result is Fan[x_]:=A0*dx*Ex[x-x0]. However, when I plot, I notice that the analytical solution and the numerical solution are different.

Plot[{Fnum[x], Fan[x]}, {x, 0, 30}, PlotRange -> {{0, 30}, All}, 
 GridLines->Automatic, PlotStyle->Thick,ImageSize->Large]

enter image description here How is this possible?

$\endgroup$
3
  • 3
    $\begingroup$ Your analytical result is incorrect. Take for example: Ex[x_] := 1, and observe the discrepancy. Your result is correct only in the $\mathrm d x \to 0$ limit. $\endgroup$ Commented May 27, 2024 at 14:11
  • 2
    $\begingroup$ How do you calculate Fan[x_]? $\endgroup$ Commented May 27, 2024 at 14:26
  • $\begingroup$ ultimately I think that my calculation of Fan[x_] was wrong $\endgroup$ Commented May 28, 2024 at 11:03

1 Answer 1

3
$\begingroup$

Using

A[x_] := Piecewise[{{A0, x0 <= x <= x0 + dx}, {0, True}}]

you get

Assuming[x > 0, Integrate[A[xprime]*Ex[x - xprime], {xprime, 0, x}]]

$$\begin{cases} \frac{1}{2} \left(\text{erf}\left(\frac{6-x}{2 \sqrt{x-1}}\right)+\text{erf}\left(\frac{x-5}{2 \sqrt{x}}\right)+e^5 \left(\text{erf}\left(\frac{x+5}{2 \sqrt{x}}\right)-\text{erf}\left(\frac{x+4}{2 \sqrt{x-1}}\right)\right)\right) & x>1 \\ \frac{1}{2} \left(\text{erfc}\left(-\frac{x-5}{2 \sqrt{x}}\right)-e^5 \text{erfc}\left(\frac{x+5}{2 \sqrt{x}}\right)\right) & \text{True} \end{cases}$$

convolution numerical and algebraic

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.