My Mathematica Plot isn't smooth

I am trying to plot the function $$\phi(x)=\sum_{n=1}^{250} \psi(x)\psi(a/3)^*$$, where $$\psi(x)=\sqrt(2/a)\sin(n \pi x/a)$$. For some reason my plot is showing up choppy and is missing a lot of points. I have tried setting PlotPoints to 1 million, but it still isn't smooth, this is my code:

ClearAll
a = 1;
ψ[x_, n_] = Sqrt[2/a] Sin[n Pi x/a];
ϕ[x_, n_] =
Sum[ψ[x, i]*Conjugate[ψ[a/3, i]], {i, 0, n}];
Plot[{ϕ[x, 250]}, {x, 0, 1}, PlotRange -> Full]

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! Feb 9 at 21:21

the problem vanishes if you define the functions with := like

ClearAll
a = 1;
\[Psi][x_, n_] := Sqrt[2/a] Sin[n Pi x/a];
\[Phi][x_, n_] :=
Sum[\[Psi][x, i]*Conjugate[\[Psi][a/3, i]], {i, 0, n}];
Plot[{\[Phi][x, 250]}, {x, 0, 1}, PlotRange -> Full]

• Thank you! I am not sure why I didn't try this before. Feb 9 at 20:19
• Also note, using an evaluation of the summation before plotting Plot[{Evaluate[\[Phi][x, 250]]},... increases the plot-speed significantly. Feb 9 at 22:43
ClearAll["Global*"]

a = 1;

ψ[x_, n_] = Sqrt[2/a] Sin[n Pi x/a];


Use ComplexExpand to simplify

ϕ[x_, n_] =
Sum[ψ[x, i]*Conjugate[ψ[a/3, i]], {i, 0, n}] // ComplexExpand //
FullSimplify

(* (-((Sqrt[3] Cos[(n π)/3] + Sin[(n π)/3]) Sin[n π x]) +
2 Sin[(n π)/3] Sin[(1 + n) π x])/(-1 + 2 Cos[π x]) *)

Plot[ϕ[x, 250], {x, 0, 1}, PlotRange -> Full]


EDIT: More generally, for real a

Clear[a]

ϕ2[x_, n_, a_ : 1] =
Sum[ComplexExpand[ψ[x, i]*Conjugate[ψ[a/3, i]]],
{i, 0, n}] // FullSimplify

(* (Cos[1/3 π (a + a n - 3 n x)] - Cos[1/3 π (a + a n + 3 n x)] -
Cos[1/3 π (a n - 3 (1 + n) x)] +
Cos[(a n π)/3 + (1 + n) π x])/(2 (Cos[(a π)/3] - Cos[π x])) *)


For a == 1 this reduces to the previous result

ϕ2[x, n] == ϕ[x, n] // Simplify

(* True *)


The problem seems to be from round-off error in the imaginary parts in the symbolic formula returned by Sum[] when evaluated at floating-point x. An alternative is to use arbitrary-precision numbers:

Plot[{ϕ[x, 250]}, {x, 0, 1},
PlotRange -> Full, WorkingPrecision -> 16]


Another way to simplify the sum:

Assuming[{n, i} ∈ Integers && n >= i >= 0 && 0 < x < 1,
ϕ[x_, n_] =
Sum[ψ[x, i]*Conjugate[ψ[a/3, i]] // Simplify, {i, 0, n}] //
ExpToTrig // FullSimplify
]
(*
1/2 (-Sec[1/6 π (2 + 3 x)] Sin[1/6 (1 + 2 n) π (-1 + 3 x)] -
Csc[1/6 (π + 3 π x)] Sin[1/6 (1 + 2 n) π (1 + 3 x)])
*)


Other way:

For your graphics labels, use:

<< MaTeX


Definition of the $$\phi(x,n)$$ function:

a = 1;
p[f_] := Plot[f, {x, 0, 1}, PlotRange -> {{-0.08, 1.08}, {-60, 280}}, PlotStyle -> {Blue, Thickness[0.0025]}, AxesStyle -> {{Black, Arrowheads[{0, 0.03}]}, {Black, Arrowheads[{0, 0.03}]}}, LabelStyle -> Directive[Black, Bold, Tiny], AxesLabel -> {MaTeX["x", Magnification -> 1], MaTeX["\\phi\\left(x,250\\right)", Magnification -> 1]}, AspectRatio -> 0.85]
ψ[x_, n_] := Sqrt[2/a] Sin[n Pi x/a];
φ[x_, n_] := FullSimplify[Refine[ComplexExpand[Sum[ψ[x, i]*Conjugate[ψ[a/3, i]], {i, 0, n}]], Assumptions -> {x > 0, n > 0}]]


The function $$\phi(x,n)$$ is given by $$\phi(x,n) = \displaystyle\frac{2 \sin \left(\frac{\pi n}{3}\right) \sin (\pi (n+1) x)-\sin (\pi n x) \left(\sin \left(\frac{\pi n}{3}\right)+\sqrt{3} \cos \left(\frac{\pi n}{3}\right)\right)}{2 \cos (\pi x)-1}$$

φ[x, n]
(* (-(Sqrt[3] Cos[(n π)/3] + Sin[(n π)/3]) Sin[n π x] +
2 Sin[(n π)/3] Sin[(1 + n) π x])/(-1 + 2 Cos[π x]) *)


We take $$n = 250$$:

φ2[x_] := (-(Sqrt[3] Cos[(n π)/3] + Sin[(n π)/3]) Sin[n π x] + 2 Sin[(n π)/3] Sin[(1 + n) π x])/(-1 + 2 Cos[π x]) /. n -> 250
φ2[x]
(*(Sqrt[3] Sin[250 π x] - Sqrt[3] Sin[251 π x])/(-1 + 2 Cos[π x])*)


Finally, we plot the function $$\phi(x,250)$$:

p[φ2[x]]