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$
  • 1
    $\begingroup$ You might consider using FunctionInterpolation[] for the purpose. $\endgroup$ – J. M. is in limbo Jun 8 '15 at 14:44
  • 1
    $\begingroup$ ListPlot[ NIntegrate[f[x,#],{x,a,b}]&/@Range[10,100] ] $\endgroup$ – N.J.Evans Jun 8 '15 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$ – Your Majesty Jun 8 '15 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 '15 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$ – Your Majesty Jun 8 '15 at 18:11
6
$\begingroup$

Define:

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

Then plot it:

Plot[integ[p],{p,10,100}]
$\endgroup$
  • $\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$ – Your Majesty Jun 8 '15 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$ – J. M. is in limbo Jun 8 '15 at 16:55
  • $\begingroup$ OK I assumed integ[] had something to do with integers. $\endgroup$ – Your Majesty Jun 8 '15 at 16:56
  • $\begingroup$ Ops, just noticed that integ is just a name haha sorry $\endgroup$ – Your Majesty Jun 8 '15 at 16:58
  • 1
    $\begingroup$ @LoveLearning integ is for integral lol $\endgroup$ – Ivan Jun 8 '15 at 21:42

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.