MATLAB Is there a faster way to symbolically integrate Legendre polynomials?

  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Integrate
AI Thread Summary
The discussion revolves around integrating Legendre polynomials and optimizing the process for better accuracy and efficiency. The user initially shares a MATLAB approach using a loop to compute values of Legendre polynomials, expressing a need for higher accuracy by increasing the number of components in the variable x to around 1000 or more. Suggestions are provided to improve the method, emphasizing the use of Gaussian quadrature instead of regular sampling for better accuracy. The user is guided to define a function handle for the Legendre polynomial calculations, which simplifies the integration process. Further, the user seeks advice on constructing a matrix of sine functions for integration without explicitly defining x nodes. The conversation highlights the transition from a loop-based approach to a more efficient vectorized function handle method, enhancing computational speed and accuracy.
member 428835
Hi PF!

I'm doing several operations involving integrating Legendre polynomials. Since I am trying to loop through, my approach is something like
Code:
N = 5;
a = 0.5;
x =linspace(-1,1,100);
for k=1:N

    v(:,k) = legendreP(k+1,x)-legendreP(k+1,a)/legendreP(1,a)*legendreP(1,x);

end% for j

where I then perform Gram-Schmidt over each ##v## vector. Desired accuracy implies I need to have ##x## have about 1000 components (actually more). I would like to do this symbolically, but I'm unsure how to build ##v## as a function handle (the looping is throwing me off). Any help?

I have it working well in Mathematica, but it's a little slower than I'd prefer.
 
Physics news on Phys.org
joshmccraney said:
Desired accuracy implies I need to have ##x## have about 1000 components (actually more).
That's because you are not using the right approach. You should not be sampling the function at regular intervals. Check out these links:
https://en.wikipedia.org/wiki/Gaussian_quadrature
http://mathworld.wolfram.com/Legendre-GaussQuadrature.html
http://homepage.divms.uiowa.edu/~atkinson/ftp/ENA_Materials/Overheads/sec_5-3.pdf

Edit: Found this also: https://www.mathworks.com/matlabcen...0-legendre-gauss-quadrature-weights-and-nodes
 
  • Like
Likes jasonRF and member 428835
You can define V as a function handle this way.

V = @(x,k,a) legendreP(k+1,x)-legendreP(k+1,a)/legendreP(1,a)*legendreP(1,x);

Then to do the integration, you can do this:

integral(@(x) V(x,1,0.05),-1,1)
 
mfig said:
You can define V as a function handle this way.

V = @(x,k,a) legendreP(k+1,x)-legendreP(k+1,a)/legendreP(1,a)*legendreP(1,x);

Then to do the integration, you can do this:

integral(@(x) V(x,1,0.05),-1,1)
But how can I loop through this so V contains multiple values of a, something like V being a vector, where each entry corresponds to a Legendre polynomial with a different a value?
 
If you post what you are trying to do with V, meaning the code that uses V, perhaps I can help. :-)
 
mfig said:
If you post what you are trying to do with V, meaning the code that uses V, perhaps I can help. :-)
The code is very long. I can send you it if you'd like but perhaps this way would be easier: let's say I'm trying to build a matrix $$A_{ij} = \int_0^1\sin(i x)\sin(j x)\,dx$$ One way to compute this integral is to make a matrix of sines such that the rows represent a node ##x_i## and the columns represent a harmonic. Example
$$
S = \begin{bmatrix}
\sin(x) & \sin(2x)&\sin (3x)
\end{bmatrix}
$$

Then to compute ##\int_0^1 \sin(ix)\sin(jx)\,dx## I take ##trapz(S(:,i).*S(:,j))dx##. However, isn't there a way to define ##S## (matrix of sines) as a vector of function handles rather than a matrix, so I wouldn't have to explicitly specify the ##x## nodes?
 
O.k., I think I see. What is wrong with the previous suggestion? Option 2 doesn't depend on specified x nodes, and is very accurate by default.

Pa1AhgY.png
 

Attachments

  • Pa1AhgY.png
    Pa1AhgY.png
    29.5 KB · Views: 474
  • Like
Likes member 428835
Thanks!
 
Back
Top