Piecewise Function for Integration | PF Code Troubleshooting

In summary, the conversation discusses a function with a singularity at s=0 and the creation of a new function to handle this singularity. The issue of error messages when integrating the function is also mentioned, and a possible solution is provided by creating a table at the end and using symbolic integration.
  • #1
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
  • #2
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}}
 

1. What is a piecewise function?

A piecewise function is a mathematical function that is defined by different equations or expressions over different intervals or "pieces" of the domain.

2. How is a piecewise function used for integration?

A piecewise function can be used for integration by breaking down the function into smaller, simpler pieces and integrating each piece separately. The resulting integrals can then be combined to find the overall integral of the function.

3. What are some common errors when coding a piecewise function for integration?

Some common errors when coding a piecewise function for integration include forgetting to specify the intervals for each piece, not correctly defining the function or its parameters, and not accounting for discontinuities in the function.

4. Can a piecewise function have more than two pieces?

Yes, a piecewise function can have any number of pieces. The number of pieces depends on the complexity of the function and the number of discontinuities or changes in behavior.

5. How do I troubleshoot errors when using a piecewise function for integration?

If you encounter errors when using a piecewise function for integration, some steps you can take to troubleshoot include checking for typos or missing symbols in your code, verifying that the function and its pieces are correctly defined, and double-checking the intervals and any discontinuities in the function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
700
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
904
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
Replies
3
Views
658
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
Replies
1
Views
812
Back
Top