c - Bad arguments error, implementing Numerical Recipes function -
edit: typo error. wrote 1 1 should l ell. everyone!
i hate ask n00b question on so, write simple program check functions written in numerical recipes press, teukolsky, etc. i'm inexperienced using c, may entirely obvious. (please patient!)
the function in question associated legendre polynomial chapter on special functions. have documented thoroughly in code below. however, appears not understand how call function correctly. when running program below using $ ./a.out after compiling gcc program.c, following error:
run time error.... bad arguments in routine plgndr exiting system i'm guessing obvious fix, far haven't quite figured out. should better understand c after question, if not helpful other readers...
the nrerror function standard of numerical recipes series, found in appendix b. here code (excuse extensive comments @ beginning):
/* recurrence relation, legendre polynomial numerical recipes in c, 1992 press, flannery, teukolsky, vetterling chapter 6., special functions, section 6.8 spherical harmonics, p.252-254 legendre polynomial recurrence relation written (n+1)*p_{n+1}(x)=(2n+1)*x*p_n(x)-n*p_{n-1}(x) float plgndr(int l, int m, float x) computes associated legendre polynomial p^m_l(x). here m , l integers satisfying 0<=m<=l, while x lies in range -1<=x<=1. p(0,x) = 1 p(1,x) = x p(n,x) = (2*n-1)/n * x * p(n-1,x) - (n-1)/n * p(n-2,x) */ #include <stdio.h> // standard file input , output header #include <stdlib.h> // utility functons such malloc() , rand() #include <string.h> #include <math.h> // mathematical functions such sin() , cos()} #include <stddef.h> // error function /* function declarations */ void nrerror(char error_text[]); double plgndr(int l, int m, double x); /* program begins */ int main(void) { int l=2; /* asign values variables here */ int m=2; double x=0.5; // x must between -1 , 1 plgndr(l,m,x); return 0; } /* functions */ void nrerror(char error_text[]) //numerical recipes standard error handler { fprintf(stderr, "run time error....\n"); fprintf(stderr, "%s\n", error_text); fprintf(stderr, "now exiting system\n"); exit(1); } double plgndr(int l, int m, double x) //originally written float /* computes associated legendre polynomial p^m_l(x). here m , l integers satisfying 0<=m<=l, while x lies in range -1<=x<=1. */ { void nrerror(char error_text[]); double fact,pll,pmm,pmmp1,somx2; int i,ll; if (m < 0 || m > 1 || fabs(x) > 1.0) nrerror("bad arguments in routine plgndr"); pmm=1.0; //compute p^m_m if (m > 0){ somx2=sqrt((1.0-x)*(1.0+x)); fact=1.0; (i=1; i<=m; i++){ pmm *= -fact*somx2; fact += 2.0; } } if (l == m) return pmm; else{ //compute p^m_{m+1} pmmp1=x*(2*m+1)*pmm; if (l == (m+1)) return pmmp1; else { //compute p^m_l, l > m+1 (ll=m+2; ll<=l; ll++){ pll=(x*(2*ll-1)*pmmp1-(ll+m-1)*pmm)/(ll-m); pmm=pmmp1; pmmp1=pll; } return pll; } } }
according comment next definition of plgndr(),
(...) m , l integers satisfying 0<=m<=l, while x lies in range -1<=x<=1.
update following line in plgndr():
if (m < 0 || m > 1 || fabs(x) > 1.0) to
if (m < 0 || m > l || fabs(x) > 1.0) /* ^ */ /* (l, not 1) */ it's stupid typo. change code doesn't crash, i'm not able verify mathematically.
Comments
Post a Comment