# How to plot the result of NIntegrate when integrand has parameters?

I am trying to plot f as a function of k (n,k naturals):

f[n_, k_] := NIntegrate[( n)^(2 k)g1[n, k, \[Theta]], {\[Theta], -\[Pi], \[Pi]}]/NIntegrate[g2[n, k, \[Theta]], {\[Theta], -\[Pi], \[Pi]}];

g1[n_, k_, \[Theta]_] := (Sin[(n \[Theta])/2]/Sin[\[Theta]/2])^(
2 k) (2 Sin[\[Theta]/2])^(2 k);

g2[n_, k_, \[Theta]_] := (Sin[(n \[Theta])/2]/Sin[\[Theta]/2])^(2 k);

n=10;
Plot[f[n, k], {k, 1, 5}]
$$$$

• Nintegrate in title Sep 21, 2022 at 22:12

We can use NumericQ and ReIm to plot this function. Also, it is better to divide f into two parts:

f[n_?NumericQ, k_?NumericQ] :=
NIntegrate[(n)^(2 k) g1[n,
k, \[Theta]], {\[Theta], -\[Pi], \[Pi]}];
f1[n_?NumericQ, k_?NumericQ] :=
NIntegrate[g2[n, k, \[Theta]], {\[Theta], -\[Pi], \[Pi]}];
g1[n_, k_, \[Theta]_] := (Sin[(n \[Theta])/2]/
Sin[\[Theta]/2])^(2 k) (2 Sin[\[Theta]/2])^(2 k);

g2[n_, k_, \[Theta]_] := (Sin[(n \[Theta])/2]/Sin[\[Theta]/2])^(2 k);

n = 10;

Plot[Evaluate[ReIm[f[n, k]/f1[n, k]]], {k, 1, 2}, Frame -> True]


• Hey, @Alex Trounev! thank you for your response clue, but why it is appearing two graphs? Sep 21, 2022 at 22:00
• This is complex function, blue is real part, orang is Im part. Sep 22, 2022 at 2:24
\$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global*"]

g1[n_, k_, θ_] := (Sin[(n θ)/2]/
Sin[θ/2])^(2 k) (2 Sin[θ/2])^(2 k);

g2[n_, k_, θ_] := (Sin[(n θ)/2]/Sin[θ/2])^(2 k);


The natural numbers is an ambiguous constraint, it can mean either positive or nonnegative integers. I have defined f for n positive and k nonnegative.

f[n_Integer?Positive, k_Integer?NonNegative] :=
NIntegrate[(n)^(2 k) g1[n, k, θ], {θ, -π, π}]/
NIntegrate[g2[n, k, θ], {θ, -π, π}];

Manipulate[
DiscretePlot[f[n, k], {k, 0, 4},
AxesLabel -> {k, f},
PlotRange -> All],
{{n, 5}, Range[5]}]


• thank you @Bob Hanlon very much for you help! Sep 22, 2022 at 14:08