Is there a faster way to symbolically integrate Legendre polynomials?

  • MATLAB
  • Thread starter member 428835
  • Start date
  • Tags
    Integrate
In summary, the conversation discusses integrating Legendre polynomials and the use of a for loop and Gram-Schmidt method. The desired accuracy requires a large number of components for x. The suggestion is to use Gaussian quadrature instead of sampling at regular intervals. The conversation then transitions to discussing how to define V as a function handle and how to loop through it for multiple values of a. The final part discusses finding a way to define a matrix of sines as a vector of function handles to avoid explicitly specifying x nodes.
  • #1
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
  • #2
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
  • #3
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)
 
  • #4
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?
 
  • #5
If you post what you are trying to do with V, meaning the code that uses V, perhaps I can help. :-)
 
  • #6
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?
 
  • #7
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: 412
  • Like
Likes member 428835
  • #8
Thanks!
 

1. What is the purpose of finding an easier way to integrate?

The purpose of finding an easier way to integrate is to simplify the process of combining two or more components or systems together. This can save time and resources, and potentially improve the overall efficiency and effectiveness of the integrated system.

2. What are some common challenges when integrating systems?

Some common challenges when integrating systems include compatibility issues, data transfer, and communication between different components. This can lead to errors, delays, and additional costs.

3. What factors should be considered when looking for an easier way to integrate?

Some factors to consider when looking for an easier way to integrate include the specific needs and requirements of the systems being integrated, the availability of resources and expertise, and the potential benefits and risks of the chosen integration method.

4. Are there any existing solutions or tools for easier integration?

Yes, there are many existing solutions and tools for easier integration, such as middleware, APIs, and integration platforms. These can help streamline the process and make it more efficient and reliable.

5. How can better integration benefit the scientific community?

Better integration can benefit the scientific community by facilitating collaboration and data sharing between researchers and institutions. It can also improve the accuracy and reproducibility of experiments and studies by ensuring seamless integration of different components and data sources.

Similar threads

Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Advanced Physics Homework Help
Replies
1
Views
970
  • Advanced Physics Homework Help
Replies
1
Views
1K
  • Advanced Physics Homework Help
Replies
10
Views
2K
  • Advanced Physics Homework Help
Replies
16
Views
2K
  • Calculus and Beyond Homework Help
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
Back
Top