# Fit a polynomial/curve to an NIntegrate result

I want to NIntegrate a function which also contains some parameter. In the end, I would like to plot the result as a function of one of these parameters. The thing is I would like to scatter plot the result and then fit a curve through them. I already know how to plot a line but then I don't know how to fit a polynomial to the result.

More specifically, suppose we have a function

f[p_]:= NIntegrate[p*Exp[-x],{x,0,Infinity}];


Now I could plot this the usual way for a range, say p=0..1 of the parameter p via Plot[f[p],{p,0,1}] But in this way I would get a curve without any info on its (approximate) analytical shape. What I want is to know the approximate analytical shape of the plot, i.e. if it's linear or quadratic and so on. So I was thinking of scatter-plotting (like in Excel) and then fitting a curve to read of the approximate shape of the curve.

Is this possible in a simple way?

• ...and you've looked at FindFit[]? – J. M. will be back soon Oct 12 '15 at 17:13
• I think that minimum due diligence does include searching the documentation for the keywords of your question before posting. – rhermans Oct 12 '15 at 17:16
• ...or searching this site using those tags. – march Oct 12 '15 at 17:17
• I'm more old-fashioned and would suggest doing the "scatter plot" first (even though the only "errors" are about machine precision and lack-of-fit rather than the usual random and independent errors) and then thinking about what kind of curve might be appropriate. After that FindFit with a candidate functional form would be appropriate. If you want no thinking at all, then the experimental FindFormula might be helpful. – JimB Oct 12 '15 at 17:32
• Sorry. I assumed from your username that a bit of sarcasm would be tolerated. Maybe this might be a topic for Mathematica Meta in that "how" to do things don't always come with additional advice about potential consequences. I would think that besides the great grammatical help given in this forum the wide range of experience available from using such techniques is an even better service. – JimB Oct 12 '15 at 18:06

Creating the table with the data

data = Table[
{
p,
NIntegrate[p*Exp[-x], {x, 0, Infinity}]
}
, {p, 0, 1, 1/10}
]

(* {{0, 0.}, {1/10, 0.1}, {1/5, 0.2}, {3/10, 0.3}, {2/5,
0.4}, {1/2, 0.5}, {3/5, 0.6}, {7/10, 0.7}, {4/5, 0.8}, {9/10,
0.9}, {1, 1.}} *)


Using Fit

Fit[
data
, {1, x, x^2}
, x]
(* -4.64784*10^-16 + 1. x - 5.58029*10^-16 x^2 *)


Plot the points and the Fit

Show[
ListPlot[data]
, Plot[
Evaluate@Fit[
data
, {1, x, x^2}
, x], {x, 0, 1}
]
]


Using FindFit

ClearAll[c,x]
FindFit[
data
, FromDigits[Reverse@Array[c, 4], x]
, Array[c, 4]
, x]
(* {c[1] -> -2.04192*10^-16, c[2] -> 1.,
c[3] -> 2.79015*10^-15, c[4] -> -2.05223*10^-15} *)


Notice that FromDigits[Reverse@Array[c, 4], x] creates the Polynomial degree 4

FromDigits[Reverse@Array[c, 4], x]
(* c[1] + x c[2] + x^2 (c[3] + x c[4]) *)


and Array[c, 4] the list of coefficients {c[1], c[2], c[3], c[4]}

• This makes sense, thanks I'll check it out. – Your Majesty Oct 12 '15 at 17:22
• How did you obtain this info: (* -4.64784*10^-16 + 1. x - 5.58029*10^-16 x^2 *) ? – Your Majesty Oct 12 '15 at 17:30
• The commented code (* *) is the output of the function before it. – rhermans Oct 12 '15 at 17:31
• Thanks this was awesome. Step by step and clear, perfect. – Your Majesty Oct 12 '15 at 17:42

With version 10.2 you can use the EXPERIMENTAL function FindFormula

Clear[f]


Note that the definition of a function defined using a numerical technique (e.g., NIntegrate) should be restricted to numeric arguments.

f[p_?NumericQ] :=
NIntegrate[p*Exp[-x], {x, 0, Infinity}]

data = Table[{p, f[p]}, {p, .1, 1, .1}];

FindFormula[data, p]

(*  1. p  *)


Using exact numbers

dataR = data // Rationalize[#, 10^-10] &;

FindFormula[dataR, p]

(*  p  *)

• I upvoted partly because of your emphasis on the word "experimental". :) – J. M. will be back soon Oct 13 '15 at 13:24