0
$\begingroup$

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}]
$\endgroup$
  • $\begingroup$ 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]] $\endgroup$ – J. M. will be back soon Mar 17 '19 at 16:18
  • $\begingroup$ So you suspect localizing chebyinnerprod will fix the solution, if I understand you? I'm going to try it right now. $\endgroup$ – Shinaolord Mar 17 '19 at 16:19
  • $\begingroup$ That does fix it. I'm surprised that worked, to be honest. Thanks ! (: $\endgroup$ – Shinaolord Mar 17 '19 at 16:20
  • $\begingroup$ You might also be interested in this thread. $\endgroup$ – J. M. will be back soon Mar 17 '19 at 16:20
  • $\begingroup$ Mostly, your problem was in using x both in Integrate[] and Plot[]. $\endgroup$ – J. M. will be back soon Mar 17 '19 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.