# Confused about correct method

**URL:** <https://community.freefem.org/t/confused-about-correct-method/2758>\
**Category:** General Discussion\
**Created:** [October 25, 2023, 1:13pm UTC](https://community.freefem.org/t/confused-about-correct-method/2758 "2023-10-25T13:13:29Z")\
**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:** [October 25, 2023, 1:13pm UTC](https://community.freefem.org/t/confused-about-correct-method/2758/1 "2023-10-25T13:13:29Z")

</div>

Dear all,

I have a question about different methods of calculation of a relation.  
my relation is

```auto
fespace Xh(th,P2);
Xh U,F,GradJ;
real alpha;
GradJ=alpha*F+U;

```

I have F and U from previous solutions of PDE equations and just want to add them together with weight alpha.  
now, I am confused if I write it as follow, answers are different. I don’t know which one is correct.

```auto
varf vf(unused,v)=int2d(th)((alpha*F+U)*v);
gradJ[]= vf(0,Xh);

```

---

<div class="post-metadata">

**Author:** ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)\
**Post date:** [October 26, 2023, 7:44am UTC](https://community.freefem.org/t/confused-about-correct-method/2758/2 "2023-10-26T07:44:00Z")

</div>

Probably the easiest thing to do is try a small mesh with known data.  
The both expressions are fairly literal - The first one is just as written scaled F + U.  
The second one integrates of the test functions it is the RHS for a FEM  
problem in form Ax=b . I guess you probably want the first expression.

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [October 26, 2023, 11:00am UTC](https://community.freefem.org/t/confused-about-correct-method/2758/3 "2023-10-26T11:00:00Z")

</div>

I think you just need to add the functions together.  
I think the most efficient way is the following

```auto
GradJ[] = F[];
GradJ[] *= alpha;
GradJ += U[];

```

This way you are just adding and multiplying the vectors. If you do `GradJ=alpha*F+U;`, then the functions are interpolated.

---

<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:** [October 27, 2023, 12:35pm UTC](https://community.freefem.org/t/confused-about-correct-method/2758/4 "2023-10-27T12:35:54Z")

</div>

Thanks aszaboa,

Actually my case is axisymmetric and I do the optimization. I was not sure to compute gradients for descent direction, should I use variational form (1st method) and multiply it by r (radius) or just use the second method. In non axisymmetric models, I used the method you proposed and worked.

---

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [October 27, 2023, 12:55pm UTC](https://community.freefem.org/t/confused-about-correct-method/2758/5 "2023-10-27T12:55:34Z")

</div>

I am not sure about the answer. I think if the radius is in the variational form ,then you cannot multiply with R outside the varf, but I do not know about the precise problem you are solving.

I found that in the Stabfem repository, you can find the FreeFem implementation of various flow stability equations, which illustrate a lot of tricks. You might find the answer there; e.g., in [SOURCES\_FREEFEM/Newton\_Axi.edp · master · StabFem / StabFem · GitLab](https://gitlab.com/stabfem/StabFem/-/blob/master/SOURCES_FREEFEM/Newton_Axi.edp?ref_type=heads)
