# Force FindFormula to avoid Piecewise in the result?

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:

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

• 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?
– JimB
May 6 '19 at 5:05
• @JimB I need a smooth (infinitely differentiable) function. Piecewise results often are not even continuous. May 6 '19 at 18:12
• @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? Oct 15 '21 at 13:03

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

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


With my Mathematica 12.2.0.0, it gives:

{
Automatic,
"NonLinearRegression",
{"SimulatedAnnealing", "WalksNumber" -> m_},
"SimulatedAnnealing",
{"SimulatedAnnealing", initial_},
{"ParallelTempering", "WalksNumber" -> m_},
"ParallelTempering",
{"ParallelTempering", initial_},
"Piecewise"
}


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[
table1,
x
, Method -> "NonLinearRegression"
, SpecificityGoal -> "High"
, TargetFunctions -> {Plus, Times, Power, Sin, Cos, Tan, Cot, Log, Sqrt, Csc, Sec, Abs, Exp}]
Show[
Plot[fit, {x, 0, 1+Length@table1}],
ListPlot@table1
]


• 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. Oct 15 '21 at 3:04
• @AlexTrounev FindFormula acts randomly, and may change by version. It may be helpful to adjust some options and keep trying. Oct 15 '21 at 10:34
• 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. Oct 15 '21 at 12:49
• @AlexTrounev The algorithm uses ramdom numbers. BlockRandom may help. Oct 16 '21 at 5:41
• How BlockRandom` may help if we have different formulas in different versions? Oct 16 '21 at 8:08