BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ZMX
Fluorite | Level 6 ZMX
Fluorite | Level 6

Hi,

 

I am trying to replicate a simulation that requires using Legendre polynomial function. I have a vector of values between -1 and 1 and need to calculate the Legendre function of different orders evaluated at each element in this vector. I noticed that Matlab has a function (legendre(n,x)) that calculates associated legendre polynomials of degree n  with order m (I only need it for m=0) and this produces the values that I expect to have for this vector. However, I'm doing this simulation work in SAS and am wondering if there is an equivalent built-in function in SAS/IML. 

I noticed that there is the ORPOL function but I am not quite sure if this is the same function as it generates different values.

Any help is much appreciated.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

I think you didn't read all the way to the end of the doc for ORPOL. It includes an example of how to generate Legendre polynomials by using the usual three-term recurrence. The doc example computes the Legendre polynomials up through degree 6. The only thing I added was writing to a SAS data set and plotting the polynomials on [-1, 1]:

 

proc iml;
maxDegree = 6;
/* evaluate polynomials at these points */
x = T( do(-1,1,0.05) );

/* define the standard Legendre Polynomials
   Using the 3-term recurrence with
   A[j]=0, B[j]=(2j-1)/j, and C[j]=(j-1)/j
   and the standardization P_j(1)=1
   which implies P_0(x)=1, P_1(x)=x. */
legendre = j(nrow(x), maxDegree+1);
legendre[,1] = 1; /* P_0 */
legendre[,2] = x; /* P_1 */

do j = 2 to maxDegree;
   legendre[,j+1] = (2*j-1)/j # x # legendre[,j] -
                      (j-1)/j # legendre[,j-1];
end;
*print legendre;

L = x || Legendre;
create Legendre from L[c=('x' || ('L0':'L6'))];
append from L;
close;

QUIT;

proc sgplot data=Legendre;
series x=x y=L0;
series x=x y=L1;
series x=x y=L2;
series x=x y=L3;
series x=x y=L4;
series x=x y=L5;
series x=x y=L6;
run;

View solution in original post

3 REPLIES 3
Rick_SAS
SAS Super FREQ

I think you didn't read all the way to the end of the doc for ORPOL. It includes an example of how to generate Legendre polynomials by using the usual three-term recurrence. The doc example computes the Legendre polynomials up through degree 6. The only thing I added was writing to a SAS data set and plotting the polynomials on [-1, 1]:

 

proc iml;
maxDegree = 6;
/* evaluate polynomials at these points */
x = T( do(-1,1,0.05) );

/* define the standard Legendre Polynomials
   Using the 3-term recurrence with
   A[j]=0, B[j]=(2j-1)/j, and C[j]=(j-1)/j
   and the standardization P_j(1)=1
   which implies P_0(x)=1, P_1(x)=x. */
legendre = j(nrow(x), maxDegree+1);
legendre[,1] = 1; /* P_0 */
legendre[,2] = x; /* P_1 */

do j = 2 to maxDegree;
   legendre[,j+1] = (2*j-1)/j # x # legendre[,j] -
                      (j-1)/j # legendre[,j-1];
end;
*print legendre;

L = x || Legendre;
create Legendre from L[c=('x' || ('L0':'L6'))];
append from L;
close;

QUIT;

proc sgplot data=Legendre;
series x=x y=L0;
series x=x y=L1;
series x=x y=L2;
series x=x y=L3;
series x=x y=L4;
series x=x y=L5;
series x=x y=L6;
run;

Rick_SAS
SAS Super FREQ

Did my response answer your question? If so, please mark as "Answered." If not, let us know your other questions.

ZMX
Fluorite | Level 6 ZMX
Fluorite | Level 6
Thanks so much for your help. Yes, I just got access to my work PC and could run the program. It does work. Thanks again.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 3 replies
  • 1209 views
  • 2 likes
  • 2 in conversation