#include<stdio.h>
#include<math.h>
double Ti(double a,double b);
double f(double a);
void main()
{
double a,b,Y;
printf("请输入积分上限:");
scanf("%f",&a);
printf("请输入积分下限:");
scanf("%f",&b);
Y=Ti(a,b);
printf("计算结果为:%10.8f",Y);
printf("\n");
}
double Ti(double a,double b)
{
int i,n=3,t=1;
double s1,s2,F[3],x,h;
while(t)
{
F[0]=f(a)+f(b);
F[1]=0;
F[2]=0;
for(i=1;i<n-1;i++)
{
h=(b-a)/(n-1);
x=a+i*h;
F[1]+=f(x);
}
s1=h*(F[0]+2*F[1])/2;
for(i=1;i<n;i++)
{
h=(b-a)/n;
x=a+i*h;
F[2]+=f(x);
}
s2=h*(F[0]+2*F[2])/2;
if(fabs(s2-s1)<0.00000001)
{
printf("计算量为:%d\n",n);
return(s2);
t=0;
}
else n++;
}
}
double f(double a)
{
return(13*(a-a*a)*exp(-1.5*a));
}
#include<math.h>
double Ti(double a,double b);
double f(double a);
void main()
{
double a,b,Y;
printf("请输入积分上限:");
scanf("%f",&a);
printf("请输入积分下限:");
scanf("%f",&b);
Y=Ti(a,b);
printf("计算结果为:%10.8f",Y);
printf("\n");
}
double Ti(double a,double b)
{
int i,n=3,t=1;
double s1,s2,F[3],x,h;
while(t)
{
F[0]=f(a)+f(b);
F[1]=0;
F[2]=0;
for(i=1;i<n-1;i++)
{
h=(b-a)/(n-1);
x=a+i*h;
F[1]+=f(x);
}
s1=h*(F[0]+2*F[1])/2;
for(i=1;i<n;i++)
{
h=(b-a)/n;
x=a+i*h;
F[2]+=f(x);
}
s2=h*(F[0]+2*F[2])/2;
if(fabs(s2-s1)<0.00000001)
{
printf("计算量为:%d\n",n);
return(s2);
t=0;
}
else n++;
}
}
double f(double a)
{
return(13*(a-a*a)*exp(-1.5*a));
}