Piecewise Function for Integration | PF Code Troubleshooting

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
member 428835
Hi PF!

I have a function ##\phi ns## defined below, and ##\phi ns## is continuous everywhere except ##s=0##, where it is singular. However, ##\lim_{s\to 0}\phi ns(s) = 0##, and since I'm integrating in this domain I thought I would define a new function ##\phi nsP## such that ##\phi nsP(s=0)=0## and ##\phi nsP(s) = \phi n(s)## everywhere else. When integrating I get several error messages. Would you mind copy-pasting this code into a notebook and seeing what you think the issue is? I'm happy to talk about the error messages specifically but thought just posting the code verbatim would be easier.
Code:
\[Alpha] = \[Pi]/2;
\[Phi]s[s_] = Table[LegendreP[j, 1, Cos[s]], {j, 1, 15, 2}];
dP[s_, j_] = D[LegendreP[j, 1, s], s];
\[Phi]ns[s_] =
  Table[j (-Sin[s]^2 + Cos[s]^2) LegendreP[j, 1, Cos[s]] +
    2 Sin[s]^2 Cos[s] dP[Cos[s], j], {j, 1, 15, 2}];

\[Phi]nsP[s_] =
  Piecewise[{{\[Phi]ns[s] , s != 0}, {Table[0, {j, 1, 15, 2}],
     s == 0}}];

m = Table[
   NIntegrate[
    Sin[s] \[Phi]nsP[s][[i]]*\[Phi]s[s][[j]], {s, 0, \[Alpha]},
    AccuracyGoal -> 10], {i, 8}, {j, 8}];

I should say when I don't define the piecewise function I do not receive any error messages but later on (not shown above) there are singular issues that I think would not be an issue if this were straightened out here.
 
Physics news on Phys.org
I don't understand why you precalculate your functions as tables. I would wait and create a table only at the end.
Code:
\[Alpha] = \[Pi]/2;

\[Phi]s[s_, j_] := LegendreP[j, 1, Cos[s]]
dP[s_, j_] := D[LegendreP[j, 1, s], s]

\[Phi]ns[s_, j_] := 
 j (-Sin[s]^2 + Cos[s]^2) LegendreP[j, 1, Cos[s]] + 
  2 Sin[s]^2 Cos[s] dP[Cos[s], j]

\[Phi]nsP[s_, j_] := Piecewise[{{0, s == 0}}, \[Phi]ns[s, j]]

m = Table[
   NIntegrate[Sin[s] \[Phi]nsP[s, i]*\[Phi]s[s, j], {s, 0, \[Alpha]}, 
    AccuracyGoal -> 10], {i, 1, 15, 2}, {j, 1, 15, 2}];
It still gives some warnings, but I think it is mostly because of results that are close to zero.

Actually, Mathematica can do all integrals symbolically:
Code:
In[14]:= Table[
Integrate[Sin[s] \[Phi]ns[s, i]*\[Phi]s[s, j], {s, 0, \[Alpha]}], {i,
   1, 15, 2}, {j, 1, 15, 2}]

Out[14]= {{-(2/3), 0, 0, 0, 0, 0, 0, 0}, {16/5, 12/35, 0, 0, 0, 0, 0,
  0}, {0, 80/7, 10/11, 0, 0, 0, 0, 0}, {0, 0, 3360/143, 56/39, 0, 0,
  0, 0}, {0, 0, 0, 672/17, 630/323, 0, 0, 0}, {0, 0, 0, 0, 7920/133,
  396/161, 0, 0}, {0, 0, 0, 0, 0, 48048/575, 2002/675, 0}, {0, 0, 0,
  0, 0, 0, 29120/261, 3120/899}}