标题:牛顿-科特斯公式-求积分----编译通过,但是得不到答案--差一步了,看谁眼尖
只看楼主
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
结帖率:100%
已结贴  问题点数:20 回复次数:6 
牛顿-科特斯公式-求积分----编译通过,但是得不到答案--差一步了,看谁眼尖
#include<stdio.h>
#include<math.h>
#define maxsize 100
#define eps 0.000001
int main(void)
 {
     double a,b,t,x[maxsize],step,s0,s1;
     double m1,m2,m3,sum1,sum2,sum3,sum4;
     double f(double m);
     int i,n;
     printf("请输入积分下限:\n");
     scanf("%lf",&a);
     printf("请输入积分上限:\n");
     scanf("%lf",&b);
 
     n=0;
     s0=0;
     do
     {
         n=n+1;
         step=(b-a)/n;
         for(i=0;i<=n;i++)
             x[i]=a+i*step;
         sum1=sum2=sum3=sum4=0;
         for(i=0;i<=n-1;i++)
         {
             m1=x[i]+(x[i+1]-x[i])/4;
             sum1+=f(m1);
         }
         for(i=0;i<=n-1;i++)
         {
             m2=x[i]+(x[i+1]-x[i])/2;
             sum2+=f(m2);
         }
         for(i=0;i<=n-1;i++)
         {
             m3=x[i]+(x[i+1]-x[i])*3/4;
             sum3+=f(m3);
         }
         for(i=0;i<=n-1;i++)
         {
             sum4+=f(x[i]);
         }
         s1=0;
         s1+=(7*(f(a)+f(b))+32*sum1+12*sum2+32*sum3+14*sum4)*step/90;
         t=s0;
         s0=s1;
     }

     while (fabs(s0-t)>eps&&n<100);
     {
         printf("s0=%lf\n",s0);
         printf("此时步长为:%d",n);
         printf("\n");
     }
     return 0;
 }
 double f(double c)
 {
     double y;
     y=c;
     return y;
 }
     
搜索更多相关主题的帖子: include 积分 double 
2012-12-09 14:38
embed_xuel
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:58
帖 子:3845
专家分:11385
注 册:2011-9-13
得分:5 
会调试和单步跟踪吗?

总有那身价贱的人给作业贴回复完整的代码
2012-12-09 14:40
embed_xuel
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:58
帖 子:3845
专家分:11385
注 册:2011-9-13
得分:0 
把注释加上

总有那身价贱的人给作业贴回复完整的代码
2012-12-09 14:42
青春无限
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江苏
等 级:贵宾
威 望:24
帖 子:3450
专家分:19340
注 册:2012-3-31
得分:5 
学习

学 会看代码…学习写程序…学会搞开发…我的目标!呵呵是不是说大话啊!!一切皆可能
2012-12-10 11:50
空沃夫
Rank: 2
来 自:老和山职业技术学院
等 级:论坛游民
帖 子:48
专家分:76
注 册:2012-12-7
得分:5 
以下是引用embed_xuel在2012-12-9 14:42:20的发言:

把注释加上

同意,楼主你注释下,再把你要实现的输出解释下。就一个公式,理解不了。
2012-12-10 11:59
daniel_
Rank: 5Rank: 5
等 级:职业侠客
帖 子:212
专家分:307
注 册:2012-11-13
得分:5 
吧这个公式呈上来先
2012-12-10 13:51
daniel_
Rank: 5Rank: 5
等 级:职业侠客
帖 子:212
专家分:307
注 册:2012-11-13
得分:0 
#include<stdio.h>
#include<math.h>
#define maxsize 100
#define eps 0.000001

double f(double m);

int main(void)
{
     double a = 0.0;
     double b = 0.0;
     double t = 0.0;
     double x[maxsize] = {0.0};
     double step = 0.0;
     double s0 = 0.0;
     double s1 = 0.0;
     double m1 = 0.0;
     double m2 = 0.0;
     double m3 = 0.0;
     double sum1 = 0.0;
     double sum2 = 0.0;
     double sum3 = 0.0;
     double sum4 = 0.0;
   
     int i = 0;
     int n = 0;

     printf("请输入积分下限:\n");
     scanf("%lf",&a);
     printf("请输入积分上限:\n");
     scanf("%lf",&b);

     n = 0;
     s0 = 0.0;
     do
     {
         n = n+1;
         step = (b-a) / n;
         for(i = 0;i <= n;i++)
             x[i] = a + i*step;
         sum1 = 0.0;
         sum2 = 0.0;
         sum3 = 0.0;
         sum4 = 0.0;
         for(i = 0;i <= n-1;i++)
         {
             m1 = x[i] + (x[i+1] - x[i]) / 4;
             sum1 += f(m1);
        
             m2 = x[i] + (x[i+1] - x[i]) / 2;
             sum2 += f(m2);
        
             m3 = x[i] + (x[i+1] - x[i]) * 3/4;
             sum3 += f(m3);
        
             sum4 += f(x[i]);
         }
         s1 = 0;
         s1 += (7 * (f(a) + f(b)) + 32*sum1 + 12*sum2 + 32*sum3 + 14*sum4) * step / 90;
         t = s0;
         s0 = s1;
     }while(fabs(s0-t) > eps && n < 100);//这是do {}while();的吧
   
    /*这是干吗的  怎么还有大括号*/
     {
         printf("s0=%lf\n",s0);
         printf("此时步长为:%d",n);
         printf("\n");
     }
     return 0;
}

double f(double c)//这个函数干吗的啊!!!!!!!!!!!怎么感觉没有任何的作用的!!!!!!!!!!
{
     double y;
     y = c;
     return y;
}
------------
    程序从来考来的啊
2012-12-10 14:01



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




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

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