标题:c语言积分问题请教
只看楼主
sinodoo
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-7-8
结帖率:100%
已结贴  问题点数:20 回复次数:1 
c语言积分问题请教
一个分段函数高斯积分,编写代码如下,正确结果是1,但是运行程序结果为0,但是编译既无错误也没有警告,不知道哪里出错,请高手帮忙看看,谢谢
#include <stdio.h>
#include <math.h>
#define N 5
double gauss(double(*)(double), double, double, int);
double function(double x);
int main( )
{
        double function(double);
        double a, b, ans;
        printf ("请输入积分上限b\n");
        scanf("%f", &b);
        printf("请输入积分下限a\n");
        scanf("%f", &a);
        ans=gauss(function, a, b, N);
        printf("ans=%f", ans);
        return 0;
}
 
double gauss(double(*func)(double x), double a, double b, int n )/*高斯求积*/
{
        /*高斯点及其求积系数列表*/
        double x1[1]={0.0};
        double A1[1]={2};
        double x2[2]={-0.5573503, 0.5573503};
        double A2[2]={1, 1};
        double x3[3]={-0.7745967, 0.0, 0.7745967};
        double A3[3]={0.555556, 0.888889, 0.555556};
        double x4[4]={0.3399810, -0.3399810, 0.8611363, -0.8611363};
        double A4[4]={0.6521452, 0.6521452, 0.3478548, 0.3478548};
        double x5[5]={0.0, 0.5384693, -0.5384693, 0.9061799, -0.9061799};
        double A5[5]={0.5688889, 0.4786287, 0.4786287, 0.2369269, 0.2369269};
        double *p, *t, g;
        int i;
        switch (n)
        {
                case 1:
                p=x1;
                t=A1;
                break;
                case 2:
                p=x2;
                t=A2;
                break;
                case 3:
                p=x3;
                t=A3;
                break;
                case 4:
                p=x4;
                t=A4;
                break;
                case 5:
                p=x5;
                t=A5;
                break;
                default:
                printf ("intput wrong!");
        }
        for (i=0,g=0; i<n; i++){
                g+=(*func)((b-a)*p[i]/2+(a+b)/2)*t[i];
        }
        g*=(b-a)/2;
        return g;
}
double function(double x)
{
    if(x>=-3 && x<=-2)
        return (pow((x+3), 5)/120);
    else if(x>=-2 && x<=-1)
        return ((pow((x+3),5)-6*pow((x+2),5))/120);
    else if(x>=-1 && x<=0)
        return ((pow((x+3),5)-6*pow((x+2),5)+15*pow((x+1),5))/120);
    else if(x>=0 && x<=1)
        return ((pow((3-x),5)-6*pow((2-x),5)+15*pow((1-x),5))/120);
    else if(x>=1 && x<=2)
        return ((pow((3-x),5)-6*pow((2-x),5))/120);
    else if(x>=2 && x<=3)
        return (pow((3-x), 5)/120);
    else
        return 0;
}
搜索更多相关主题的帖子: function 运行程序 include double c语言 
2013-07-10 16:07
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
得分:20 
编译没有错误和警告只是调试的第一步,编译器不会告诉你算法逻辑上的错误。这个只能你自己调试,可以用单步调试也可以在一些主要地方printf。

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2013-07-11 06:08



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-417054-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.745984 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved