Skip to main content

fitting - Fit a function to data so that fit is always equal or less than the data


I have a set of measurement data and I try to fit there a function (a Exp[b x] + c Exp[d x] - o). I have two problems:



  • The fit function does not converge nor after 500 iterations.


  • The fit function always has to be equal or less than the data points.


Thank you for your help!


fitdata = {{-0.826333, 9.72503}, {-0.796121, 9.61975}, {-0.765909, 8.25744},
{-0.735697, 7.40448}, {-0.705485, 5.38543}, {-0.675273, 5.15899},
{-0.645061, 4.9356}, {-0.614848, 4.67804}, {-0.584636, 4.28955},
{-0.554424, 4.73144}, {-0.524212, 4.93957}, {-0.494, 4.80621},
{-0.463788, 5.77301}, {-0.433576, 3.39416}, {-0.403364, 1.73309},
{-0.373152, 1.4862}, {-0.342939, 1.4212}, {-0.312727, 1.49505},
{-0.282515, 1.35071}, {-0.252303, 1.28845}, {-0.222091, 1.25183},

{-0.191879, 1.43158}, {-0.161667, 1.39557}, {-0.131454, 1.40259},
{-0.101242, 1.36108}, {-0.0710303, 1.2265}, {-0.0408181, 1.23474},
{-0.0106061, 1.24481}, {0.0196061, 1.26526}, {0.0498182, 1.32446},
{0.0800303, 1.31866}, {0.110242, 1.35345}, {0.140455, 1.45935},
{0.170667, 1.58142}, {0.200879, 1.64947}, {0.231091, 1.72851},
{0.261303, 1.79931}, {0.291515,1.53534}, {0.321727, 1.76849},
{0.351939, 2.56439}, {0.382152, 3.65875}, {0.412364, 1.30584},
{0.442576, 2.4179}, {0.472788, 3.02307}, {0.503, 1.58539},
{0.533212, 1.45324}, {0.563424, 1.4743}, {0.593636, 1.42791},
{0.623849, 1.44165}, {0.654061, 1.56433}, {0.684273, 1.68152},

{0.714485, 2.1933}, {0.744697, 2.30194}, {0.774909, 2.29156},
{0.805121, 2.62207}, {0.835333, 3.09906}, {0.865546, 3.17169},
{0.895758, 4.08508}, {0.92597, 7.48046}, {0.956182, 4.48303},
{0.986394, 4.11621}, {1.01661, 4.18457}, {1.04682, 4.72107},
{1.07703, 5.77667}, {1.10724, 6.35589}, {1.13745, 6.41082},
{1.16767, 7.43164}, {1.19788, 9.28222}};



@ All - The bigger the choice, the harder it is to choose. Thank you for incredible nice and quick answers!



Answer




This answer provides two solutions both use Quantile regression.




  1. The first uses sort of brute force fitting with a family of curves.




  2. The second is similar to the one by Jack LaVigne, but uses QuantileRegressionFit on the second step, which in this case seems to be more straightforward.




This command loads the package QuantileRegression.m used below:



Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/QuantileRegression.m"]

1. QuantileRegressionFit only


Generate a family of functions:


funcs = Table[Exp[k*x], {k, -6, 6, 0.002}];
Length[funcs]

(* 6001 *)

Do Quantile regression fit with the family of functions:



qfunc = First@QuantileRegressionFit[fitdata, funcs, x, {0.01}]

(* 0. + 0.0125232 E^(-5.398 x) + 0.0930724 E^(-5.392 x) +
0.232695 E^(0.2 x) + 0.511794 E^(0.214 x) + 0.039419 E^(4.336 x) *)

and visualize:


Show[
ListPlot[fitdata, PlotStyle -> Black, PlotRange -> All],
Plot[qfunc, {x, -1, 1.5}, PlotStyle -> Red, PlotRange -> All]]


enter image description here


The found fitted function satisfies the condition to be less or equal of the data points, but it is made of too many Exp terms. So the next step is to reduce the number of Exp terms to two as required in the question.


We can visually evaluate the contribution of each of the terms in the found fit:


Plot[Evaluate[(List @@ qfunc)/qfunc], {x, -1, 1.5}, PlotRange -> All, 
PlotLegends -> (List @@ qfunc)]

enter image description here


We pick two of the terms (plus the intercept) and call QuantileRegressionFit again:


qfunc2 = 
First@QuantileRegressionFit[fitdata,

Prepend[(List @@ qfunc)[[{3, -1}]], 1], x, {0}]

(* 0.658637 + 0.105279 E^(-5.392 x) + 0.0414842 E^(4.336 x) *)

Show[ListPlot[fitdata, PlotStyle -> Black, PlotRange -> All],
Plot[qfunc2, {x, -1, 1.5}, PlotStyle -> Red, PlotRange -> All]]

enter image description here


2. NonlinearModelFit model followed by QuantileRegressionFit


First we find a model with the required functions:



nlm = NonlinearModelFit[fitdata, 
a*Exp[b*x] + c*Exp[d*x] + o, {{a, 0.1}, {b, 3}, {c, 0.3}, {d, -4}, {o, 1}}, x]

nlm["BestFitParameters"]

(* {a -> 0.127629, b -> 3.38374, c -> 0.310767, d -> -4.07292,
o -> 1.02299} *)

Show[ListPlot[fitdata, PlotStyle -> Black],
Plot[nlm[x], {x, -1, 1.5}, PlotStyle -> Red]]


enter image description here


Using the model functions:


{Exp[b*x], Exp[d*x]} /. 
Cases[nlm["BestFitParameters"], HoldPattern[(b | d) -> _]]

(* {E^(3.38374 x), E^(-4.07292 x)} *)

find the quantile regression curve that is below the data points (0. quantile):


qfunc = 

First@QuantileRegressionFit[
fitdata, {1, Exp[b*x], Exp[d*x]} /.
Cases[nlm["BestFitParameters"], HoldPattern[(b | d) -> _]],
x, {0.0}]

(* 0. + 0.303621 E^(-4.07292 x) + 0.13403 E^(3.38374 x) *)

Plot the result:


Show[
ListPlot[fitdata, PlotStyle -> Black, PlotRange -> All],

Plot[qfunc, {x, -1, 1.5}, PlotStyle -> Red, PlotRange -> All]]

enter image description here


Comments

Popular posts from this blog

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

What is and isn't a valid variable specification for Manipulate?

I have an expression whose terms have arguments (representing subscripts), like this: myExpr = A[0] + V[1,T] I would like to put it inside a Manipulate to see its value as I move around the parameters. (The goal is eventually to plot it wrt one of the variables inside.) However, Mathematica complains when I set V[1,T] as a manipulated variable: Manipulate[Evaluate[myExpr], {A[0], 0, 1}, {V[1, T], 0, 1}] (*Manipulate::vsform: Manipulate argument {V[1,T],0,1} does not have the correct form for a variable specification. >> *) As a workaround, if I get rid of the symbol T inside the argument, it works fine: Manipulate[ Evaluate[myExpr /. T -> 15], {A[0], 0, 1}, {V[1, 15], 0, 1}] Why this behavior? Can anyone point me to the documentation that says what counts as a valid variable? And is there a way to get Manpiulate to accept an expression with a symbolic argument as a variable? Investigations I've done so far: I tried using variableQ from this answer , but it says V[1...