# Average of variable

**URL:** <https://community.freefem.org/t/average-of-variable/2607>\
**Category:** General Discussion\
**Created:** [July 17, 2023, 5:33am UTC](https://community.freefem.org/t/average-of-variable/2607 "2023-07-17T05:33:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zese](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zese/32/917_2.png) [@zese](https://community.freefem.org/u/zese)\
**Post date:** [July 17, 2023, 5:33am UTC](https://community.freefem.org/t/average-of-variable/2607/1 "2023-07-17T05:33:59Z")

</div>

Dear all

I want to take the area average of my velocity field in freefem++ code. is it enough to write  
uAverage=int2d(th)(ux)/Area ?  
I think something is wrong and the integral must be area weighted.  
Do you know how I can write it?

BR

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [July 17, 2023, 6:54am UTC](https://community.freefem.org/t/average-of-variable/2607/2 "2023-07-17T06:54:29Z")

</div>

Sounds like you have the right approach:  
`real uAverage = int2d(th)(ux)/int2d(th)(1.0);`

---

<div class="post-metadata">

**Author:** ![zese](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zese/32/917_2.png) [@zese](https://community.freefem.org/u/zese)\
**Post date:** [July 17, 2023, 6:57am UTC](https://community.freefem.org/t/average-of-variable/2607/3 "2023-07-17T06:57:42Z")

</div>

Thank you.  
I also wrote it in another way witch was comparably in good agreement with Tecplot. I write it here if anyone needs.  
int nbtriangle=th.nt;

real[int] uxA(nbtriangle);  
uxA=0.;  
real uxAverage=0.;  
for (int i=0; i\<nbtriangle; i++)  
{  
for (int j=0; j\<3; j++)  
{  
real X=th[i][j].x;  
real Y=th[i][j].y;  
uxA(i)=velocity(X,Y)+uxA(i);  
}  
uxA(i)=(uxA(i)/3)\*th[i].area;  
uxAverage=uxA(i)+uxAverage;  
}  
uxAverage=uxAverage/(th.area);  
cout\<\<“Average=”\<\<uxAverage\<\< " " \<\< “THAREA=”\<\< th.area \<\<endl;

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [July 17, 2023, 7:59am UTC](https://community.freefem.org/t/average-of-variable/2607/4 "2023-07-17T07:59:07Z")

</div>

This approach may work in your case, but this type of loop is slow and prone to rounding errors on large meshes. Additionally, this definition of `uxA` does not use a proper quadrature and may be very wrong for elements with bad aspect ratios. In general, I would recommend the other approach.

---

<div class="post-metadata">

**Author:** ![zese](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/zese/32/917_2.png) [@zese](https://community.freefem.org/u/zese)\
**Post date:** [July 17, 2023, 8:01am UTC](https://community.freefem.org/t/average-of-variable/2607/5 "2023-07-17T08:01:37Z")

</div>

Thank you very much Chris  
you are right it is slow.  
Your approach worked very well.
