# Why is an integration error appearing during this code?

I'm really confused about this to be honest. If i evaluate approx[x,k] for some k, and then input the list into the Plot function, I get a plot. But if I try to use approx[x,k] directly, I get an Integration error, even though, outside of Plot, this error doesn't happen. I feel like I'm fundamentally missing something. NIntegrate fixes this, which I can use, but I'm worried that for more complex Inner Product evaluations that NIntegrate may not suffice. Any ideas on the error?

**** DOES NOT WORK WITH N[] OR //N ****

Clear[chebyinnerprod, approx]
chebyinnerprod[k_] := chebyinnerprod[k]=
Integrate[(Sin[\[Pi] x] ChebyshevT[k, x])/Sqrt[1 - x^2], {x, -1, 1}]/
Which[k == 0, \[Pi], k > 0, \[Pi]/2]

approx[x_, k_] := approx[x,k]=Sum[chebyinnerprod[t]*ChebyshevT[t, x], {t, 0, k}]

Plot[approx[x, 2], {x, -1, 1}]


**** WORKS ****

Clear[chebyinnerprod, approx]

chebyinnerprod[k_] := chebyinnerprod[k]=
NIntegrate[(Sin[\[Pi] x] ChebyshevT[k, x])/Sqrt[1 - x^2], {x, -1, 1}]/
Which[k == 0, \[Pi], k > 0, \[Pi]/2]

approx[x_, k_] :=approx[x,k]= Sum[chebyinnerprod[t]*ChebyshevT[t, x], {t, 0, k}]

Plot[approx[x, 2], {x, -1, 1}]

• Try this: chebyinnerprod[k_Integer?NonNegative] := chebyinnerprod[k] = Block[{x}, Integrate[(Sin[π x] ChebyshevT[k, x])/Sqrt[1 - x^2], {x, -1, 1}]/Which[k == 0, π, k > 0, π/2]] – J. M. will be back soon Mar 17 '19 at 16:18
• So you suspect localizing chebyinnerprod will fix the solution, if I understand you? I'm going to try it right now. – Shinaolord Mar 17 '19 at 16:19
• That does fix it. I'm surprised that worked, to be honest. Thanks ! (: – Shinaolord Mar 17 '19 at 16:20
• You might also be interested in this thread. – J. M. will be back soon Mar 17 '19 at 16:20
• Mostly, your problem was in using x both in Integrate[] and Plot[]. – J. M. will be back soon Mar 17 '19 at 16:22