8.13 用递归方法求n阶勒让德多项式的值

 

#include <stdio.h>

int main(){
    double n,x,y;
    double p(double n,double x);
    printf("input n,x(n>=0):\n");
    scanf("%lf,%lf",&n,&x);
    //方程pn(x)
    y=p(n,x);
    printf("the result is %lf\n",y);
}

double p(double n,double x){
    double y;
    if(n==0) y=1;
    else if(n==1) y=x;
    if(n>=1) {
        y=((2*n-1)*x-p(n-1,x)-(n-1)*p(n-2,x))/n;//递归
    }
    return y;
}

结果:

posted @ 2017-07-29 11:06  Allen101  阅读(1029)  评论(0编辑  收藏  举报