Piecewise Function for Integration | PF Code Troubleshooting

Click For Summary
SUMMARY

The discussion focuses on the implementation of a piecewise function for integration in Mathematica, specifically addressing the function ##\phi ns##, which is continuous except at ##s=0##. The user attempts to define a new function ##\phi nsP## that resolves the singularity at ##s=0## by setting ##\phi nsP(s=0)=0##. Despite this, the user encounters error messages during integration with the NIntegrate function. The conversation highlights the importance of defining functions correctly to avoid singular issues and suggests that Mathematica can perform symbolic integration to bypass these problems.

PREREQUISITES
  • Understanding of piecewise functions in Mathematica
  • Familiarity with Legendre polynomials and their derivatives
  • Knowledge of numerical integration techniques in Mathematica, specifically NIntegrate
  • Basic proficiency in symbolic integration using Mathematica
NEXT STEPS
  • Explore the use of Piecewise functions in Mathematica for handling singularities
  • Learn about the properties and applications of Legendre polynomials in mathematical modeling
  • Investigate the differences between NIntegrate and Integrate in Mathematica for various scenarios
  • Study error handling and debugging techniques in Mathematica to resolve integration issues
USEFUL FOR

Mathematicians, physicists, and engineers working with numerical methods and symbolic computation in Mathematica, particularly those dealing with integration of piecewise functions and singularities.

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 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
7K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K