# Minimize cost function with BFGS algorithm FreeFem?

**URL:** <https://community.freefem.org/t/minimize-cost-function-with-bfgs-algorithm-freefem/843>\
**Category:** General Discussion\
**Created:** [March 9, 2021, 10:04am UTC](https://community.freefem.org/t/minimize-cost-function-with-bfgs-algorithm-freefem/843 "2021-03-09T10:04:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lemin](https://avatars.discourse-cdn.com/v4/letter/l/c6cbf5/32.png) [@Lemin](https://community.freefem.org/u/Lemin)\
**Post date:** [March 9, 2021, 10:04am UTC](https://community.freefem.org/t/minimize-cost-function-with-bfgs-algorithm-freefem/843/1 "2021-03-09T10:04:26Z")

</div>

Hello,

I have a cost function and its gradient calculated with finite element discretization (values at integrations points) and I have the data in a text file.  
How to minimize this function? any idea?  
Thanks for your help.

Best regards,

---

<div class="post-metadata">

**Author:** ![LucasPa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/lucaspa/32/347_2.png) [@LucasPa](https://community.freefem.org/u/LucasPa)\
**Post date:** [March 9, 2021, 2:02pm UTC](https://community.freefem.org/t/minimize-cost-function-with-bfgs-algorithm-freefem/843/2 "2021-03-09T14:02:16Z")

</div>

Hi,

I never tried that but I think that this page [Algorithms & Optimization](https://doc.freefem.org/documentation/algorithms-and-optimization.html) could interest you.  
Moreover, I saw the example plugin/IpoptMinSurf.edp, that may help you.

Best,

Lucas

---

<div class="post-metadata">

**Author:** ![Lemin](https://avatars.discourse-cdn.com/v4/letter/l/c6cbf5/32.png) [@Lemin](https://community.freefem.org/u/Lemin)\
**Post date:** [March 9, 2021, 2:08pm UTC](https://community.freefem.org/t/minimize-cost-function-with-bfgs-algorithm-freefem/843/3 "2021-03-09T14:08:51Z")

</div>

> [@LucasPa](#):
>
> plugin/IpoptMinSurf.edp

Thank you so much Lucas for your response.  
I have examined the link above but The problem is my cost function and its gradient not mathematically explicit, calculated numerically at some points xi in volume V (integration point) at each increment of time t using another FE solver. The results for the function and its gradient are stored in an array format. My question is how to handle the cost function and its gradient to give them as input for the BFGS algorithm.

Thanks for your help

---

<div class="post-metadata">

**Author:** ![LucasPa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/lucaspa/32/347_2.png) [@LucasPa](https://community.freefem.org/u/LucasPa)\
**Post date:** [March 9, 2021, 2:21pm UTC](https://community.freefem.org/t/minimize-cost-function-with-bfgs-algorithm-freefem/843/4 "2021-03-09T14:21:42Z")

</div>

Excuse me, I did not understood at first your problem. But actually there is still something I do not understand, if the cost function and the gradient are calculated by an other FE solver and stored in a file, why do you want to perform the optimization in FreeFem?

---

<div class="post-metadata">

**Author:** ![Lemin](https://avatars.discourse-cdn.com/v4/letter/l/c6cbf5/32.png) [@Lemin](https://community.freefem.org/u/Lemin)\
**Post date:** [March 9, 2021, 2:36pm UTC](https://community.freefem.org/t/minimize-cost-function-with-bfgs-algorithm-freefem/843/5 "2021-03-09T14:36:59Z")

</div>

> [@LucasPa](#):
>
> plugin/IpoptMinSurf.edp

Thanks,

The choice of freefem solver because It is finite element solver and also optimization solver and I think it’s easy for freefem to handle the data generated by FEA solver (discretization) and minimizing the cost function giving the cost, its gradient, and initial guess.

Thanks for your help
