# Convolution in 1d

**URL:** <https://community.freefem.org/t/convolution-in-1d/4077>\
**Category:** General Discussion\
**Created:** [September 22, 2025, 5:32pm UTC](https://community.freefem.org/t/convolution-in-1d/4077 "2025-09-22T17:32:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![smail](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@smail](https://community.freefem.org/u/smail)\
**Post date:** [September 22, 2025, 5:32pm UTC](https://community.freefem.org/t/convolution-in-1d/4077/1 "2025-09-22T17:32:25Z")

</div>

I need to compute a convolution in 1d \\int\_{0}^{t}g(t-s)u(s) ds for simplicity I take g=u=\\exp(-s), the exact convolution is t \\exp(-t). I tried to adapt the freedom code Convolution-Sample.edp to the 1D case, but the error approximation is large, as shown in the attached figure.

Attached here are

[conv1d-v1.edp](https://community.freefem.org/uploads/short-url/wDwNfcL14211M33Hsx5VKSKrEbb.edp) (529 Bytes)

[Convolution-Sample.edp](https://community.freefem.org/uploads/short-url/eMzzverZvHCzpTxYcJFlbIHKkfS.edp) (1.5 KB)

 ![conv1d](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/5/5b4cb876b08fb443f8d736e5c8cd62b106ad974f.png)

the two codes and the figure.

---

<div class="post-metadata">

**Author:** ![smail](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@smail](https://community.freefem.org/u/smail)\
**Post date:** [September 22, 2025, 11:02pm UTC](https://community.freefem.org/t/convolution-in-1d/4077/2 "2025-09-22T23:02:31Z")

</div>

Note that when the function g has a small compact support,

 ![smallsupport1](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/5/5cb6dd70b11cd63dd9e405f7aeb8a2c06691a8e4.png)

see the attached figure the result is the same that I obtained using chebfun/Matlab

 ![smallsuport2](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/1/1586eba521996f1243ca69130606e51f1d8944bd.png)

the matlab result

 ![smallsupport](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/b/bc635ed1d87d421f8596529c3978805779f2d07f.png)

---

<div class="post-metadata">

**Author:** ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)\
**Post date:** [September 23, 2025, 1:19pm UTC](https://community.freefem.org/t/convolution-in-1d/4077/3 "2025-09-23T13:19:51Z")

</div>

Hello,  
You need to take care of the domain of integration. Here is a correction

```auto
load "msh3"
load "Element_P3"
// Compute the convolution: (g*u)(t)= \int_{0}{t}g(t-s)u(s) ds
 
meshL Th=segment(200,[10*x]);
fespace Vh(Th,P3);

func g = (x>0)? exp(-x) : 0.;
func u = (x>0)? exp(-x) : 0.;
func v = x*exp(-x); // the exact convolution for u = g = exp(-s)
Vh vh=v;
Vh uh=u;
Vh gh=g;

func real conv1d(real a) { return int1d(Th)(gh(a-x)*uh); }
Vh convu = conv1d(x);
Vh xTh = x;

plot([xTh[],vh[]],[xTh[],convu[]], wait=true,cmm=" exact and approx u*g");
real err=sqrt(int1d(Th)((vh-convu)^2));
cout << " the L2 error is " << err << endl;

```

---

<div class="post-metadata">

**Author:** ![smail](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@smail](https://community.freefem.org/u/smail)\
**Post date:** [September 23, 2025, 1:51pm UTC](https://community.freefem.org/t/convolution-in-1d/4077/4 "2025-09-23T13:51:10Z")

</div>

> [@fb77](#):
>
> Hello,  
> You need to take care of the domain of integration. Here is a correction

I sincerely thank you, Prof. Bouchut, for your valuable help in identifying the error in my code. Your guidance was very helpful\*.\*

Best regards.
