Legendre Polynomials in VBA

wellinth

Board Regular
Joined
Aug 6, 2004
Messages
58
I wrote a worksheet function in VBA to calculate the nth Legendre polynomial for a given value of x. The code is shown below. I'll be glad to answer any questions about it. I can also send anyone an Excel file that uses this function. Hope this helps.

Regards,

- Tom

Public Function Legendre(n As Integer, x As Double) As Double
'Computes the nth Legendre polynomial given x
Dim Pn As Double, Pn_1 As Double, Pn_2 As Double
Dim j As Integer
If n < 0 Then
MsgBox "n must be >= 0. Error"
Legendre = 0
Exit Function
End If
'P0 is 1 and P1 is x.
If n = 0 Then
Pn = 1
ElseIf n = 1 Then
Pn = x
Else 'n >= 2 so calculate Legendre polynomial iteratively
Pn_1 = 1
Pn = x
For j = 2 To n
Pn_2 = Pn_1
Pn_1 = Pn
Pn = (((2 * j - 1) * x * Pn_1) - ((j - 1) * Pn_2)) / j
Next j
End If
Legendre = Pn
End Function
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top