Mathematica Piecewise Function for Integration | PF Code Troubleshooting

AI Thread Summary
The discussion revolves around troubleshooting a piecewise function for integration in Mathematica, specifically the function ##\phi nsP##, which is defined to handle a singularity at ##s=0## by assigning it a value of zero at that point. The user encounters error messages during integration with this piecewise function, while the original function without piecewise definition does not produce errors but leads to singular issues later. There is a suggestion that precalculating functions as tables may not be necessary and could be deferred until the end of the process. Additionally, it is noted that Mathematica can perform the integrals symbolically, which may provide a more straightforward solution. The conversation highlights the complexities of handling singularities in integration and the potential benefits of symbolic computation.
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}}
 

Similar threads

Replies
7
Views
2K
Replies
2
Views
2K
Replies
6
Views
7K
Replies
2
Views
5K
Replies
2
Views
1K
Replies
1
Views
2K
Replies
4
Views
3K
Replies
4
Views
1K
Replies
13
Views
2K
Back
Top