0
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ ...and you've looked at FindFit[]? $\endgroup$ – J. M. will be back soon Oct 12 '15 at 17:13
  • 3
    $\begingroup$ I think that minimum due diligence does include searching the documentation for the keywords of your question before posting. $\endgroup$ – rhermans Oct 12 '15 at 17:16
  • 2
    $\begingroup$ ...or searching this site using those tags. $\endgroup$ – march Oct 12 '15 at 17:17
  • 2
    $\begingroup$ 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. $\endgroup$ – JimB Oct 12 '15 at 17:32
  • 2
    $\begingroup$ 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. $\endgroup$ – JimB Oct 12 '15 at 18:06
3
$\begingroup$

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}
  ]
 ]

Mathematica graphics

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]}

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

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  *)
$\endgroup$
  • $\begingroup$ I upvoted partly because of your emphasis on the word "experimental". :) $\endgroup$ – J. M. will be back soon Oct 13 '15 at 13:24

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.