Is it possible to provide any arguments to FindFormula to force it to avoid using Piecewise in the result, but instead try to find the best fit using expressions constructed only from functions specified in TargetFunctions option?

To give a concrete example, consider e.g. the data in this question. FindFormula will always return a Piecewise expression with many pieces:

enter image description here

In many (most?) applications, it is reasonable to assume that the function is smooth, and a piecewise result is simply not useful.

  • $\begingroup$ Does adding SpecificityGoal-> "Low" help? The documentation states that this results in simpler fits. My unsolicited advice is to avoid FindFormula completely or accept the fits that it provides. Any calculations with a piecewise fit will still be performed by a computer and not by hand so what exactly is the downside to having a piecewise result? $\endgroup$
    – JimB
    May 6, 2019 at 5:05
  • 1
    $\begingroup$ @JimB I need a smooth (infinitely differentiable) function. Piecewise results often are not even continuous. $\endgroup$ May 6, 2019 at 18:12
  • $\begingroup$ @VladimirReshetnikov This problem has no unique solution and also depends on version. For instance in v. 12.2 code fit = FindFormula[table1, x, Method -> "NonLinearRegression", SpecificityGoal -> "High", TargetFunctions -> {Plus, Times, Power, Sin, Cos, Tan, Cot, Log, Sqrt, Csc, Abs, Exp}] has outcome 0.317828 - 2. E^(-.901747 x) + 0.511021 x while in v.12.3 it gives 0.317828 - 2.2358 E^(-1. x) Sqrt[x] + 0.511021 x. How it can solve your problem? $\endgroup$ Oct 15, 2021 at 13:03

1 Answer 1


FindFormula has an undocumented option Method, its possible values can be printed by the following code:

sym = First@Names@"*`iFindFormula"
Keys[#][[1,2]] & /@ Rest@DownValues@#&@Symbol@sym

With my Mathematica, it gives:

  {"SimulatedAnnealing", "WalksNumber" -> m_},
  {"SimulatedAnnealing", initial_},
  {"ParallelTempering", "WalksNumber" -> m_},
  {"ParallelTempering", initial_},

Do not use Automatic and "Piecewise" (although this one won't pass the argument check of FindFormula), then you can get rid of the Piesewise results.

With proper option values of SpecificityGoal and TargetFunctions, better results can be obtained:

fit = FindFormula[
, Method -> "NonLinearRegression"
, SpecificityGoal -> "High"
, TargetFunctions -> {Plus, Times, Power, Sin, Cos, Tan, Cot, Log, Sqrt, Csc, Sec, Abs, Exp}]
  Plot[fit, {x, 0, 1+Length@table1}],


  • $\begingroup$ It is nice code (+1), but with version 12.3 it gives 0.317828 + 0.511021 x - 2.2358 E^(-0.866132 (0.107106 + x)) Sec[19. x], and it looks not so good as in your plot. $\endgroup$ Oct 15, 2021 at 3:04
  • 2
    $\begingroup$ @AlexTrounev FindFormula acts randomly, and may change by version. It may be helpful to adjust some options and keep trying. $\endgroup$
    – rnotlnglgq
    Oct 15, 2021 at 10:34
  • $\begingroup$ If we remove Sec from options, then we have in v.12.3 0.317828 - 2.2358 E^(-1. x) Sqrt[x] + 0.511021 x. It is differ from above as well. Also I don't understand how it could solve problem of differentiation. $\endgroup$ Oct 15, 2021 at 12:49
  • 1
    $\begingroup$ @AlexTrounev The algorithm uses ramdom numbers. BlockRandom may help. $\endgroup$
    – rnotlnglgq
    Oct 16, 2021 at 5:41
  • $\begingroup$ How BlockRandom may help if we have different formulas in different versions? $\endgroup$ Oct 16, 2021 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.