[原创]辛普生二重积分程序气死我了!!救救我啊!!!
编译出现了几个同样的错误:warning C4244: '=' : conversion from 'double ' to 'int ', possible loss of data
而且运行结果也不对,快要论文答辩了.个位大虾能不能帮帮我啊~~
还要用MATLAB画图,我都不会用.!惨了!
学校的毕业论文真是害死人啊~~又没学过的软件,怎么做啊!!!!
#include <stdio.h>
#include <math.h>
#include <malloc.h>
#include <conio.h>
double *fls(double x)
{
double *y;
y=(double *)malloc(2*sizeof(double));
y[0]=-sqrt(1-x*x);
y[1]=-y[0];
return y;
}
double f(double x,double y)
{
double result;
result=exp(x*x+y*y);
return result;
}
double *simps1(double x,double eps)
{
double *result,*y;
int n=1,i,kflag=0;
double h,e,t1,yy,t2,s,s0;
result=(double *)malloc(3*sizeof(double));
y=fls(x);
h=0.5*(y[1]-y[0]);
e=fabs(y[0])+fabs(y[1]);
t1=h*(f(x,y[0])+f(x,y[1]));
do
{
yy=y[0]-h;
t2=0.5*t1;
for(i=1;i<=n;i++)
{
yy=yy+2.0*h;
t2=t2+h*f(x,yy);
}
s=(4.0*t2-t1)/3.0;
n=n+n;
if(n<8)
{s0=s;
t1=t2;
h=0.5*h;
}
if(fabs(s-s0)<=eps*(fabs(s)+1.0))
break;
s0=s;
t1=t2;
h=0.5*h;
}while(e+h!=e);
if(y[0]!=y[1])
kflag=kflag+1;
result[0]=kflag;
result[1]=n;
result[2]=s;
return(result);
}
double *simps2(double a,double b,double eps)/*a积分下限b积分上限eps给定的计算精度*/
{
double *result,*m,n,sum,sum0;
int n2=1,i,kflag=0,flag=0,n1,ss1,n3,ss2;
double h,d=0,s1=0,s2=0,t1,x,t2,g=0,s=0,s0=0,c;
result=(double *)malloc(3*sizeof(double));
c=fabs(a)+fabs(b);
h=0.5*(b-a);
m=simps1(a,eps);
n1=m[1];
ss1=m[2];
m=simps1(b,eps);
n3=m[1];
ss2=m[2];
n=n1+n3+2;
t1=h*(ss2+ss1);
do
{
x=a-h;
t2=0.5*t1;
for(i=1;i<=n2;i++)
{
x=x+2.0*h;
m=simps1(x,eps);
n=n+m[1]+1;
t2=t2+h*m[2];
}
sum=(4.0*t2-t1)/3.0;
n2=n2+n2;
if(n2<8)
{
sum0=sum;
t1=t2;
h=0.5;
}
if(fabs(sum-sum0)<=eps*(fabs(sum)+1.0))
{
result[0]=kflag;
result[1]=n;
result[2]=sum;
flag=1;
break;
}
s0=s;
t1=t2;
h=0.5*h;
}while(c+h!=c);
if(flag==1)
kflag=kflag+1;
result[0]=kflag;
result[1]=n;
result[2]=sum;
return(result);
}
main()
{double a,b,EPS,c;
double *result;
printf("请输入积分下限:\na=");
scanf("%f",&a);
printf("请输入积分上限:\nb=");
scanf("%f",&b);
printf("请输入计算精度:\nEPS=");
scanf("%f",&EPS);
result=simps2(a,b,EPS);
printf("您计算的二重积分的答案是:\n");
printf("I=%f",result[2]);