Clear Filters
Clear Filters

How can I calculate the integral of Legendre function P(n,theta) ?

7 views (last 30 days)
How can I calculate the integral of Legendre function P(n,theta) ?
say:
int(cos(theta)*P(n,theta),theta=0..pi) ;

Accepted Answer

Torsten
Torsten on 5 May 2024
Edited: Torsten on 5 May 2024
The Legendre function is a numerical, not a symbolic function in MATLAB.
Further, it's define for -1 < x < 1. So integration from 0 to pi does not work.
format long
n = 10;
integral(@(x)cos(x).*legendre(n,x),0,0.5,'ArrayValued',1)
ans = 11x1
1.0e+08 * 0.000000000145441 -0.000000001206777 -0.000000017891223 0.000000175228961 0.000002390134810 -0.000029734731041 -0.000254703600961 0.004659715709494 0.007831261641120 -0.449733569474298
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Or do you mean the Legendre polynomials:
?
syms x
n = 10;
int(cos(x)*legendreP(n,x),x,0,sym(pi))
ans = 
  7 Comments
Shreen El-Sapa
Shreen El-Sapa on 6 May 2024
Edited: Shreen El-Sapa on 6 May 2024
associate legendreP @Torsten
I wrote it as Maple code, but I did not know how can I call it in Matlab, can you help me?
Torsten
Torsten on 6 May 2024
As a MATLAB function, I can only find this one for ssociated Legendre functions, and it does not accept symbolic input aguments:
Thus you have to use the numerical integration method that I gave you at first.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 6 May 2024
Edited: John D'Errico on 6 May 2024
I think you do not understand that a Legendre polynomial is meaningless over some other interval. It has no special properties when applied over a different interval. For example, we know that on the interval [-1,1], they form an orthogonal family.
syms x
P2 = legendreP(2,x);
P3 = legendreP(3,x);
int(P2*P3,-1,1)
ans = 
0
However, over some other interval, you get:
int(P2*P3,0,pi)
ans = 
Yes. Random garbage. You need to understand that fact. On some other interval, they have no useful value or meaning. And that means you need to transform them, IF you will try to use thm on some other interval.
Now first, I will point out that LegendreP returns a polynomial with the property that the integral over [-1,1] the integral of P_n*P_n is 1/(n+1/2). (This is stated in the help docs for legendreP.) So personally, I would tend to make them an orthonormal family, by rescaling by the sqrt of that value.
mylegendreP = @(n,x) legendreP(n,x)*sqrt(sym(n)+1/2);
Testing that, we would have
int(mylegendreP(2,x)*mylegendreP(3,x),-1,1)
ans = 
0
int(mylegendreP(3,x)*mylegendreP(3,x),-1,1)
ans = 
1
And as you see, they now form an orthonormal family.
Next, in order to use them over a different interval, you need to transform them. Over the interval [a,b], we could do that like this:
mytranslegendreP = @(n,x,a,b) mylegendreP(n,(x-(a+b)/2)/((b-a)/2))/sqrt((b-a)/2)
mytranslegendreP = function_handle with value:
@(n,x,a,b)mylegendreP(n,(x-(a+b)/2)/((b-a)/2))/sqrt((b-a)/2)
I need to use pi in a symbolic form to make sure there are no numerical problems.
mytranslegendreP(2,x,0,sym(pi))
ans = 
It looks a little messy, but test it out.
int(mytranslegendreP(2,x,0,sym(pi))*mytranslegendreP(3,x,0,sym(pi)),0,sym(pi))
ans = 
0
int(mytranslegendreP(5,x,0,sym(pi))*mytranslegendreP(5,x,0,sym(pi)),0,sym(pi))
ans = 
1
You can see the transformed family is now orthonormal on the interval [0,pi].
In your problem, now we can use it properly.
n = 10;
int(cos(x).^2*mytranslegendreP(n,x,0,sym(pi)),0,sym(pi))
ans = 
vpa(ans)
ans = 
0.000022277912611803161679787506990139
As I said though, just using the default legendre polynomials on some other interval will yield meaningless garbage. Of course, it is entirely your choice to do as you want. But someone needs to tell you that what you want to do is patently silly and useless. Maybe this is homework. Your choice, anyway.
  4 Comments

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!