[C language] Find Legendre polynomials of order n by recursion Author: Chuwen Time: 2019-06-02 Classification: C/C++ comment #Title ># # Solve Legendre polynomials of order n by recursion. The recursion formula is: ``` { 1 (n=0) Pn(x) = { x (n=1) { ((2n-1)*x-Pn-1(x)-(n-1)*On-2(x))/n (n>=1) ``` ! [Solve Legendre polynomials of order n by recursion, and the recursion formula is] [1] #Solutions 1. It is required to input the values of n and x 2. Output the value of Pn (x) #C language code implementation ```c #include "stdio.h" int main(){ double legendre(double n, double x);// Claiming user-defined function legendre double n,x; Printf ("Please enter n, x (separated by spaces):"); scanf("%lf %lf", &n, &x); Printf ("result:% lf", legendre (n, x)); return 0; } double legendre(double n, double x){ double s; if(n == 0){ s = 1; }else if(n == 1){ s = x; }else if(n > 1){ //Use recursive legendre (n-1.0, x), legendre (n-2.0, x) s = ((2.0*n-1.0)*x-legendre(n-1.0, x)-(n-1.0)*legendre(n-2.0, x))/n; } return s; } ``` #Test ># # Enter the values of n and x as' 2 'and' 2` ># # Predicted return results: ` 1.5` ``` Please enter n, x (separated by spaces): 2 2 Result: 1.500000 ``` [1]: https://cdn.nowtime.cc/2019/06/02/4033601759.jpg