3
$\begingroup$

Suppose we have an integral of the form $$I = \int_a^b dx \, \sin(px), \tag{1}$$ where I've take $f(x;p) = \sin (px)$ to be specific. I want to plot this integral for a range of values of the parameter $10<p<100$ say. How can one do that? Please notice that the function $f$ is in reality kind of complicated and the integral must be done using numerical integration.

$\endgroup$
6
  • 1
    $\begingroup$ You might consider using FunctionInterpolation[] for the purpose. $\endgroup$ Jun 8, 2015 at 14:44
  • 1
    $\begingroup$ ListPlot[ NIntegrate[f[x,#],{x,a,b}]&/@Range[10,100] ] $\endgroup$
    – N.J.Evans
    Jun 8, 2015 at 16:17
  • $\begingroup$ @N.J.Evans So you first define f[{x,p}]:= Sin[p*x] (say) and then write ListPlot[ NIntegrate[f[x,#],{x,a,b}]&/@Range[10,100] ]? $\endgroup$ Jun 8, 2015 at 16:35
  • $\begingroup$ Yes, but you need to make sure the brackets are the same when you define f[x_,p_] and when you use it. Either one works, but your definition would make me think that x and p typically come together in a list. $\endgroup$
    – N.J.Evans
    Jun 8, 2015 at 16:59
  • $\begingroup$ @N.J.Evans OK so the number icon # means with respect to the second variable because it appears in the second position in the argument of f? Now I've managed to plot my function actually but I want to be sure what it is I've plotted, i.e. if it is wrt to the "second-position" variable (I mean there are no other variables around so it must be but anyway). $\endgroup$ Jun 8, 2015 at 18:11

1 Answer 1

6
$\begingroup$

Define:

integ[p_?NumericQ] := NIntegrate[f[x, p], {x, a, b}]

Then plot it:

Plot[integ[p],{p,10,100}]
$\endgroup$
8
  • $\begingroup$ Hey @Ivan, what if $p$ is a not a integer? Can we do the same but with smaller step-sizes, say 0.01? $\endgroup$ Jun 8, 2015 at 16:24
  • $\begingroup$ @Love, nothing Ivan did here assumes that p is an integer. He did forget the safety mechanism of integ[p_?NumericQ] := (* stuff*) tho. $\endgroup$ Jun 8, 2015 at 16:55
  • $\begingroup$ OK I assumed integ[] had something to do with integers. $\endgroup$ Jun 8, 2015 at 16:56
  • $\begingroup$ Ops, just noticed that integ is just a name haha sorry $\endgroup$ Jun 8, 2015 at 16:58
  • 1
    $\begingroup$ @LoveLearning integ is for integral lol $\endgroup$
    – Ivan
    Jun 8, 2015 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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