不要让昨天 占据你的今天 夏午晴天

夏午晴天

Legendre多项式

Legendre多项式

时间限制: 1 Sec  内存限制: 128 MB

题目描述

Legendre多项式的递归公式

编写程序, 输出n阶Legendre多项式x在[-1,1]闭区间的101个点值, (每个点等间距)

 

 

输入

输入阶数 n

输出

输出n阶Legendre多项式x在[-1,1]闭区间的101个点值, 保留4位有效小数(每个点等间距)

样例输入

5

样例输出

-1.0000
-0.7204
-0.4796
-0.2744
-0.1017
0.0411
0.1570
0.2484
0.3177
0.3674
0.3995
0.4162
0.4193
0.4107
0.3922
0.3652
0.3313
0.2919
0.2482
0.2014
0.1526
0.1028
0.0529
0.0037
-0.0441
-0.0898
-0.1330
-0.1730
-0.2095
-0.2421
-0.2706
-0.2948
-0.3144
-0.3294
-0.3397
-0.3454
-0.3465
-0.3431
-0.3353
-0.3234
-0.3075
-0.2880
-0.2650
-0.2389
-0.2101
-0.1788
-0.1455
-0.1106
-0.0744
-0.0374
0.0000
0.0374
0.0744
0.1106
0.1455
0.1788
0.2101
0.2389
0.2650
0.2880
0.3075
0.3234
0.3353
0.3431
0.3465
0.3454
0.3397
0.3294
0.3144
0.2948
0.2706
0.2421
0.2095
0.1730
0.1330
0.0898
0.0441
-0.0037
-0.0529
-0.1028
-0.1526
-0.2014
-0.2482
-0.2919
-0.3313
-0.3652
-0.3922
-0.4107
-0.4193
-0.4162
-0.3995
-0.3674
-0.3177
-0.2484
-0.1570
-0.0411
0.1017
0.2744
0.4796
0.7204
1.0000

提示

 

x的取值在[-1, 1]之间, 每个点之间相距0.02, 即x=x+0.02, 这样就有101个点的数据

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 
 5 double Leg(int n, double x)
 6 {
 7     double ans;
 8     if(n == 0)
 9         ans = 1;
10     else if(n == 1)
11         ans = x;
12     else ans = ( (2 * n - 1) * x * Leg(n-1,x) - (n - 1) * Leg(n - 2,x) ) / n;
13     return ans;
14 }
15 
16 int main()
17 {
18     int n;
19     double x;
20     scanf("%d",&n);
21     for(x = -1; x <= 1.02; x=x+0.02)
22     {
23         double leg;
24         leg = Leg(n,x);
25         printf("%.4lf\n",leg);
26     }
27     return 0;
28 }

 

posted on 2017-03-18 15:34  夏晴天  阅读(1084)  评论(0编辑  收藏  举报

导航

Live2D