Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2529319
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-08-11 15:17:07

用递归方法求n阶勒让德多项式的值,递归公式为:
Pn (x) = 1    n = 0
Pn (x) = x    n = 1
Pn (x) =  ((2n  - 1) * x - Pn-1 (x) - (n -1) * Pn-2 (x) ) / n  n >=1
根据上面的表达式,我们知道了n = 0,n = 1的表达式的值。让n >= 1是,它会产生递归调用,因此我们能很快的写出程序,代码如下:
 

#include <stdio.h>

float myfunction(int,int);
int main(int argc, char *argv[])
{
    int n,x;
    float result;
    printf("please inputa n,x:");
    scanf("%d,%d",&n,&x);
    result = myfunction(n,x);
    printf("the result is : %f",result);
    system("pause");
    return 0;
}

float myfunction(int n,int x)
{
      if (0 == n)
      {
         return 1;
      }
      else if (1 == n)
      {
           return x;
      }
      else
      {
          return ((2 * n - 1) * x - myfunction(n - 1 , x) - (n - 1) * myfunction(n - 2, x)) / n;
      }
}


阅读(13862) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~