# Fminsearch function evaluations has been exceeded

**URL:** <https://scilab.discourse.group/t/fminsearch-function-evaluations-has-been-exceeded/224>\
**Category:** Optimization\
**Created:** [November 6, 2023, 12:07pm UTC](https://scilab.discourse.group/t/fminsearch-function-evaluations-has-been-exceeded/224 "2023-11-06T12:07:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Smart](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@Smart](https://scilab.discourse.group/u/Smart)\
**Post date:** [November 6, 2023, 12:07pm UTC](https://scilab.discourse.group/t/fminsearch-function-evaluations-has-been-exceeded/224/1 "2023-11-06T12:07:36Z")

</div>

Hello,

I attempted to fit an exponential curve to my data in my initial attempt. It seems that I may need to delve deeper into the process of fitting and optimization in general.

On occasion, I encounter the following error. How can I resolve this issue?

> fminsearch: Exiting due to exceeding the maximum number of function evaluations - consider increasing the MaxFunEvals option.  
> Current function value: 705.45696

You can access my test data through the following link: [data135.txt](https://scilab.discourse.group/uploads/short-url/AaAHnStB4uiaVkkOpCSeHufj3Kz.txt) (7.4 KB)

Here is the minimal example code I have used:

```auto
function q =pt2expfit(p,xd,yd)
    // fitting function 
    y = p(1) * (1 - exp(-xd / p(2))).^p(3);
    // leastsq function
    q = sum((y(:,1)-yd(:,1)).^2);
endfunction

function MainScript()
    // load measurement data
    data = fscanfMat('data135.txt');
    xd = data(:,1);
    yd = data(:,2)-data(1,2); // move y-values to start at 0
    init = [50,1,3];
        
    // fit data, init: start values for fminsearch fit function
    // find paramters for fitting function
     [p,fval,exitflag,output] = fminsearch(pt2expfit,init)
     y_fit = p(1) * (1 - exp(-xd / p(2))).^p(3);
     disp(output)   
    // compare fitting function with real data

     plot(xd,[yd,y_fit])
endfunction

MainScript()

```

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [November 6, 2023, 1:24pm UTC](https://scilab.discourse.group/t/fminsearch-function-evaluations-has-been-exceeded/224/2 "2023-11-06T13:24:08Z")

</div>

Hello,

After some modifications for Scilab 2024.0 (change `^` to `.^` where applicable, otherwise the script was not running), I got the result in 81 iterations and 148 function calls.

 ![Screenshot 2023-11-06 at 14.23.06](https://global.discourse-cdn.com/free1/uploads/utc/original/1X/6b63bdb02d2cc3f730269798eac113f044b579c4.png)

```scilab
--> MainScript()

  algorithm = "Nelder-Mead simplex direct search"
  funcCount = 148
  iterations = 81
  message: [3x1 string]

```

I tested under Windows and macOS.

S.

---

<div class="post-metadata">

**Author:** ![Smart](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@Smart](https://scilab.discourse.group/u/Smart)\
**Post date:** [November 7, 2023, 8:37am UTC](https://scilab.discourse.group/t/fminsearch-function-evaluations-has-been-exceeded/224/3 "2023-11-07T08:37:58Z")

</div>

Thank you, for this fast reply! Wow, a new Version is available again. I’ll test the fitting function.

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [November 7, 2023, 8:58am UTC](https://scilab.discourse.group/t/fminsearch-function-evaluations-has-been-exceeded/224/4 "2023-11-07T08:58:52Z")

</div>

However, I don’t understand how you have obtained the reported results. Which version of Scilab were you using ?

---

<div class="post-metadata">

**Author:** ![Smart](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@Smart](https://scilab.discourse.group/u/Smart)\
**Post date:** [November 7, 2023, 11:35am UTC](https://scilab.discourse.group/t/fminsearch-function-evaluations-has-been-exceeded/224/5 "2023-11-07T11:35:45Z")

</div>

I used version 2023. The detailed “output” information provided by fminsearch has been quite helpful. I’m definitely looking forward to finding the time to explore Scilab’s optimization solutions more thoroughly.

I have some colleagues in the lab who lack a deep understanding of mathematics and have no programming experience. They primarily work with LabVIEW and Excel. Recently, we had a discussion about whether these guys should consider transitioning to Scilab or Python for their data analysis needs. Personally, I prefer using Scilab, especially because I rely on the Xcos application.

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [November 7, 2023, 12:17pm UTC](https://scilab.discourse.group/t/fminsearch-function-evaluations-has-been-exceeded/224/6 "2023-11-07T12:17:07Z")

</div>

With Scilab, you have everything in one place and popular toolboxes are maintained. For differentiable optimization I particularly recommend the following toolboxes:

> **[Scilab Module : Sci-IPOpt](https://atoms.scilab.org/toolboxes/sci_ipopt)**
>
> A Non-linear Interior Point Optimization Solver

> **[Scilab Module : Fmincon](https://atoms.scilab.org/toolboxes/fmincon)**
>
> Nonlinearily constrained multivariable optimization solver

S.

---

<div class="post-metadata">

**Author:** ![Smart](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@Smart](https://scilab.discourse.group/u/Smart)\
**Post date:** [November 7, 2023, 12:59pm UTC](https://scilab.discourse.group/t/fminsearch-function-evaluations-has-been-exceeded/224/7 "2023-11-07T12:59:37Z")

</div>

I’m concerned about the transition into the saturated region in the curve we interpolated above. Do you have expertise in whether it’s possible to give this region a bit more emphasis or weighting?

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [November 7, 2023, 1:21pm UTC](https://scilab.discourse.group/t/fminsearch-function-evaluations-has-been-exceeded/224/8 "2023-11-07T13:21:40Z")

</div>

Yes, just add a weight when you compute the residual, e.g.

```auto
w = 1+9*(xd>=2 & xd <=3)
q = sum(w.*(y(:,1)-yd(:,1)).^2);

```

---

<div class="post-metadata">

**Author:** ![Smart](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@Smart](https://scilab.discourse.group/u/Smart)\
**Post date:** [November 14, 2023, 1:52pm UTC](https://scilab.discourse.group/t/fminsearch-function-evaluations-has-been-exceeded/224/9 "2023-11-14T13:52:17Z")

</div>

This is great!! It helped a lot!

---

<div class="post-metadata">

**Author:** ![Smart](https://avatars.discourse-cdn.com/v4/letter/s/8baadc/32.png) [@Smart](https://scilab.discourse.group/u/Smart)\
**Post date:** [November 15, 2023, 1:29pm UTC](https://scilab.discourse.group/t/fminsearch-function-evaluations-has-been-exceeded/224/10 "2023-11-15T13:29:35Z")

</div>

I am genuinely pleased with the current results, and I hope my inquiry isn’t considered impolite. However, I am encountering the following output message:

> fminsearch: Exiting: The maximum number of function evaluations has been exceeded
> 
> - Please consider increasing the MaxFunEvals option.  
> Current function value: 414.64762

Unfortunately, I haven’t had the opportunity to delve further into the intricacies of fminsearch. Perhaps you could provide a brief solution without inconveniencing you too much? How can I prevent this output from occurring?

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [November 15, 2023, 1:57pm UTC](https://scilab.discourse.group/t/fminsearch-function-evaluations-has-been-exceeded/224/11 "2023-11-15T13:57:17Z")

</div>

Can you edit your post and drag/drop the scilab file (with .sci or .sce extension) ?
