n阶勒让德多项式的定义是递归的进行定义,所以求解,也需要进行递归的求解。代码如下:
- #include <stdio.h>
-
-
float rand(int n, float x)
-
{
-
if(n == 0) return 1;
-
else if(n == 1) return x;
-
else return ((2 * n) * x - rand(n-1, x) - (n - 1) * rand(n-2, x)) / n;
-
}
-
-
int main(int argc, char *argv[])
-
{
-
int n;
-
float x,p;
-
printf("please input n\n");
-
scanf("%d",&n);
-
printf("please input x\n");
-
scanf("%f",&x);
-
p = rand(n,x);
-
printf("the result of P%d(%f) = %f\n",n,x,p);
-
return 0;
-
}
阅读(4603) | 评论(0) | 转发(0) |