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.
1 Answer
$\begingroup$
$\endgroup$
8
Define:
integ[p_?NumericQ] := NIntegrate[f[x, p], {x, a, b}]
Then plot it:
Plot[integ[p],{p,10,100}]
-
$\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 ofinteg[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
FunctionInterpolation[]
for the purpose. $\endgroup$ListPlot[ NIntegrate[f[x,#],{x,a,b}]&/@Range[10,100] ]
$\endgroup$f[{x,p}]:= Sin[p*x]
(say) and then writeListPlot[ NIntegrate[f[x,#],{x,a,b}]&/@Range[10,100] ]
? $\endgroup$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$#
means with respect to the second variable because it appears in the second position in the argument off
? 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$