Is there a faster way to symbolically integrate Legendre polynomials?

  • Context: MATLAB 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    Integrate
Click For Summary

Discussion Overview

The discussion revolves around finding efficient methods for symbolically integrating Legendre polynomials, particularly in the context of numerical accuracy and computational efficiency. Participants explore various approaches to defining functions and handling integration in MATLAB.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant shares their current method for integrating Legendre polynomials using a loop and expresses a desire for a more efficient symbolic approach.
  • Another participant suggests that the current approach of sampling the function at regular intervals may not be optimal and recommends exploring Gaussian quadrature methods.
  • A participant proposes defining a function handle for the Legendre polynomial integration, providing a specific example of how to implement this in MATLAB.
  • Further clarification is sought on how to adapt the function handle to accommodate multiple values of a, indicating a need for a vectorized approach.
  • Another participant requests additional context regarding the intended use of the function handle to provide more targeted assistance.
  • A participant discusses the possibility of constructing a matrix of sine functions for integration and questions whether this can be done using function handles instead of explicitly defined nodes.
  • One participant acknowledges the previous suggestion and indicates a growing understanding of the proposed methods.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to integrate Legendre polynomials, with some advocating for Gaussian quadrature while others explore function handles and matrix representations. The discussion remains unresolved regarding the optimal method.

Contextual Notes

Participants note limitations related to the accuracy of sampling methods and the dependence on specific node definitions, indicating that the integration methods discussed may have varying levels of effectiveness based on these factors.

Who May Find This Useful

This discussion may be useful for individuals interested in numerical methods for integration, particularly in the context of Legendre polynomials and MATLAB programming.

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   Reactions: 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: 514
  • Like
Likes   Reactions: member 428835
Thanks!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
3
Views
3K